Home > Database > Mysql Tutorial > mysql联合索引的应用_MySQL

mysql联合索引的应用_MySQL

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-01 13:30:16
Original
1112 people have browsed it

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;
Copy after login

记录共有11万条

1、

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

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

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

优化是非常明显的

但是如果

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

2、

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

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

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

 

 

这个时间变为0.143秒

 

优化也是非常明显

 

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

bitsCN.com
Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
MySQL stops process
From 1970-01-01 08:00:00
0
0
0
Error when installing mysql on linux
From 1970-01-01 08:00:00
0
0
0
phpstudy cannot start mysql?
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template