Оптимизация запроса MySQL
По мере работы над созданием городской доски объявлений я расширял и расширял возможности, что в итоге привело меня к вот такому, как мне кажется уродскому запросу и огромным нагрузкам на mysql:
Код:
SELECT
tpubl.id AS id, tpubl.author AS author, tpubl.editor AS editor, tpubl.title AS pubtitle, tpubl.unititle AS unititle, tpubl.category AS category, tpubl.text AS text, tpubl.pretext AS pretext, tpubl.firsttext AS firsttext,
tpubl.announce AS announce, tpubl.keywords AS keywords, tpubl.date AS date, tpubl.firstdate AS firstdate, tpubl.sortdate AS sortdate, tpubl.osertext AS osertext, tpubl.enddate AS enddate, tpubl.inmain AS inmain, tpubl.maxup AS maxup,
tpubl.archive AS archive, tpubl.reseller AS reseller, tpubl.nopubl AS nopubl, tpubl.blocked AS blocked, tpubl.hidded AS hidded,
tcategory.id AS categoryid, tcategory.name AS categoryname, tcategory.fullname AS categoryfullname, tcategory.uniname AS categoryuniname, tcategory.rsname AS categoryrsname, tcategory.descr AS categorydescr, tcategory.title AS categorytitle, tcategory.childCats AS categorychildCats,
tcategory.skinlist AS categoryskinlist, tcategory.skinpubl AS categoryskinpubl, tcategory.askinpubl AS categoryaskinpubl, tcategory.askinlist AS categoryaskinlist, tcategory.keywords AS categorykeywords, tcategory.description AS categorydescription, tcategory.transactions AS categorytransactions,
ttranscat.id AS transactionid, ttranscat.name AS transactionname, ttranscat.fullname AS transactionfullname, ttranscat.uniname AS transactionuniname, ttranscat.keywords AS transactionkeywords,
tuser.id AS authorid, tuser.name AS authorname, tuser.login AS authorlogin, tuser.phone AS authorphone, tuser.userrights AS authorrightsid, tuser.signature AS authorsignature, tuser.avatar AS authoravatar,
tuser.fullname AS authorfullname, tuser.ban AS ban,
teditor.name AS editorname, teditor.userrights AS editorrightsid,
tarights.name AS authorrightsname, tarights.uniname AS authorrightsuniname,
terights.name AS editorrightsname, terights.uniname AS editorrightsuniname
FROM publ AS tpubl
LEFT JOIN `users` AS tuser ON tpubl.author = tuser.id
LEFT JOIN `users` AS teditor ON tpubl.editor = teditor.id
LEFT JOIN `userrights` AS tarights ON tarights.id = tuser.userrights
LEFT JOIN `userrights` AS terights ON terights.id = teditor.userrights
LEFT JOIN `category` AS `tcategory` ON `tpubl`.`category` = `tcategory`.`id`
LEFT JOIN `category_transaction` AS `ttranscat` ON `tpubl`.`transactions` = `ttranscat`.`id`
WHERE 1 = 1 AND `tpubl`.`reseller` = "1" AND (tcategory.id="1" OR `tpubl`.`category` = "2" OR `tpubl`.`category` = "3" OR `tpubl`.`category` = "4" OR `tpubl`.`category` = "5" OR `tpubl`.`category` = "6" OR `tpubl`.`category` = "7" OR `tpubl`.`category` = "7" OR `tpubl`.`category` = "8" OR `tpubl`.`category` = "9" OR `tpubl`.`category` = "10" OR `tpubl`.`category` = "11" OR `tpubl`.`category` = "12" OR `tpubl`.`category` = "13" OR `tpubl`.`category` = "14" ) AND tpubl.archive = "0" ORDER BY `sortdate` DESC, `date` DESC, maxup DESC LIMIT 0 , 100;
tpubl.id AS id, tpubl.author AS author, tpubl.editor AS editor, tpubl.title AS pubtitle, tpubl.unititle AS unititle, tpubl.category AS category, tpubl.text AS text, tpubl.pretext AS pretext, tpubl.firsttext AS firsttext,
tpubl.announce AS announce, tpubl.keywords AS keywords, tpubl.date AS date, tpubl.firstdate AS firstdate, tpubl.sortdate AS sortdate, tpubl.osertext AS osertext, tpubl.enddate AS enddate, tpubl.inmain AS inmain, tpubl.maxup AS maxup,
tpubl.archive AS archive, tpubl.reseller AS reseller, tpubl.nopubl AS nopubl, tpubl.blocked AS blocked, tpubl.hidded AS hidded,
tcategory.id AS categoryid, tcategory.name AS categoryname, tcategory.fullname AS categoryfullname, tcategory.uniname AS categoryuniname, tcategory.rsname AS categoryrsname, tcategory.descr AS categorydescr, tcategory.title AS categorytitle, tcategory.childCats AS categorychildCats,
tcategory.skinlist AS categoryskinlist, tcategory.skinpubl AS categoryskinpubl, tcategory.askinpubl AS categoryaskinpubl, tcategory.askinlist AS categoryaskinlist, tcategory.keywords AS categorykeywords, tcategory.description AS categorydescription, tcategory.transactions AS categorytransactions,
ttranscat.id AS transactionid, ttranscat.name AS transactionname, ttranscat.fullname AS transactionfullname, ttranscat.uniname AS transactionuniname, ttranscat.keywords AS transactionkeywords,
tuser.id AS authorid, tuser.name AS authorname, tuser.login AS authorlogin, tuser.phone AS authorphone, tuser.userrights AS authorrightsid, tuser.signature AS authorsignature, tuser.avatar AS authoravatar,
tuser.fullname AS authorfullname, tuser.ban AS ban,
teditor.name AS editorname, teditor.userrights AS editorrightsid,
tarights.name AS authorrightsname, tarights.uniname AS authorrightsuniname,
terights.name AS editorrightsname, terights.uniname AS editorrightsuniname
FROM publ AS tpubl
LEFT JOIN `users` AS tuser ON tpubl.author = tuser.id
LEFT JOIN `users` AS teditor ON tpubl.editor = teditor.id
LEFT JOIN `userrights` AS tarights ON tarights.id = tuser.userrights
LEFT JOIN `userrights` AS terights ON terights.id = teditor.userrights
LEFT JOIN `category` AS `tcategory` ON `tpubl`.`category` = `tcategory`.`id`
LEFT JOIN `category_transaction` AS `ttranscat` ON `tpubl`.`transactions` = `ttranscat`.`id`
WHERE 1 = 1 AND `tpubl`.`reseller` = "1" AND (tcategory.id="1" OR `tpubl`.`category` = "2" OR `tpubl`.`category` = "3" OR `tpubl`.`category` = "4" OR `tpubl`.`category` = "5" OR `tpubl`.`category` = "6" OR `tpubl`.`category` = "7" OR `tpubl`.`category` = "7" OR `tpubl`.`category` = "8" OR `tpubl`.`category` = "9" OR `tpubl`.`category` = "10" OR `tpubl`.`category` = "11" OR `tpubl`.`category` = "12" OR `tpubl`.`category` = "13" OR `tpubl`.`category` = "14" ) AND tpubl.archive = "0" ORDER BY `sortdate` DESC, `date` DESC, maxup DESC LIMIT 0 , 100;
Таблица `publ` содержит все объявления пользователей и выглядит примерно так:
`id`,`title`,`announce`,`text`...
Таблица [ B ] `users`[ /B ] содержит всех пользователей и выглядит примерно так:
`id`,`name`,`login`,`userrights`...
Таблица [ B ] `category`[/ B ] содержит список категорий, Недвижимость, Авто, Работа итп
`id`,`name`,`title`,`childs`...
Таблица [ B ] `category_transaction`[ /B ] содержит список типов, Продам, Куплю. Меняю, Сдам итп
`id`,`name`,`title`...
[ B ] Подскажите, как бы вы оптимизировали такой запрос?[ /B ]
UPD. Единственное что некоторое сомнение вызывает, это вот эти связки:
Код:
LEFT JOIN `users` AS tuser ON tpubl.author = tuser.id
LEFT JOIN `users` AS teditor ON tpubl.editor = teditor.id
LEFT JOIN `userrights` AS tarights ON tarights.id = tuser.userrights
LEFT JOIN `userrights` AS terights ON terights.id = teditor.userrights
LEFT JOIN `users` AS teditor ON tpubl.editor = teditor.id
LEFT JOIN `userrights` AS tarights ON tarights.id = tuser.userrights
LEFT JOIN `userrights` AS terights ON terights.id = teditor.userrights
SELECT
tpubl.id AS id, tpubl.author AS author, tpubl.editor AS editor, tpubl.title AS pubtitle, tpubl.unititle AS unititle, tpubl.category AS category, tpubl.text AS text, tpubl.pretext AS pretext, tpubl.firsttext AS firsttext,
tpubl.announce AS announce, tpubl.keywords AS keywords, tpubl.date AS date, tpubl.firstdate AS firstdate, tpubl.sortdate AS sortdate, tpubl.osertext AS osertext, tpubl.enddate AS enddate, tpubl.inmain AS inmain, tpubl.maxup AS maxup,
tpubl.archive AS archive, tpubl.reseller AS reseller, tpubl.nopubl AS nopubl, tpubl.blocked AS blocked, tpubl.hidded AS hidded,
а так видно, что запрос хороший
Начните оптимизацию с EXPLAIN. Что показывает??