100W数据,测试复合索引
复合索引不是那么容易被catch到的。
两个查询条件都是等于的时候,才会被catch到。
mysql> select count(*) from tf_user_index where sex = 2 and score > 80;
+----------+
| count(*) |
+----------+
|  1261904 |
+----------+
1 row in set (10.65 sec)
mysql> select count(*) from tf_user where sex = 2 and score > 80;
+----------+
| count(*) |
+----------+
|  1261904 |
+----------+
1 row in set (4.38 sec)
mysql> explain select count(*) from tf_user_index where sex = 2 and score > 80;
+----+-------------+---------------+------+---------------+------+---------+-------+---------+-------------+
| id | select_type | table         | type | possible_keys | key  | key_len | ref   | rows    | Extra       |
+----+-------------+---------------+------+---------------+------+---------+-------+---------+-------------+
|  1 | SIMPLE      | tf_user_index | ref  | score,sex     | sex  | 1       | const | 4481227 | Using where |
+----+-------------+---------------+------+---------------+------+---------+-------+---------+-------------+
1 row in set (0.08 sec)
查询条件中,如果有大于号。那么优先抓取等于号对应的索引,也就是sex对应的索引。经过索引的一番折腾,查询时间反而更长了。
即便是把score放到前面,一样的效果。
mysql> explain select count(*) from tf_user_index where score> 80 and sex = 2;
+----+-------------+---------------+------+---------------+------+---------+-------+---------+-------------+
| id | select_type | table         | type | possible_keys | key  | key_len | ref   | rows    | Extra       |
+----+-------------+---------------+------+---------------+------+---------+-------+---------+-------------+
|  1 | SIMPLE      | tf_user_index | ref  | score,sex     | sex  | 1       | const | 4481227 | Using where |
+----+-------------+---------------+------+---------------+------+---------+-------+---------+-------------+
两个条件都为等于的时候,索引的效果就有点明显了。
mysql> select count(*) from tf_user_index where sex = 2 and score = 80;
+----------+
| count(*) |
+----------+
|    63230 |
+----------+
1 row in set (1.09 sec)
mysql> select count(*) from tf_user where sex = 2 and score = 80;
+----------+
| count(*) |
+----------+
|    63230 |
+----------+
1 row in set (2.61 sec)
mysql> explain select count(*) from tf_user_index where sex = 2 and score = 80;
+----+-------------+---------------+-------------+---------------+-----------+---------+------+--------+------------------------------------------------------+
| id | select_type | table         | type        | possible_keys | key       | key_len | ref  | rows   | Extra                                                |
+----+-------------+---------------+-------------+---------------+-----------+---------+------+--------+------------------------------------------------------+
|  1 | SIMPLE      | tf_user_index | index_merge | score,sex     | score,sex | 4,1     | NULL | 124004 | Using intersect(score,sex); Using where; Using index |
+----+-------------+---------------+-------------+---------------+-----------+---------+------+--------+------------------------------------------------------+
1 row in set (0.00 sec)
这个时候,并没有添加复合索引。
加了复合索引,如果查询条件是大于号,一样catch不到。
mysql> explain select count(*) from tf_user_index where sex = 2 and score > 80;
+----+-------------+---------------+------+---------------------+------+---------+-------+---------+-------------+
| id | select_type | table         | type | possible_keys       | key  | key_len | ref   | rows    | Extra       |
+----+-------------+---------------+------+---------------------+------+---------+-------+---------+-------------+
|  1 | SIMPLE      | tf_user_index | ref  | score,sex,score_sex | sex  | 1       | const | 4481227 | Using where |
+----+-------------+---------------+------+---------------------+------+---------+-------+---------+-------------+
1 row in set (0.01 sec)
mysql> select count(*) from tf_user_index where score > 80 and sex =2;
+----------+
| count(*) |
+----------+
|  1261904 |
+----------+
1 row in set (15.32 sec)
竟然执行了15秒之久。
这条sql语句可以优化一下,将sex也改为大于号。
mysql> select count(*) from tf_user_index where score > 80 and sex >1;
+----------+
| count(*) |
+----------+
|  1261904 |
+----------+
1 row in set (0.66 sec)
mysql> explain select count(*) from tf_user_index where score > 80 and sex >1;
+----+-------------+---------------+-------+---------------------+-----------+---------+------+---------+--------------------------+
| id | select_type | table         | type  | possible_keys       | key       | key_len | ref  | rows    | Extra                    |
+----+-------------+---------------+-------+---------------------+-----------+---------+------+---------+--------------------------+
|  1 | SIMPLE      | tf_user_index | range | score,sex,score_sex | score_sex | 4       | NULL | 4481227 | Using where; Using index |
+----+-------------+---------------+-------+---------------------+-----------+---------+------+---------+--------------------------+
1 row in set (0.00 sec)
这样就捕捉到了索引。
mysql> select count(*) from tf_user_index where score = 80 and sex = 1;
+----------+
| count(*) |
+----------+
|    62866 |
+----------+
1 row in set (0.01 sec)
mysql> explain select count(*) from tf_user_index where score = 80 and sex = 1;
+----+-------------+---------------+------+---------------------+-----------+---------+-------------+--------+-------------+
| id | select_type | table         | type | possible_keys       | key       | key_len | ref         | rows   | Extra       |
+----+-------------+---------------+------+---------------------+-----------+---------+-------------+--------+-------------+
|  1 | SIMPLE      | tf_user_index | ref  | score,sex,score_sex | score_sex | 5       | const,const | 124794 | Using index |
+----+-------------+---------------+------+---------------------+-----------+---------+-------------+--------+-------------+
1 row in set (0.00 sec)
mysql> select count(*) from tf_user_index where  sex = 1 and score=80;
+----------+
| count(*) |
+----------+
|    62866 |
+----------+
1 row in set (0.01 sec)
mysql> explain select count(*) from tf_user_index where  sex = 1 and score=80;
+----+-------------+---------------+------+---------------------+-----------+---------+-------------+--------+-------------+
| id | select_type | table         | type | possible_keys       | key       | key_len | ref         | rows   | Extra       |
+----+-------------+---------------+------+---------------------+-----------+---------+-------------+--------+-------------+
|  1 | SIMPLE      | tf_user_index | ref  | score,sex,score_sex | score_sex | 5       | const,const | 124794 | Using index |
+----+-------------+---------------+------+---------------------+-----------+---------+-------------+--------+-------------+
1 row in set (0.00 sec)
顺序并不重要。
索引,复合索引,确实可以提供查询的速度。关键是,要能够捕捉到。要能够找寻它们捕捉的规律。理解它们执行的过程。
合理的分析查询的规律,合理的给表添加索引。分析常用的查询,分析常用的查询字段。通过explain字段来进行sql语句的分析,优化sql语句。

实践发现sex_score,score_sex查询的效果是一样的,关键是能否捕捉到。
mysql> explain select count(*) from tf_user_index where  sex = 2 and score>80;
+----+-------------+---------------+-------+---------------------+-----------+---------+------+---------+--------------------------+
| id | select_type | table         | type  | possible_keys       | key       | key_len | ref  | rows    | Extra                    |
+----+-------------+---------------+-------+---------------------+-----------+---------+------+---------+--------------------------+
|  1 | SIMPLE      | tf_user_index | range | score,sex,sex_score | sex_score | 5       | NULL | 2446346 | Using where; Using index |
+----+-------------+---------------+-------+---------------------+-----------+---------+------+---------+--------------------------+
1 row in set (0.00 sec)
mysql> select count(*) from tf_user_index where  sex = 2 and score>80;
+----------+
| count(*) |
+----------+
|  1261904 |
+----------+
1 row in set (0.31 sec)
我屮艸芔茻,sex_score竟然捕捉到了索引。看来顺序还是有所区别的。这个建索引还是多多的实验吧。孰能生巧。
100W数据,测试复合索引的更多相关文章
- 100W数据,测试索引
		两张表,结构相同,数据内容相同.唯一不同的就是是否包含索引.tf_user_index表中包含索引. 这100w数据,我造了近一天时间. mysql> select count(*) from ... 
- SQL Server 执行计划利用统计信息对数据行的预估原理二(为什么复合索引列顺序会影响到执行计划对数据行的预估)
		本文出处:http://www.cnblogs.com/wy123/p/6008477.html 关于统计信息对数据行数做预估,之前写过对非相关列(单独或者单独的索引列)进行预估时候的算法,参考这里. ... 
- 关于SQL查询效率,100w数据,查询只要1秒
		1.关于SQL查询效率,100w数据,查询只要1秒,与您分享:机器情况p4: 2.4内存: 1 Gos: windows 2003数据库: ms sql server 2000目的: 查询性能测试,比 ... 
- SQL Server创建复合索引时,复合索引列顺序对查询的性能影响
		说说复合索引 写索引的博客太多了,一直不想动手写,有一下两个原因:一是觉得有炒剩饭的嫌疑,有兄弟曾说:索引吗,只要在查询条件上建索引就行了,真的可以这么暴力吗?二来觉得,索引是个非常大的话题,很难概括 ... 
- SQL SERVER大话存储结构(4)_复合索引与包含索引
		索引这块从存储结构来分,有2大类,聚集索引和非聚集索引,而非聚集索引在堆表或者在聚集索引表都会对其 键值有所影响,这块可以详细查看本系列第二篇文章:SQL SERVER大话存储结构 ... 
- Mysql limit 优化,百万至千万级快速分页,--复合索引的引用并应用于轻量级框架
		MySql 性能到底能有多高?用了php半年多,真正如此深入的去思考这个问题还是从前天开始.有过痛苦有过绝望,到现在充满信心!MySql 这个数据库绝对是适合dba级的高手去玩的,一般做一点1万篇新闻 ... 
- MySQL复合索引探究
		复合索引(又称为联合索引),是在多个列上创建的索引.创建复合索引最重要的是列顺序的选择,这关系到索引能否使用上,或者影响多少个谓词条件能使用上索引.复合索引的使用遵循最左匹配原则,只有索引左边的列匹配 ... 
- Sql Server之旅——第九站 看公司这些DBA们设计的这些复合索引
		这一篇再说下索引的最后一个主题,索引覆盖,当然学习比较好的捷径是看看那些大师们设计的索引,看从中能提取些什么营养的东西,下面我们看 看数据库中一个核心的Orders表. 一:查看表的架构 <1& ... 
- Sql Server之旅——第八站 复合索引和include索引到底有多大区别?
		周末终于搬进出租房了,装了宽带....才发现没网的日子...那是一个怎样的与世隔绝呀...再也受不了那样的日子了....好了,既然网 安上去了,还得继续我的这个系列. 索引和锁,这两个主题对我们开发工 ... 
随机推荐
- 微信小程序 --- 选择图片和拍照
			wx.chooseImage 选择图片 / 进行拍照 //获取应用实例 const app = getApp() Page({ data: { onOff:true }, btnclick:funct ... 
- 【巷子】---vue项目打包---基本使用---【vue】
			一.基本设置 二.打包 npm run build 三.github创建一个仓库 1.将文件上传至仓库 2.点击仓库设置 3.选择代码上传到master 4.点击地址即可预览 5.常用技巧 a.如果在 ... 
- MVC学习之HtmlHelper
			1.为什么要使用HtmlHelper? 1.首先HtmlHelper是一个类型,MVC中的ViewPage<TModel>中的一个属性Html属性,这个属性的类型就是HtmlHelper& ... 
- Python开发【Tornado】:异步Web服务(一)
			异步Web服务 前言: 到目前为止,我们已经看到了许多使Tornado成为一个Web应用强有力框架的功能.它的简单性.易用性和便捷性使其有足够的理由成为许多Web项目的不错的选择.然而,Tornado ... 
- JDK安装(linux系统)
			安装好centos6.8以后,输入命令: 如果显示如下: 证明系统自带了JDK,需要手动删除. 然后搜索: 备注: 查询一个包是否被安装 # rpm -q < rpm package name& ... 
- LeetCode——Palindrome Number
			Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ... 
- Spark Shuffle(二)Executor、Driver之间Shuffle结果消息传递、追踪(转载)
			1. 前言 在博客里介绍了ShuffleWrite关于shuffleMapTask如何运行,输出Shuffle结果到Shuffle_shuffleId_mapId_0.data数据文件中,每个exec ... 
- 三、Mosquitto Java 客户端实现
			本文的实现是在 << 一.Mosquitto 介绍&安装>> << 二. Mosquitto 的使用说明 >> 两篇文章搭建好 Mosquitt ... 
- 跟我学Makefile(三)
			紧接着跟我学Makefile(二)继续学习:变量高级用法 (1)变量值的替换 :替换变量中的共有的部分,其格式是“$(var:a=b)”或是“${var:a=b}”,把变量“var”中所有以“a”字串 ... 
- curl基本使用
			curl简介 linux curl是一个利用URL规则在命令行下工作的文件传输工具.它支持文件的上传和下载. curl可以使用URL的语法模拟浏览器来传输数据,因为它是模拟浏览器,因此它同样支持多种协 ... 
