在工做之中,因为SQL问题致使的数据库故障层出不穷,索引问题是SQL问题中出现频率最高的,常见的索引问题包括:无索引,隐式转换,索引建立不合理。mysql
当数据库中出现访问表的SQL没建立索引致使全表扫描,若是表的数据量很大扫描大量的数据,执行效率过慢,占用数据库链接,链接数堆积很快达到数据库的最大链接数设置,新的应用请求将会被拒绝致使故障发生。sql
隐式转换是指SQL查询条件中的传入值与对应字段的数据定义不一致致使索引没法使用。常见隐式转换如字段的表结构定义为字符类型,但SQL传入值为数字;或者是字段定义collation为区分大小写,在多表关联的场景下,其表的关联字段大小写敏感定义各不相同。隐式转换会致使索引没法使用,进而出现上述慢SQL堆积数据库链接数跑满的状况。数据库
查看表结构。oop
mysql> show create table customers;
CREATE TABLE `customers` (
`cust_id` int(11) NOT NULL AUTO_INCREMENT,
`cust_name` char(50) NOT NULL,
`cust_address` char(50) DEFAULT NULL,
`cust_city` char(50) DEFAULT NULL,
`cust_state` char(5) DEFAULT NULL,
`cust_zip` char(10) DEFAULT NULL,
`cust_country` char(50) DEFAULT NULL,
`cust_contact` char(50) DEFAULT NULL,
`cust_email` char(255) DEFAULT NULL,
PRIMARY KEY (`cust_id`),
) ENGINE=InnoDB AUTO_INCREMENT=10006 DEFAULT CHARSET=utf8
执行语句。性能
mysql> select * from customers where cust_zip = '44444' limit 0,1 \G;
执行计划。优化
mysql> explain select * from customers where cust_zip = '44444' limit 0,1 \G;
id: 1
select_type: SIMPLE
table: customers
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 505560
Extra: Using where
执行计划看到type为ALL,是全表扫描,每次执行须要扫描505560行数据,这是很是消耗性能的,那么下面将介绍优化方式。spa
添加索引。code
mysql> alter table customers add index idx_cus(cust_zip);
执行计划。索引
mysql> explain select * from customers where cust_zip = '44444' limit 0,1 \G;
id: 1
select_type: SIMPLE
table: customers
type: ref
possible_keys: idx_cus
key: idx_cus
key_len: 31
ref: const
rows: 4555
Extra: Using index condition
执行计划看到type为ref,基于索引的等值查询,或者表间等值链接。ip
表结构同上案例相同,执行语句。
mysql> select cust_id,cust_name,cust_zip from customers where cust_zip = '42222'order by cust_zip,cust_name;
执行计划。
mysql> explain select cust_id,cust_name,cust_zip from customers where cust_zip = '42222'order by cust_zip,cust_name\G;
id: 1
select_type: SIMPLE
table: customers
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 505560
Extra: Using filesort
添加索引。
mysql> alter table customers add index idx_cu_zip_name(cust_zip,cust_name);
执行计划。
mysql> explain select cust_id,cust_name,cust_zip from customers where cust_zip = '42222'order by cust_zip,cust_name\G;
id: 1
select_type: SIMPLE
table: customers
type: ref
possible_keys: idx_cu_zip_name
key: idx_cu_zip_name
key_len: 31
ref: const
rows: 4555
Extra: Using where; Using index
order by使用字段,并且字段应该是索引字段。
mysql> explain select * from customers where cust_zip = 44444 limit 0,1 \G;
id: 1
select_type: SIMPLE
table: customers
type: ALL
possible_keys: idx_cus
key: NULL
key_len: NULL
ref: NULL
rows: 505560
Extra: Using where
mysql> show warnings;
Warning: Cannot use range access on index 'idx_cus' due to type or collation conversion on field 'cust_zip'
上述案例中因为表结构定义cust_zip字段是字符串数据类型,而应用传入的是数字,致使了隐式转换,没法使用索引。
查看表结构。
mysql> show create table customers1;
CREATE TABLE `customers1` (
`cust_id` varchar(10) CHARACTER SET latin1 COLLATE latin1_bin DEFAULT NULL,
`cust_name` char(50) NOT NULL,
KEY `idx_cu_id` (`cust_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
mysql> show create table customers2;
CREATE TABLE `customers2` (
`cust_id` varchar(10) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`cust_name` char(50) NOT NULL,
KEY `idx_cu_id` (`cust_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
执行语句。
mysql> select customers1.* from customers2 left join customers1 on customers1.cust_id=customers2.cust_id where customers2.cust_id='x';
执行计划。
mysql> explain select customers1.* from customers2 left join customers1 on customers1.cust_id=customers2.cust_id where customers2.cust_id='x'\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: customers2
type: ref
possible_keys: idx_cu_id
key: idx_cu_id
key_len: 33
ref: const
rows: 1
Extra: Using where; Using index
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: customers1
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 1
Extra: Using where; Using join buffer (Block Nested Loop)
修改COLLATE。
mysql> alter table customers1 modify column cust_id varchar(10) COLLATE utf8_bin ;
执行计划。
mysql> explain select cust_id,cust_name,cust_zip from customers where cust_zip = '42222'order by cust_zip,cust_name\G;
id: 1
select_type: SIMPLE
table: customers2
type: ref
possible_keys: idx_cu_id
key: idx_cu_id
key_len: 33
ref: const
rows: 1
Extra: Using where; Using index
id: 1
select_type: SIMPLE
table: customers1
type: ref
possible_keys: idx_cu_id
key: idx_cu_id
key_len: 33
ref: const
rows: 1
Extra: Using where
字段的COLLATE一致后执行计划使用到了索引,因此必定要注意表字段的collate属性的定义保持一致。
在使用索引时,咱们能够经过explain查看SQL的执行计划,判断是否使用了索引以及发生了隐式转换,建立合适的索引。索引太复杂,建立需谨慎。