I'm trying to optimize this query:
SELECT articles.id
FROM articles
INNER JOIN articles_authors ON articles.id=articles_authors.fk_Articles
WHERE articles_authors.fk_Authors=586
ORDER BY articles.publicationDate LIMIT 0,50;
Table articles :
- Engine : MyISAM
- Row_format: Dynamic
- Rows : 1 482 588
- Data_length : 788 926 672
- Max data length : 281 474 976 710 655
- Index length : 127 300 608
- data free : 0
- checksum : null
CREATE TABLE `articles` ( `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, `title` VARCHAR(255) NOT NULL, `publicationDate` DATE NOT NULL DEFAULT '1970-01-01', PRIMARY KEY (`id`), KEY `publicationDate` (`publicationDate`) ) ENGINE=MYISAM AUTO_INCREMENT=1498496 DEFAULT CHARSET=utf8
Table articles_authors :
- Engine : MyISAM
- Row_format: Dynamic
- Rows : 1 970 750
- Data_length : 45 008 420
- Max data length : 281 474 976 710 655
- Index length : 127 300 608
- data free : 0
- checksum : null
CREATE TABLE `articles_authors` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `fk_Articles` int(10) unsigned NOT NULL, `fk_Authors` int(10) unsigned NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `fk_Articles_fk_Authors` (`fk_Articles`,`fk_Authors`), KEY `fk_Articles` (`fk_Articles`), KEY `fk_Authors` (`fk_Authors`), ) ENGINE=MyISAM AUTO_INCREMENT=2349047 DEFAULT CHARSET=utf8
Explain on query :
id (1), select_type(SIMPLE), TABLE(articles_authors), TYPE(ref), possible_keys(fk_Articles_fk_Authors, fk_Articles, fk_Authors), KEY (fk_Authors), Key_len(4), ref(const), ROWS(171568), extra (USING TEMPORARY; USING FILE sort)
id (1), select_type(SIMPLE), TABLE(articles), TYPE(eq_ref), possible_keys(PRIMARY), KEY (PRIMARY), Key_len(4), ref(articles_authors.fk_Authors), ROWS(1), extra ()
As you can see, the SQL query is not optimized (using file sort in explain).
Thanks for your help!
(fk_Authors,publicationDate)
– Pentium10