I need your help to optimize the query below. Let us assume we have a web application for articles. The software use two table;one is the article table and the second one is the users table. The article table hold the date when the article is created,the id,the body,the title & the section. Let us assume that we have one section called “news” and there are one million article belong to news section. So in this case, how to optimize the following query:
SELECT username,title FROM article,users
WHERE article.auther_id=users.id AND section LIKE 'news'
ORDER BY article.date DESC
LIMIT 0,40
The table structures are:
CREATE TABLE `article` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`title` VARCHAR( 100 ) NOT NULL ,
`body` VARCHAR( 200 ) NOT NULL ,
`date` VARCHAR( 30 ) NOT NULL ,
`auther_id` INT NOT NULL ,
`section` VARCHAR( 30 ) NOT NULL
) ENGINE = MYISAM ;
CREATE TABLE `users` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`username` VARCHAR( 30 ) NOT NULL
) ENGINE = MYISAM ;
I tried to create one index that consists of the section & the date but it is not the best,because if we have 2 millions record and one million of them belong to one section,the DB will scan one million row.
You need to create an index on
(section, date).Don’t include
auther_idas a leading column: articles will be leading in the join and no searching will be performed on this column.Since there is a
LIMIT 0, 40in your query,MySQLwill not have to scan the whole index. It will just pick the first40records.Here’s a test script to check:
t_sourceis a dummy table with1,000,000rows in it.The final query completes in
0.0018 son my machine (instantly)Here’s the execution plan: