【发布时间】:2011-08-11 03:06:43
【问题描述】:
我正在尝试优化这个查询:
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;
表格文章:
- 引擎:MyISAM
- 行格式:动态
- 行数:1 482 588
- 数据长度:788 926 672
- 最大数据长度:281 474 976 710 655
- 索引长度:127 300 608
- 免费数据:0
- 校验和:空
创建表 `articles` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`title` VARCHAR(255) NOT NULL,
`publicationDate` DATE NOT NULL DEFAULT '1970-01-01',
主键(`id`),
KEY `publicationDate` (`publicationDate`)
) ENGINE=MYISAM AUTO_INCREMENT=1498496 默认字符集=utf8
表articles_authors:
- 引擎:MyISAM
- 行格式:动态
- 行数:1 970 750
- 数据长度:45 008 420
- 最大数据长度:281 474 976 710 655
- 索引长度:127 300 608
- 免费数据:0
- 校验和:空
创建表`articles_authors` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`fk_Articles` int(10) 无符号非空,
`fk_Authors` int(10) 无符号非空,
主键(`id`),
唯一键`fk_Articles_fk_Authors`(`fk_Articles`,`fk_Authors`),
KEY `fk_Articles` (`fk_Articles`),
KEY `fk_Authors` (`fk_Authors`),
) ENGINE=MyISAM AUTO_INCREMENT=2349047 默认字符集=utf8
解释查询:
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 ()
如您所见,SQL 查询未优化(在说明中使用文件排序)。
感谢您的帮助!
【问题讨论】:
-
+1,一个有据可查的问题!当人们真正包含相关信息时,我会喜欢它!
-
我看不出如何进一步优化,因为在 where/order 子句中你有来自两个不同表的值,你不能创建复合索引
(fk_Authors,publicationDate) -
编辑了答案以包含反规范化选项。
标签: mysql