我有一个MySQL数据库如下表:多列索引VS多个索引
CREATE TABLE `secondary_images` (
`imgId` int(10) unsigned NOT NULL AUTO_INCREMENT,
`primaryId` int(10) unsigned DEFAULT NULL,
`view` varchar(255) DEFAULT NULL,
`imgURL` varchar(255) DEFAULT NULL,
`imgDate` datetime DEFAULT NULL,
PRIMARY KEY (`imgId`),
KEY `primaryId` (`primaryId`),
KEY `imgDate` (`imgDate`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 ;
的SQL将是以下几点:
SELECT imgURL, view FROM secondary_images
WHERE primaryId={$imgId} ORDER BY imgDate DESC
正如你可以看到我做了两个primaryId
和imgDate
,索引键。我背后的想法是因为WHERE
子句的查询结果使用primaryId
和ORDER
子句使用imgDate
。
我的问题是,它会更好,因为我现在使用多个索引?或者我应该多列索引(目前我不太了解的东西)?
这是我从得到解释一下:
id = 1
select_type = simple
table = secondary_images
type = ref
possible_keys = primaryId
key = primaryId
key_len = 5
ref = const
rows = 1
extra = Using where; Using filesort
注:这不是使用多列索引,它是从使用上表中描述的结果。
你可以发布EXPLAIN的选择? :) – Konerak
请记住,indeces不是免费的。如果您有多个indeces,这意味着每次插入或更新时,都需要更新每个索引。您需要权衡这些更新的性能点击与您在检索时看到的性能改进。 – Marvo
还请记住,如果您有许多行中具有相同值的列,则索引可能实际上会使性能变差。 – Poodlehat