Heim >Datenbank >MySQL-Tutorial >mysql联合索引的应用_MySQL

mysql联合索引的应用_MySQL

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-01 13:30:161104Durchsuche

bitsCN.com

mysql联合索引的应用

 

有一个log表,结构是这样的:

 

CREATE TABLE `weblog` (  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,  `ip` varchar(45) NOT NULL,  `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,  `kind` varchar(255) NOT NULL,  PRIMARY KEY (`id`)) ENGINE=MyISAM AUTO_INCREMENT=48024 DEFAULT CHARSET=utf8;

 

 

记录共有11万条

 

1、

select count(times1) as times from (SELECT count(id) as times1 FROM weblog group by ip,kind) as a

 

 

当执行这个查询时,需耗时1.9秒,有些受不了,后来发现这样这样添加个一个联合索引

 

alter table weblog add INDEX `sindex` (`ip`,`kind`)
后,,该查询就会减少到0.182秒

 

优化是非常明显的

 

但是如果

alter table weblog add INDEX `sindex` (`ip`,`kind`,'id')
后,查询反倒会变慢

 

2、

select count(times1) as times from (SELECT count(id) as times1 FROM weblog group by kind) as a

 

 

当未进行联合查询优化时,需要时间我1.8025秒

 

alter table weblog add INDEX `sindex` (`kind`,`id`)进行联合索引后

 

 

这个时间变为0.143秒

 

优化也是非常明显

 

如果有类似的group by的 如果进行联合索引,合适的话,应该能提高很高的效能

bitsCN.com
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn