索引未在连接视图中用于排序

时间:2017-01-21 18:39:06

标签: mysql database-performance

我有以下架构:

CREATE TABLE `news` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `news_category_id` int(10) unsigned NOT NULL,
  `news_type_id` int(10) unsigned NOT NULL,
  `news_pictures_main_id` int(10) unsigned DEFAULT NULL,
  `title` tinytext COLLATE latin1_general_ci,
  `body` text COLLATE latin1_general_ci,
  `tmstp` timestamp NULL DEFAULT NULL,
  `subcategory` varchar(64) COLLATE latin1_general_ci DEFAULT NULL,
  `source` varchar(128) COLLATE latin1_general_ci DEFAULT NULL,
  `old_id` int(10) unsigned DEFAULT NULL,
  `tags` text COLLATE latin1_general_ci,
  PRIMARY KEY (`id`),
  KEY `news_time_idx` (`tmstp`),
  KEY `fk_news_news_pictures1` (`news_pictures_main_id`),
  KEY `fk_news_news_category1` (`news_category_id`),
  KEY `fk_news_news_type1` (`news_type_id`),
  CONSTRAINT `fk_news_news_category1` FOREIGN KEY (`news_category_id`) REFERENCES `news_category` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `fk_news_news_pictures1` FOREIGN KEY (`news_pictures_main_id`) REFERENCES `news_pictures` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
  CONSTRAINT `fk_news_news_type1` FOREIGN KEY (`news_type_id`) REFERENCES `news_type` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB

CREATE TABLE `news_pictures` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `path` text COLLATE latin1_general_ci,
  `description` text COLLATE latin1_general_ci,
  `author` varchar(45) COLLATE latin1_general_ci DEFAULT NULL,
  `news_id` int(10) unsigned DEFAULT NULL,
  `temp_id` varchar(40) COLLATE latin1_general_ci DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `filename_old_id_unq` (`path`(20),`temp_id`(6)),
  KEY `fk_news_pictures_news1` (`news_id`),
  KEY `temp_id_idx` (`temp_id`(8)),
  CONSTRAINT `fk_news_pictures_news1` FOREIGN KEY (`news_id`) REFERENCES `news` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB

CREATE TABLE `news_category` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(45) COLLATE latin1_general_ci DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB

CREATE TABLE `news_type` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(45) COLLATE latin1_general_ci DEFAULT NULL,
  `slug` varchar(45) COLLATE latin1_general_ci DEFAULT NULL,
  PRIMARY KEY (`id`)
  KEY `news_type_slug_idx` (`slug`)
) ENGINE=InnoDB

由此可以得出以下观点:

CREATE OR REPLACE VIEW `news_full` AS select `n`.`id` AS `id`,
`n`.`title` AS `title`,
`n`.`body` AS `body`,
`n`.`tmstp` AS `tmstp`,
`n`.`subcategory` AS `subcategory`,
`n`.`source` AS `source`,
`n`.`old_id` AS `old_id`,
`n`.`news_type_id` AS `news_type_id`,
`n`.`tags` AS `tags`,
`nt`.`name` AS `news_type_name`,
`nt`.`slug` AS `news_type_slug`,
`n`.`news_pictures_main_id` AS `news_pictures_main_id`,
`np`.`path` AS `news_pictures_main_path`,
`np`.`description` AS `news_pictures_main_description`,
`np`.`author` AS `news_pictures_main_author`,
`np`.`temp_id` AS `news_pictures_main_temp_id`,
`n`.`news_category_id` AS `news_category_id`,
`nc`.`name` AS `news_category_name` 
from (((`news` `n` 
            left join `news_pictures` `np` on((`n`.`news_pictures_main_id` = `np`.`id`))) 
        join `news_category` `nc` on((`n`.`news_category_id` = `nc`.`id`))) 
    join `news_type` `nt` on((`n`.`news_type_id` = `nt`.`id`)));

但是,如果我尝试运行以下查询:

select * from news_full order by tmstp limit 100

我得到以下执行计划(请点击图片展开它):

Explain result

注意第一步中的Using temporary; Using filesort字段。但这很奇怪,因为tmstp字段在基表上被索引。

首先我认为这是视图中的left join,但我已将其更改为inner join,我得到的结果相同。

修改

正如@ Michael-sqlbot巧妙地注意到的那样,查询优化器正在颠倒基表的顺序,首先放置news_categorync)。

如果我将创建视图的查询更改为仅使用LEFT JOIN,则它似乎有效:

New query plan

正如预期的那样,执行时间明显不同: Query comparison

不满意,我使用原始查询创建了另一个视图,添加了STRAIGHT_JOIN语句。因此,查询计划如下:

Query plan - straight join

所以,它没有使用索引。

但是,如果我运行基本查询的计划添加相同的ORDER BYLIMIT子句, 使用索引:

Query plan - straight join no view

1 个答案:

答案 0 :(得分:1)

(不是答案,但还有其他一些问题......)

UNIQUE KEY `filename_old_id_unq` (`path`(20),`temp_id`(6))
  • 这限制了path的前20个字符,以及temp_id的前6个字符在整个表格中是唯一的。你真的想要吗?
  • 我怀疑优化器永远不会使用该索引的两列。 (一般来说,前缀是无用的。)

和...

`title` tinytext COLLATE latin1_general_ci

更改为VARCHAR(255)。有TINYTEXT的缺点,也许没有优势。