LONGMAN
Dark Side of the Moon..
Оптимизация простого запроса
Как можно оптимизировать такой запрос:
[sql]
SELECT `a` . *
FROM `content` AS `a`
INNER JOIN `categories` AS `c` ON `a`.`catid` = `c`.`id`
INNER JOIN `sections` AS `s` ON `a`.`sectionid` = `s`.`id`
WHERE `a`.`state` = 1 AND (
`a`.`publish_up` = '0000-00-00 00:00:00' OR `a`.`publish_up` <= '2010-10-04 16:07:53'
) AND (
`a`.`publish_down` = '0000-00-00 00:00:00' OR `a`.`publish_down` >= '2010-10-04 16:07:53'
)
ORDER BY `a`.`publish_up` DESC
LIMIT 6
[/sql]
Explain:
Запрос выполняется 0.5 sec. Записи в таблице content 55000. Моя цель вывести 6 последних статьи. Есть индексы на catid, sectionid, state и один общий на state, publish_up, publish_down
Как можно оптимизировать такой запрос:
[sql]
SELECT `a` . *
FROM `content` AS `a`
INNER JOIN `categories` AS `c` ON `a`.`catid` = `c`.`id`
INNER JOIN `sections` AS `s` ON `a`.`sectionid` = `s`.`id`
WHERE `a`.`state` = 1 AND (
`a`.`publish_up` = '0000-00-00 00:00:00' OR `a`.`publish_up` <= '2010-10-04 16:07:53'
) AND (
`a`.`publish_down` = '0000-00-00 00:00:00' OR `a`.`publish_down` >= '2010-10-04 16:07:53'
)
ORDER BY `a`.`publish_up` DESC
LIMIT 6
[/sql]
Explain:
Код:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE a ref idx_section,idx_state,idx_catid,state idx_state 1 const 10034 Using where; Using temporary; Using filesort
1 SIMPLE s index PRIMARY PRIMARY 4 NULL 2 Using where; Using index
1 SIMPLE c eq_ref PRIMARY PRIMARY 4 web1db1.a.catid 1 Using where; Using index