现象

新建了一张员工表,插入了少量数据,索引中所有的字段均在where条件出现时,正确走到了idx_nap索引,但是where出现部分自左开始的索引时,却进行全表扫描,与MySQL官方所说的最左匹配原则“相悖”。

数据背景

CREATE TABLE `staffs` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(24) NOT NULL DEFAULT '' COMMENT '姓名',
`age` int(11) NOT NULL DEFAULT '0' COMMENT '年龄',
`pos` varchar(20) NOT NULL DEFAULT '' COMMENT '职位',
`add_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '入职时间',
PRIMARY KEY (`id`),
KEY `idx_nap` (`name`,`age`,`pos`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 COMMENT='员工记录表'; 表中数据如下:
id name age pos add_time
1 July 23 dev 2018-06-04 16:02:02
2 Clive 22 dev 2018-06-04 16:02:32
3 Cleva 24 test 2018-06-04 16:02:38
4 July 23 test 2018-06-04 16:12:22
5 July 23 pre 2018-06-04 16:12:37
6 Clive 22 pre 2018-06-04 16:12:48
7 July 25 dev 2018-06-04 16:30:17

Explain语句看下执行计划

-- 全匹配走了索引
explain select * from staffs where name = 'July' and age = 23 and pos = 'dev';
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE staffs NULL ref idx_nap idx_nap 140 const,const,const 1 100.00 NULL

开启优化器跟踪优化过程

-- 左侧部分匹配却没有走索引,全表扫描
explain select * from staffs where name = 'July' and age = 23;
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE staffs2 NULL ALL idx_nap NULL NULL NULL 6 50.00 Using where
-- 开启优化器跟踪
set session optimizer_trace='enabled=on';
-- 在执行完查询语句后,在执行以下的select语句可以查看具体的优化器执行过程
select * from information_schema.optimizer_trace;

Trace部分的内容

{
"steps": [
{
"join_preparation": {
"select#": 1,
"steps": [
{
"expanded_query": "/* select#1 */ select `staffs`.`id` AS `id`,`staffs`.`name` AS `name`,`staffs`.`age` AS `age`,`staffs`.`pos` AS `pos`,`staffs`.`add_time` AS `add_time` from `staffs` where ((`staffs`.`name` = 'July') and (`staffs`.`age` = 23))"
}
]
}
},
{
"join_optimization": {
"select#": 1,
"steps": [
{
"condition_processing": {
"condition": "WHERE",
"original_condition": "((`staffs`.`name` = 'July') and (`staffs`.`age` = 23))",
"steps": [
{
"transformation": "equality_propagation",
"resulting_condition": "((`staffs`.`name` = 'July') and multiple equal(23, `staffs`.`age`))"
},
{
"transformation": "constant_propagation",
"resulting_condition": "((`staffs`.`name` = 'July') and multiple equal(23, `staffs`.`age`))"
},
{
"transformation": "trivial_condition_removal",
"resulting_condition": "((`staffs`.`name` = 'July') and multiple equal(23, `staffs`.`age`))"
}
]
}
},
{
"substitute_generated_columns": {
}
},
{
"table_dependencies": [
{
"table": "`staffs`",
"row_may_be_null": false,
"map_bit": 0,
"depends_on_map_bits": [
]
}
]
},
{
"ref_optimizer_key_uses": [
{
"table": "`staffs`",
"field": "name",
"equals": "'July'",
"null_rejecting": false
},
{
"table": "`staffs`",
"field": "age",
"equals": "23",
"null_rejecting": false
}
]
},
{
"rows_estimation": [
{
"table": "`staffs`",
"range_analysis": {
"table_scan": {
"rows": 6,
"cost": 4.3
},
"potential_range_indexes": [
{
"index": "PRIMARY",
"usable": false,
"cause": "not_applicable"
},
{
"index": "idx_nap",
"usable": true,
"key_parts": [
"name",
"age",
"pos",
"id"
]
}
],
"setup_range_conditions": [
],
"group_index_range": {
"chosen": false,
"cause": "not_group_by_or_distinct"
},
"analyzing_range_alternatives": {
"range_scan_alternatives": [
{
"index": "idx_nap",
"ranges": [
"July <= name <= July AND 23 <= age <= 23"
],
"index_dives_for_eq_ranges": true,
"rowid_ordered": false,
"using_mrr": false,
"index_only": false,
"rows": 3,
"cost": 4.61,
"chosen": false,
"cause": "cost"
}
],
"analyzing_roworder_intersect": {
"usable": false,
"cause": "too_few_roworder_scans"
}
}
}
}
]
},
{
"considered_execution_plans": [
{
"plan_prefix": [
],
"table": "`staffs`",
"best_access_path": {
"considered_access_paths": [
{
//可以看到这边MySQL计算得到使用索引的成本为2.6
"access_type": "ref",
"index": "idx_nap",
"rows": 3,
"cost": 2.6,
"chosen": true
},
{
//而全表扫描计算所得的成本为2.2
"rows_to_scan": 6,
"access_type": "scan",
"resulting_rows": 6,
"cost": 2.2,
"chosen": true
}
]
},
//因此选择了成本更低的scan
"condition_filtering_pct": 100,
"rows_for_plan": 6,
"cost_for_plan": 2.2,
"chosen": true
}
]
},
{
"attaching_conditions_to_tables": {
"original_condition": "((`staffs`.`age` = 23) and (`staffs`.`name` = 'July'))",
"attached_conditions_computation": [
],
"attached_conditions_summary": [
{
"table": "`staffs`",
"attached": "((`staffs`.`age` = 23) and (`staffs`.`name` = 'July'))"
}
]
}
},
{
"refine_plan": [
{
"table": "`staffs`"
}
]
}
]
}
},
{
"join_execution": {
"select#": 1,
"steps": [
]
}
}
]
}

增加表数据量

-- 接下来增大表的数据量
INSERT INTO `staffs` (`name`, `age`, `pos`, `add_time`)
VALUES
('July', 25, 'dev', '2018-06-04 16:30:17'),
('July', 23, 'dev1', '2018-06-04 16:02:02'),
('July', 23, 'dev2', '2018-06-04 16:02:02'),
('July', 23, 'dev3', '2018-06-04 16:02:02'),
('July', 23, 'dev4', '2018-06-04 16:02:02'),
('July', 23, 'dev6', '2018-06-04 16:02:02'),
('July', 23, 'dev5', '2018-06-04 16:02:02'),
('July', 23, 'dev7', '2018-06-04 16:02:02'),
('July', 23, 'dev8', '2018-06-04 16:02:02'),
('July', 23, 'dev9', '2018-06-04 16:02:02'),
('July', 23, 'dev10', '2018-06-04 16:02:02'),
('Clive', 23, 'dev1', '2018-06-04 16:02:02'),
('Clive', 23, 'dev2', '2018-06-04 16:02:02'),
('Clive', 23, 'dev3', '2018-06-04 16:02:02'),
('Clive', 23, 'dev4', '2018-06-04 16:02:02'),
('Clive', 23, 'dev6', '2018-06-04 16:02:02'),
('Clive', 23, 'dev5', '2018-06-04 16:02:02'),
('Clive', 23, 'dev7', '2018-06-04 16:02:02'),
('Clive', 23, 'dev8', '2018-06-04 16:02:02'),
('Clive', 23, 'dev9', '2018-06-04 16:02:02'),
('Clive', 23, 'dev10', '2018-06-04 16:02:02');

执行Explain

-- 再次执行同样的查询语句,会发现走到索引上了
explain select * from staffs where name = 'July' and age = 23;
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE staffs NULL ref idx_nap idx_nap 78 const,const 13 100.00 NULL

查看新的Trace内容

-- 再看下优化器执行过程
{
"steps": [
{
"join_preparation": {
"select#": 1,
"steps": [
{
"expanded_query": "/* select#1 */ select `staffs`.`id` AS `id`,`staffs`.`name` AS `name`,`staffs`.`age` AS `age`,`staffs`.`pos` AS `pos`,`staffs`.`add_time` AS `add_time` from `staffs` where ((`staffs`.`name` = 'July') and (`staffs`.`age` = 23))"
}
]
}
},
{
"join_optimization": {
"select#": 1,
"steps": [
{
"condition_processing": {
"condition": "WHERE",
"original_condition": "((`staffs`.`name` = 'July') and (`staffs`.`age` = 23))",
"steps": [
{
"transformation": "equality_propagation",
"resulting_condition": "((`staffs`.`name` = 'July') and multiple equal(23, `staffs`.`age`))"
},
{
"transformation": "constant_propagation",
"resulting_condition": "((`staffs`.`name` = 'July') and multiple equal(23, `staffs`.`age`))"
},
{
"transformation": "trivial_condition_removal",
"resulting_condition": "((`staffs`.`name` = 'July') and multiple equal(23, `staffs`.`age`))"
}
]
}
},
{
"substitute_generated_columns": {
}
},
{
"table_dependencies": [
{
"table": "`staffs`",
"row_may_be_null": false,
"map_bit": 0,
"depends_on_map_bits": [
]
}
]
},
{
"ref_optimizer_key_uses": [
{
"table": "`staffs`",
"field": "name",
"equals": "'July'",
"null_rejecting": false
},
{
"table": "`staffs`",
"field": "age",
"equals": "23",
"null_rejecting": false
}
]
},
{
"rows_estimation": [
{
"table": "`staffs`",
"range_analysis": {
"table_scan": {
"rows": 27,
"cost": 8.5
},
"potential_range_indexes": [
{
"index": "PRIMARY",
"usable": false,
"cause": "not_applicable"
},
{
"index": "idx_nap",
"usable": true,
"key_parts": [
"name",
"age",
"pos",
"id"
]
}
],
"setup_range_conditions": [
],
"group_index_range": {
"chosen": false,
"cause": "not_group_by_or_distinct"
},
"analyzing_range_alternatives": {
"range_scan_alternatives": [
{
"index": "idx_nap",
"ranges": [
"July <= name <= July AND 23 <= age <= 23"
],
"index_dives_for_eq_ranges": true,
"rowid_ordered": false,
"using_mrr": false,
"index_only": false,
"rows": 13,
"cost": 16.61,
"chosen": false,
"cause": "cost"
}
],
"analyzing_roworder_intersect": {
"usable": false,
"cause": "too_few_roworder_scans"
}
}
}
}
]
},
{
"considered_execution_plans": [
{
"plan_prefix": [
],
"table": "`staffs`",
"best_access_path": {
"considered_access_paths": [
{
//使用索引的成本变为了5.3
"access_type": "ref",
"index": "idx_nap",
"rows": 13,
"cost": 5.3,
"chosen": true
},
{
//scan的成本变为了6.4
"rows_to_scan": 27,
"access_type": "scan",
"resulting_rows": 27,
"cost": 6.4,
"chosen": false
}
]
},
//使用索引查询的成本更低,因此选择了走索引
"condition_filtering_pct": 100,
"rows_for_plan": 13,
"cost_for_plan": 5.3,
"chosen": true
}
]
},
{
"attaching_conditions_to_tables": {
"original_condition": "((`staffs`.`age` = 23) and (`staffs`.`name` = 'July'))",
"attached_conditions_computation": [
],
"attached_conditions_summary": [
{
"table": "`staffs`",
"attached": null
}
]
}
},
{
"refine_plan": [
{
"table": "`staffs`"
}
]
}
]
}
},
{
"join_execution": {
"select#": 1,
"steps": [
]
}
}
]
}

结论

MySQL表数据量的大小,会影响索引的选择,具体的情况还是通过Explain和Optimizer Trace来查看与分析。

表数据量影响MySQL索引选择的更多相关文章

  1. 单表扫描,MySQL索引选择不正确 并 详细解析OPTIMIZER_TRACE格式

    单表扫描,MySQL索引选择不正确 并 详细解析OPTIMIZER_TRACE格式     一 表结构如下:  万行 CREATE TABLE t_audit_operate_log (  Fid b ...

  2. SOME:收缩数据库日志文件,查看表数据量和空间占用,查看表结构索引修改时间

    ---收缩数据库日志文件 USE [master]ALTER DATABASE yourdatabasename SET RECOVERY SIMPLE WITH NO_WAITALTER DATAB ...

  3. MySQL索引选择及添加原则

    索引选择性就是结果个数与总个数的比值. 用sql语句表示为: SELECT COUNT(*) FROM table_name WHERE column_name/SELECT COUNT(*) FRO ...

  4. sql server编写通用脚本自动统计各表数据量心得

    工作过程中,如果一个数据库的表比较多,手工编写统计脚本就会比较繁琐,于是摸索出自动生成各表统计数据量脚本的通用方法,直接上代码: /* 脚本来源:https://www.cnblogs.com/zha ...

  5. 查询优化百万条数据量的MySQL表

    转自https://www.cnblogs.com/llzhang123/p/9239682.html 1.两种查询引擎查询速度(myIsam 引擎 ) InnoDB 中不保存表的具体行数,也就是说, ...

  6. MySQL单表数据量过千万,采坑优化记录,完美解决方案

    问题概述 使用阿里云rds for MySQL数据库(就是MySQL5.6版本),有个用户上网记录表6个月的数据量近2000万,保留最近一年的数据量达到4000万,查询速度极慢,日常卡死.严重影响业务 ...

  7. 单表扫描,MySQL索引选择不正确 并 详细解析OPTIMIZER_TRACE格式

    一 表结构如下:  万行 CREATE TABLE t_audit_operate_log (  Fid bigint(16) AUTO_INCREMENT,  Fcreate_time int(10 ...

  8. MySQL索引选择不正确并详细解析OPTIMIZER_TRACE格式

    一 表结构如下: CREATE TABLE t_audit_operate_log (  Fid bigint(16) AUTO_INCREMENT,  Fcreate_time int(10) un ...

  9. 大数据量下MySQL插入方法的性能比较

    不管是日常业务数据处理中,还是数据库的导入导出,都可能遇到需要处理大量数据的插入.插入的方式和数据库引擎都会对插入速度造成影响,这篇文章旨在从理论和实践上对各种方法进行分析和比较,方便以后应用中插入方 ...

随机推荐

  1. VS工具栏没有出现可用工具的情况

    (1)没有切换到资源视图,打开具体的对话框. (2)如果你在调试状态,即使打开了具体的对话框,VS工具箱还是不会出现可用的控件的.所以不要在调试状态下添加控件.

  2. MFC TabCtrl 控件修改标签尺寸

    注意:无论那种方法,都要先设置Tab控件的Style属性为fixed width. 第一种方法 MFC,tabcontrol控件改变标签大小 - CSDN博客 https://blog.csdn.ne ...

  3. cant found Microsoft.VSSDK.BuildTools.15.0.26201

    如果在vs扩展开发中出现 严重性 代码 说明 项目 文件 行 禁止显示状态 错误 Failed to load 'C:\程序\EncodingNormalior\packages\Microsoft. ...

  4. navicat连接linux系统中mysql-错误:10038

    输入 命令 netstat -anp(查看所有的进程和端口使用情况) (注:Local Address一列中: 0.0.0.0 表示监听本地所有ip地址,其他电脑是可以访问的,并且修改ip不受影响. ...

  5. Javascript多线程

    最近项目中要用一个倒计时,但是当弹窗的时候倒计时会被阻塞,所以我想到使用Javascript多线程解决该问题. 虽然JavaScript是单线程的,但是通过worker可以让Javascript另外开 ...

  6. SSH框架搭建过程详解

    Spring.Struts2.Hibernate框架: 具体三大框架的知识以前的文章写过,在这里整合 Spring框架知识:http://www.cnblogs.com/xuyiqing/catego ...

  7. HttpRunner Manager接口自动化测试平台实践(Windows)

    1. 源码下载 github: https://github.com/HttpRunner/HttpRunnerManager 下载后放入项目目录,结构如下: 2.依赖环境  根据根目录require ...

  8. mybatis 详解(一)

    http://www.cnblogs.com/ysocean/p/7271600.html 1.什么是MyBatis? MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目 ...

  9. iOSAPP开发项目搭建

    架构图: 架构原则:易读性.易维护性.易扩展性. 一.思考 做好一件事,花在思考上的时间应该多于执行. 首先根据产品需求和设计图,脑中先建立一个产品架构: 1. 产品的定位是什么. 社交?媒体?游戏? ...

  10. Python——pytessercat识别简单的验证码

    什么是验证码 验证码(CAPTCHA)是“Completely Automated Public Turing test to tell Computersand Humans Apart” (全自动 ...