Mysql实战题目,如何加索引2?
这一篇,较上一篇文章区别在于构造的数据内容发生了变化
实战题目
select * from t2 where a=? and b>? order by c limit 0,100 如何加索引
测试环境
mysql> select version() ;
+-----------+
| version() |
+-----------+
| 8.0.17 |
+-----------+
1 row in set (0.01 sec)
附
快速搭建一个指定版本的Mysql https://blog.csdn.net/hl_java/article/details/90259538
表及数据准备
drop table if exists t2;
create table t2(
id int primary key auto_increment,
a int ,
b int ,
c int
) ENGINE=InnoDB;
drop procedure if exists add_data;
DELIMITER ;;
CREATE PROCEDURE add_data2(IN loop_times INT)
BEGIN
DECLARE var INT DEFAULT 1;
WHILE var <= loop_times DO
INSERT INTO t2 VALUES (NULL,var%10,var ,loop_times - var);
SET var = var + 1;
END WHILE;
END
;;
DELIMITER ;
SET @type = 100000;
CALL add_data2(@type);
不同场景测试
SQL1
select * from t2 where a=1 and b>50000 order by c limit 0,100 ;
说明:根据插入的数据我们提前分析sql查询的结果集为5000,然后limt之后就只返回100条了
SQL2
select * from t2 where a=1 and b>100 order by c limit 0,100 ;
说明:根据插入的数据我们提前分析sql查询的结果集为9990,然后limt之后就只返回100条了
仅有主键索引的情况
select * from t2 where a=1 and b>50000 order by c limit 0,100 ;
100 rows in set (0.03 sec)
select * from t2 where a=1 and b>100 order by c limit 0,100 ;
100 rows in set (0.03 sec)
附
explain select * from t2 where a=1 and b>50000 order by c limit 0,100 ;
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-----------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-----------------------------+
| 1 | SIMPLE | t2 | NULL | ALL | NULL | NULL | NULL | NULL | 100068 | 3.33 | Using where; Using filesort |
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-----------------------------+
1 row in set, 1 warning (0.00 sec)
explain select * from t2 where a=1 and b>100 order by c limit 0,100 ;
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-----------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-----------------------------+
| 1 | SIMPLE | t2 | NULL | ALL | NULL | NULL | NULL | NULL | 100068 | 3.33 | Using where; Using filesort |
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-----------------------------+
1 row in set, 1 warning (0.00 sec)
增加字段a索引的情况
alter table t2 add index idx_a (a);
select * from t2 where a=1 and b>50000 order by c limit 0,100 ;
100 rows in set (0.01 sec)
select * from t2 where a=1 and b>100 order by c limit 0,100 ;
100 rows in set (0.01 sec)
附
mysql> explain select * from t2 where a=1 and b>50000 order by c limit 0,100 ;
+----+-------------+-------+------------+------+---------------+-------+---------+-------+-------+----------+-----------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+-------+---------+-------+-------+----------+-----------------------------+
| 1 | SIMPLE | t2 | NULL | ref | idx_a | idx_a | 5 | const | 10000 | 33.33 | Using where; Using filesort |
+----+-------------+-------+------------+------+---------------+-------+---------+-------+-------+----------+-----------------------------+
1 row in set, 1 warning (0.00 sec)
mysql> explain select * from t2 where a=1 and b>100 order by c limit 0,100 ;
+----+-------------+-------+------------+------+---------------+-------+---------+-------+-------+----------+-----------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+-------+---------+-------+-------+----------+-----------------------------+
| 1 | SIMPLE | t2 | NULL | ref | idx_a | idx_a | 5 | const | 10000 | 33.33 | Using where; Using filesort |
+----+-------------+-------+------------+------+---------------+-------+---------+-------+-------+----------+-----------------------------+
1 row in set, 1 warning (0.00 sec)
增加字段a,b联合索引的情况
alter table t2 add index idx_ab (a,b);
select * from t2 where a=1 and b>50000 order by c limit 0,100 ;
100 rows in set (0.01 sec)
select * from t2 where a=1 and b>100 order by c limit 0,100 ;
100 rows in set (0.02 sec)
附
explain select * from t2 where a=1 and b>50000 order by c limit 0,100 ;
+----+-------------+-------+------------+-------+---------------+--------+---------+------+------+----------+---------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+--------+---------+------+------+----------+---------------------------------------+
| 1 | SIMPLE | t2 | NULL | range | idx_ab | idx_ab | 10 | NULL | 5000 | 100.00 | Using index condition; Using filesort |
+----+-------------+-------+------------+-------+---------------+--------+---------+------+------+----------+---------------------------------------+
1 row in set, 1 warning (0.01 sec)
explain select * from t2 where a=1 and b>100 order by c limit 0,100 ;
+----+-------------+-------+------------+-------+---------------+--------+---------+------+-------+----------+---------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+--------+---------+------+-------+----------+---------------------------------------+
| 1 | SIMPLE | t2 | NULL | range | idx_ab | idx_ab | 10 | NULL | 18222 | 100.00 | Using index condition; Using filesort |
+----+-------------+-------+------------+-------+---------------+--------+---------+------+-------+----------+---------------------------------------+
1 row in set, 1 warning (0.00 sec)
增加字段a,c联合索引的情况
alter table t2 drop index idx_ab;
alter table t2 add index idx_ac (a,c);
select * from t2 where a=1 and b>50000 order by c limit 0,100 ;
100 rows in set (0.00 sec)
select * from t2 where a=1 and b>100 order by c limit 0,100 ;
100 rows in set (0.00 sec)
附
explain select * from t2 where a=1 and b>50000 order by c limit 0,100 ;
+----+-------------+-------+------------+------+---------------+--------+---------+-------+-------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+--------+---------+-------+-------+----------+-------------+
| 1 | SIMPLE | t2 | NULL | ref | idx_ac | idx_ac | 5 | const | 18242 | 33.33 | Using where |
+----+-------------+-------+------------+------+---------------+--------+---------+-------+-------+----------+-------------+
1 row in set, 1 warning (0.00 sec)
explain select * from t2 where a=1 and b>100 order by c limit 0,100 ;
+----+-------------+-------+------------+------+---------------+--------+---------+-------+-------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+--------+---------+-------+-------+----------+-------------+
| 1 | SIMPLE | t2 | NULL | ref | idx_ac | idx_ac | 5 | const | 18242 | 33.33 | Using where |
+----+-------------+-------+------------+------+---------------+--------+---------+-------+-------+----------+-------------+
1 row in set, 1 warning (0.00 sec)
增加字段a,b,c联合索引的情况
alter table t2 drop index idx_ac;
alter table t2 add index idx_abc (a,b,c);
select * from t2 where a=1 and b>50000 order by c limit 0,100 ;
100 rows in set (0.01 sec)
select * from t2 where a=1 and b>100 order by c limit 0,100 ;
100 rows in set (0.01 sec)
附
explain select * from t2 where a=1 and b>50000 order by c limit 0,100 ;
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+------------------------------------------+
| 1 | SIMPLE | t2 | NULL | range | idx_abc | idx_abc | 10 | NULL | 5000 | 100.00 | Using where; Using index; Using filesort |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+------------------------------------------+
1 row in set, 1 warning (0.01 sec)
explain select * from t2 where a=1 and b>100 order by c limit 0,100 ;
+----+-------------+-------+------------+-------+---------------+---------+---------+------+-------+----------+------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+-------+----------+------------------------------------------+
| 1 | SIMPLE | t2 | NULL | range | idx_abc | idx_abc | 10 | NULL | 18462 | 100.00 | Using where; Using index; Using filesort |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+-------+----------+------------------------------------------+
1 row in set, 1 warning (0.00 sec)
增加字段c,a,b联合索引的情况
alter table t2 drop index idx_abc;
alter table t2 add index idx_cab (c,a,b);
select * from t2 where a=1 and b>50000 order by c limit 0,100 ;
100 rows in set (0.01 sec)
select * from t2 where a=1 and b>100 order by c limit 0,100 ;
100 rows in set (0.01 sec)
附
explain select * from t2 where a=1 and b>50000 order by c limit 0,100 ;
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+--------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+--------------------------+
| 1 | SIMPLE | t2 | NULL | index | idx_cab | idx_cab | 15 | NULL | 100 | 3.33 | Using where; Using index |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+--------------------------+
1 row in set, 1 warning (0.00 sec)
explain select * from t2 where a=1 and b>100 order by c limit 0,100 ;
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+--------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+--------------------------+
| 1 | SIMPLE | t2 | NULL | index | idx_cab | idx_cab | 15 | NULL | 100 | 3.33 | Using where; Using index |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+--------------------------+
1 row in set, 1 warning (0.00 sec)
表数据内容调整后
与上一篇依旧没有大的变化,加索引后时间开销也还分布在0.01 ~ 0.02 sec之间
下面这篇文章会提到答案
Mysql的explain之Extra字段讲解