Почему IS NULL так медленно работает?
Добрый день. Есть запрос вида:
SELECT `call_call`.*, `call_operation`.`id_operation`
FROM `call_call`
LEFT JOIN `call_operation` ON `call_operation`.`id_call_operation` = `call_call`.`id_call`
WHERE `active_call` = '1' AND `id_operation` IS NULL
Explain:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE call_call ALL NULL NULL NULL NULL 109590 Using where
1 SIMPLE call_operation ALL NULL NULL NULL NULL 5717 Using where; Not exists
Мне нужно получить все записи основной таблицы и узнать для каких из них нет записей в таблице подчиненной. Выполняется этот запрос за 0.3568 сек, получаем чуть больше 500 записей.
Немного изменив условие:
SELECT `call_call`.*, `call_operation`.`id_operation`
FROM `call_call`
LEFT JOIN `call_operation` ON `call_operation`.`id_call_operation` = `call_call`.`id_call`
WHERE `active_call` = '1' AND (`id_operation`>0)
Explain:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE call_operation ALL PRIMARY NULL NULL NULL 5717 Using where
1 SIMPLE call_call eq_ref PRIMARY PRIMARY 8 recaller.call_operation.id_call_operation 1 Using where
Получаем запрос, который делает все в точности до наоборот, извлекает только те записи из главной, у которой есть связные записи с подчиненной. Выполняется этот запрос 0.0074 сек, получаем чуть больше 400 записей.
Уважаемые знатоки, подскажите почему такая большая разница, и в какую сторону копать, чтобы оптимизировать первый запрос? Заранее спасибо.
Эксперименты:
WHERE NOT (`id_operation`>0) /* возвращает 0 строк */
WHERE (`id_operation`<1) /* возвращает 0 строк */
WHERE NOT EXIST `id_operation` /* возвращает error */
WHERE CONVERT(`id_operation`,UNSIGNED) = 0 /* возвращает 0 строк */
WHERE (`id_operation`*1) = 0 /* возвращает 0 строк */
WHERE `id_operation` = NULL /* возвращает 0 строк */
WHERE `id_operation` NOT REGEXP '^[0-9]+$' /* возвращает 0 строк */
SHOW CREATE TABLE call_call
CREATE TABLE `call_call` (
`id_call` bigint(20) NOT NULL auto_increment,
...
`reason_call` varchar(255) collate utf8_unicode_ci NOT NULL,
`active_call` int(2) default NULL,
PRIMARY KEY (`id_call`)
) ENGINE=MyISAM AUTO_INCREMENT=127271 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
SHOW CREATE TABLE call_operation
CREATE TABLE `call_operation` (
`id_operation` bigint(20) unsigned NOT NULL auto_increment,
`id_call_operation` bigint(20) unsigned NOT NULL,
...
`result_operation` varchar(1) collate utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id_operation`)
) ENGINE=MyISAM AUTO_INCREMENT=5782 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
Похожая проблема: http://www.sql.ru/forum/actualthread.aspx?bid=1&tid=111883, но советов по делу нет, человека не поняли.