一百万数据索引实例測试--mysql
推荐书籍:http://pan.baidu.com/s/1sjJIyRV
任务描写叙述:
如果一高频查询例如以下
SELECT * FROM user WHERE area='amoy' AND sex=0 ORDER BY last_login DESC limit 30;
怎样建立索引?描写叙述考虑的过程
user表例如以下:
初始化100W条数据,当中。area要通过IP查询生成,sex为 0,1 随机
CREATE TABLE user
(
id
int(10) NOT NULL AUTOINCREMENT COMMENT '自增编号',
username
varchar(30) NOT NULL DEFAULT '0' COMMENT '用户名',
password
varchar(30) NOT NULL DEFAULT '0' COMMENT '密码',
area
varchar(30) NOT NULL COMMENT '地址',
sex
int(10) NOT NULL COMMENT '性别0,男;1,女。
',
last_login
int(10) NOT NULL COMMENT '近期一次登录时间戳',
PRIMARY KEY (id
)
) ENGINE=InnoDB AUTOINCREMENT=892013 DEFAULT CHARSET=latin1
终于我的索引
(last_login,area)
数据例如以下:http://pan.baidu.com/s/1eQy0eQI
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGFtcF93YXRlcg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
索引原则:
1.where和order by等的字段建立索引
2.使用唯一索引:对于last_login,area等字段反复的次数比較少,能够使用索引;而sex无非就两个值:性别1。男;2。不值得索引
3.多列索引:不要为每个列单独建立索引。这样并不能将mysql索引的效率最大化。使用“索引合并策略”
4.选择合理的索引列顺序:索引列的顺序意味着索引首先依照最左列进行排序。然后是第二列,以此类推。如(lastlogin,area)会先依照 lastlogin 进行排序。然后才是area。
5.将选择性最高的索引放到前面。也就是会所依照这个条件搜索到的数据最少,选择性就越高。比方选择性:last_login> area> sex。
6.索引不是越多越好。适合的索引能够提高查询效率。可是会减少写入效率。依据项目保持两者的平衡性最好了。
总结上面,首先sex不适合建立索引,有没有索引对于效率的提升意义不大,其次索引会依照最左列进行排序,因此将last_login放到最前面
測试过程:
user表
没有不论什么索引的查询相关日志:
SELECT * FROM user WHERE area='美国ATT用户' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.57s
SELECT * FROM user WHERE area='泰国' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.56s
SELECT * FROM user WHERE area='台湾省台湾大宽频' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.55s
SELECT * FROM user WHERE area='美国弗吉尼亚州' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.59s
SELECT * FROM user WHERE area='德国奔驰汽车' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.55s
SELECT * FROM user WHERE area='台湾省中华电信' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.55s
SELECT * FROM user WHERE area='韩国' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.57s
SELECT * FROM user WHERE area='拉美地区' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.58s
SELECT * FROM user WHERE area='美国纽约(Prudential)' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.57s
SELECT * FROM user WHERE area='印度尼西亚' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.57s
共花费时间:5.66s
建立索引area:
ALTER TABLE user
ADD INDEX index_area
(area
)
;
SELECT * FROM user WHERE area='美国ATT用户' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.06s
SELECT * FROM user WHERE area='泰国' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.02s
SELECT * FROM user WHERE area='台湾省台湾大宽频' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.00s
SELECT * FROM user WHERE area='美国弗吉尼亚州' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.10s
SELECT * FROM user WHERE area='德国奔驰汽车' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.04s
SELECT * FROM user WHERE area='台湾省中华电信' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.02s
SELECT * FROM user WHERE area='韩国' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.11s
SELECT * FROM user WHERE area='拉美地区' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.20s
SELECT * FROM user WHERE area='美国纽约(Prudential)' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.07s
SELECT * FROM user WHERE area='印度尼西亚' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.04s
共花费时间:0.66s
可见。建立area以后对性能的影响是巨大的(5.66/0.66 约为8.5758倍)
删除索引:ALTER TABLE user
DROP INDEX index_area
;
删除area索引发现时间又变成了0.57s
建立lastlogin索引:
SELECT * FROM user WHERE area='美国ATT用户' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.03s
SELECT * FROM user WHERE area='泰国' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.09s
SELECT * FROM user WHERE area='台湾省台湾大宽频' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.51s
SELECT * FROM user WHERE area='美国弗吉尼亚州' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.01s
SELECT * FROM user WHERE area='德国奔驰汽车' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.04s
SELECT * FROM user WHERE area='台湾省中华电信' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.07s
SELECT * FROM user WHERE area='韩国' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.01s
SELECT * FROM user WHERE area='拉美地区' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.01s
SELECT * FROM user WHERE area='美国纽约(Prudential)' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.04s
SELECT * FROM user WHERE area='印度尼西亚' AND sex=0 ORDER BY last_login DESC limit 30; 0.06s
共花费时间:0.87s
相同可以提升性能(5.66/0.87 约为6.5057倍)
建立sex索引:
ALTER TABLE user
ADD INDEX index_sex
(sex
)
;
SELECT * FROM user WHERE area='美国ATT用户' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.87s
SELECT * FROM user WHERE area='泰国' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.87s
SELECT * FROM user WHERE area='台湾省台湾大宽频' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.87s
SELECT * FROM user WHERE area='美国弗吉尼亚州' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.89s
SELECT * FROM user WHERE area='德国奔驰汽车' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.88s
SELECT * FROM user WHERE area='台湾省中华电信' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.87s
SELECT * FROM user WHERE area='韩国' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.86s
SELECT * FROM user WHERE area='拉美地区' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.88s
SELECT * FROM user WHERE area='美国纽约(Prudential)' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.87s
SELECT * FROM user WHERE area='印度尼西亚' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.87s
共花费时间:8.73s
相同可以提升性能(5.66s/8.73 约为0.6483倍)效率反而减少了??求解?
建立这个sex索引还不如不建。
删除索引:
ALTER TABLE user
DROP INDEX index_sex
;
发现时间又变成了0.57s左右,
建立两个单独的索引:
ALTER TABLE user
ADD INDEX index_area
(area
)
,
ADD INDEX index_last_login
(last_login
)
;
SELECT * FROM user WHERE area='美国ATT用户' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.09s
SELECT * FROM user WHERE area='泰国' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.33s
SELECT * FROM user WHERE area='台湾省台湾大宽频' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.21s
SELECT * FROM user WHERE area='美国弗吉尼亚州' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.01s
SELECT * FROM user WHERE area='德国奔驰汽车' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.28s
SELECT * FROM user WHERE area='台湾省中华电信' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.02s
SELECT * FROM user WHERE area='韩国' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.02s
SELECT * FROM user WHERE area='拉美地区' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.02s
SELECT * FROM user WHERE area='美国纽约(Prudential)' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.03s
SELECT * FROM user WHERE area='印度尼西亚' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.67s
发现建立两个单独的索引还不如仅仅建立一个索引
删除索引:
发现时间又变成了0.57s左右。
建立一个的联合索引:
ALTER TABLE user
ADD INDEX index_last_login_area
(last_login
,area
)
,
SELECT * FROM user WHERE area='美国ATT用户' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.00s
SELECT * FROM user WHERE area='泰国' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.00s
SELECT * FROM user WHERE area='台湾省台湾大宽频' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.00s
SELECT * FROM user WHERE area='美国弗吉尼亚州' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.00s
SELECT * FROM user WHERE area='德国奔驰汽车' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.00s
SELECT * FROM user WHERE area='台湾省中华电信' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.00s
SELECT * FROM user WHERE area='韩国' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.00s
SELECT * FROM user WHERE area='拉美地区' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.00s
SELECT * FROM user WHERE area='美国纽约(Prudential)' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.00s
SELECT * FROM user WHERE area='印度尼西亚' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.00s
额。第二条数据这是怎么了,我測试了5次都在这附近晃悠哈!
这尼玛。找对索引啦。就该这么建立,查询不出来须要的时间啦!预计就是我们须要的索引啦!
!
!!
删除索引:
发现时间又变成了0.57s左右,
建立一个的联合索引:
ALTER TABLE user
ADD INDEX index_sex_last_login_area
(sex
,last_login
,area
)
SELECT * FROM user WHERE area='美国ATT用户' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.18s
SELECT * FROM user WHERE area='泰国' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.17s
SELECT * FROM user WHERE area='台湾省台湾大宽频' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.81s
SELECT * FROM user WHERE area='美国弗吉尼亚州' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.01s
SELECT * FROM user WHERE area='德国奔驰汽车' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.02s
SELECT * FROM user WHERE area='台湾省中华电信' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.04s
SELECT * FROM user WHERE area='韩国' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.01s
SELECT * FROM user WHERE area='拉美地区' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.01s
SELECT * FROM user WHERE area='美国纽约(Prudential)' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.03s
SELECT * FROM user WHERE area='印度尼西亚' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.04s
sex怎么总是你在拖后腿啊!
把你调整到索引的最后一个吧。
删除索引:
发现时间又变成了0.57s左右,
建立一个的联合索引:
ALTER TABLE user
ADD INDEX index_last_login_area_sex
(area
,last_login
,sex
)
SELECT * FROM user WHERE area='美国ATT用户' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.03s
SELECT * FROM user WHERE area='泰国' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.07s
SELECT * FROM user WHERE area='台湾省台湾大宽频' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.50s
SELECT * FROM user WHERE area='美国弗吉尼亚州' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.02s
SELECT * FROM user WHERE area='德国奔驰汽车' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.05s
SELECT * FROM user WHERE area='台湾省中华电信' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.06s
SELECT * FROM user WHERE area='韩国' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.02s
SELECT * FROM user WHERE area='拉美地区' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.02s
SELECT * FROM user WHERE area='美国纽约(Prudential)' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.04s
SELECT * FROM user WHERE area='印度尼西亚' AND sex=0 ORDER BY lastlogin DESC limit 30; 0.06s
综上所述:1.建立索引不一定可以加快查询效率如sex这样的给反复次数特别多的列添加索引如sex这样的会减少查询效率,详细的原因有待查找
2.给反复次数比較少的列添加索引还是可以大幅度提高效率
3.给where和orderby之后的字段加入索引才会加快查询效率
4.为每个列单独建立索引,不能将索引的效率最大化,应该使用索引合并策略,即依据查询条件。建立联合索引
5.联合索引的顺序问题:将选择性高的索引放到前面
6.依据资料建立索引意味着索引依照最左列进行排序,然后事第二列。以此类推。如(lastlogin ,area)就会依照lastlogin进行排序,然后才是area
7.依据这次的这个查询条件来说最好的索引是:ALTER TABLE user
ADD INDEX index_last_login_area
(last_login
,area
)。
在公司能有个机会。查看资料和实践索引真的非常不错哈!
推荐书籍:高性能mysql(第三版)
PDF版本号的:http://pan.baidu.com/s/1sjJIyRV
一百万数据索引实例測试--mysql的更多相关文章
- GDAL1.11版本号对SHP文件索引加速測试
GDAL库中对于矢量数据的读取中能够设置一些过滤器来对矢量图形进行筛选.对于Shapefile格式来说.假设数据量太大,设置这个过滤器时间慢的简直无法忍受.好在GDAL1.10版本号開始支持读取Sha ...
- Coreseek:第二步建索引及測试
1,建索引非常easy.一行代码 g:/service/coreseek/bin/indexer -c g:/service/coreseek/etc/csft_mysql.conf person ...
- Bandwidth内存带宽測试工具
本博文为原创,遵循CC3.0协议,转载请注明出处:http://blog.csdn.net/lux_veritas/article/details/24766015 ----------------- ...
- 菜鸟学Java(二十一)——怎样更好的进行单元測试——JUnit
測试在软件生命周期中的重要性,不用我多说想必大家也都很清楚.软件測试有许多分类,从測试的方法上可分为:黑盒測试.白盒測试.静态測试.动态測试等:从软件开发的过程分为:单元測试.集成測试.确认測试.验收 ...
- iOS自己主动化測试的那些干货
前言 假设有測试大佬发现内容不正确.欢迎指正,我会及时改动. 大多数的iOS App(没有持续集成)迭代流程是这种 也就是说.測试是公布之前的最后一道关卡.假设bug不能在測试中发现,那么bug 就会 ...
- TestNg的工厂測试引用@DataProvider数据源----灵活使用工厂測试
之前说过@Factory更适合于同一类型的參数变化性的測试,那么假设參数值没有特定的规律时,我们能够採用@Factory和@DataProvider相结合的方式进行測试 注意要点:请注意測试方法将被一 ...
- mysql选择联合索引还是单索引?索引列应该使用哪一个最有效?深入測试探讨
先建表 CREATE TABLE `menu_employee` ( `Id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增主键,无实际意义', `emplo ...
- 进行mysql压力測试须要注意的几点
1.填充測试数据比物理内存还要大,至少超过innodb_buffer_pool_size 值,不能将数据所有装载到内存中,除非你的本意就想測试全内存状态下的MySQL性能. 2.每轮測试完毕后,都重新 ...
- 【Mysql】数据库索引,百万数据测试索引效果
Mysql官方对索引的定义是:索引(index)是帮助Mysql高效获取数据的数据结构.进而,我们可以知道索引的本质是数据结构. 一.索引的分类 主键索引:也就是我们常见的 PRIMARY KEY,只 ...
随机推荐
- 2007LA 3902 网络(树+贪心)
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=283&am ...
- hdu1114
完全背包的水题,不过今天才学动态规划,就这样啦……hahahah!!! 完全背包跟普通背包的区别是普通背包从后往前循环,以防止被替换 完全背包是从前往后循环,后面的状态会跟着之前状态的改变而改变…… ...
- ResourceManager架构解析
RM作为master管理着所有的集群资源,它会和NM和特定application的AM共同工作 1. NodeManagers NM从RM中获得指令,并管理着单节点上可用资源 2. Applicati ...
- Android应用开发学习笔记之播放视频
作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 在Android中,我们可以通过使用VideoView类或者MediaPlayer类来播放视频. 一.使用Video ...
- UVA11324-- The Largest Clique(SCC+DP)
题目链接 题意:给出一张有向图,求一个结点数最大的结点集,使得该结点集中随意两个结点u和v满足:要么u能够到到v,要么v能够到达u(u和v能够互相到达) 思路:我们能够缩点,用Tarjan求出全部强连 ...
- 窗口显示于parent控件上(用到了ManualDock函数)
procedure TForm1.btn1Click(Sender: TObject); begin with TForm2.Create(self) do begin ManualDock(self ...
- best javascript framework list -- 最好的js框架
Javascript Framework List | Top Javascript Framework List | Best Javascript Framework List http://co ...
- 【菜鸟看框架】——EF怎样自己主动生成实体
引言 在上一篇博客中给大家介绍了一些关于EF框架的基本知识.让大家对实体架构算是有了一个入门的认识,当然知识 这一篇博客是不能非常清楚的理解实体架构的内涵的.我们须要在实践中自己去不断的研究和探索当中 ...
- Centos 7 学习之静态IP设置
原文链接:http://blog.csdn.net/johnnycode/article/details/40624403 本学习主要针对 Centos 7.0.1406 版本进行学习整理! 如果你使 ...
- 重拾linux
重拾linux 起因 因为想重拾起linux,同时需要用docker起几个镜像,用来学习网络知识.本来想直接去阿里云上买,后来一想自己机器上,起一个linux是个不错的选择,毕竟不花钱! 还可以用来做 ...