MySQLl数据量不一样,导致走不同的索引
发表于:2025-01-22 作者:千家信息网编辑
千家信息网最后更新 2025年01月22日,1、测试环境:MySQL 5.7.172、测试表结构mysql> show create table a;+-------+--------------------------------------
千家信息网最后更新 2025年01月22日MySQLl数据量不一样,导致走不同的索引
1、测试环境:MySQL 5.7.17
2、测试表结构
mysql> show create table a;+-------+--------------------------------------------------------------------------------------------------------------------------------------+| Table | Create Table |+-------+--------------------------------------------------------------------------------------------------------------------------------------+| a | CREATE TABLE `a` ( `id` int(11) NOT NULL, `name` char(20) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=gbk |+-------+--------------------------------------------------------------------------------------------------------------------------------------+1 row in set (0.00 sec)
mysql> show create table b;+-------+------------------------------------------------------------------------------------------------------------------------------------+| Table | Create Table |+-------+------------------------------------------------------------------------------------------------------------------------------------+| b | CREATE TABLE `b` ( `id` int(11) NOT NULL, `tx` char(20) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=gbk |+-------+------------------------------------------------------------------------------------------------------------------------------------+1 row in set (0.00 sec)
3、两张表的数据量
mysql> select count(*) from a;+----------+| count(*) |+----------+| 7 |+----------+1 row in set (0.00 sec)mysql> select count(*) from b;+----------+| count(*) |+----------+| 10 |+----------+1 row in set (0.00 sec)
4、查看执行计划
mysql> explain select name from a,b where a.id=b.id;+----+-------------+-------+------------+--------+---------------+---------+---------+-----------+------+----------+-------------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+-------------+-------+------------+--------+---------------+---------+---------+-----------+------+----------+-------------+| 1 | SIMPLE | a | NULL | ALL | PRIMARY | NULL | NULL | NULL | 7 | 100.00 | NULL || 1 | SIMPLE | b | NULL | eq_ref | PRIMARY | PRIMARY | 4 | apex.a.id | 1 | 100.00 | Using index |+----+-------------+-------+------------+--------+---------------+---------+---------+-----------+------+----------+-------------+2 rows in set, 1 warning (0.00 sec)
mysql> explain select name from a,b where b.id=a.id;+----+-------------+-------+------------+--------+---------------+---------+---------+-----------+------+----------+-------------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+-------------+-------+------------+--------+---------------+---------+---------+-----------+------+----------+-------------+| 1 | SIMPLE | a | NULL | ALL | PRIMARY | NULL | NULL | NULL | 7 | 100.00 | NULL || 1 | SIMPLE | b | NULL | eq_ref | PRIMARY | PRIMARY | 4 | apex.a.id | 1 | 100.00 | Using index |+----+-------------+-------+------------+--------+---------------+---------+---------+-----------+------+----------+-------------+2 rows in set, 1 warning (0.00 sec)
mysql> explain select name from b,a where b.id=a.id;+----+-------------+-------+------------+--------+---------------+---------+---------+-----------+------+----------+-------------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+-------------+-------+------------+--------+---------------+---------+---------+-----------+------+----------+-------------+| 1 | SIMPLE | a | NULL | ALL | PRIMARY | NULL | NULL | NULL | 7 | 100.00 | NULL || 1 | SIMPLE | b | NULL | eq_ref | PRIMARY | PRIMARY | 4 | apex.a.id | 1 | 100.00 | Using index |+----+-------------+-------+------------+--------+---------------+---------+---------+-----------+------+----------+-------------+2 rows in set, 1 warning (0.00 sec)
5、向a表插入3数据,使两表数据量一样,查看执行计划,发现第三条语句的执行计划发生了变化
mysql> insert into a values(8,'test');Query OK, 1 row affected (0.00 sec)mysql> insert into a values(9,'test');Query OK, 1 row affected (0.00 sec)mysql> insert into a values(10,'test');Query OK, 1 row affected (0.01 sec)mysql> select count(*) from a;+----------+| count(*) |+----------+| 10 |+----------+1 row in set (0.00 sec)
mysql> explain select name from a,b where a.id=b.id;+----+-------------+-------+------------+--------+---------------+---------+---------+-----------+------+----------+-------------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+-------------+-------+------------+--------+---------------+---------+---------+-----------+------+----------+-------------+| 1 | SIMPLE | a | NULL | ALL | PRIMARY | NULL | NULL | NULL | 10 | 100.00 | NULL || 1 | SIMPLE | b | NULL | eq_ref | PRIMARY | PRIMARY | 4 | apex.a.id | 1 | 100.00 | Using index |+----+-------------+-------+------------+--------+---------------+---------+---------+-----------+------+----------+-------------+2 rows in set, 1 warning (0.00 sec)
mysql> explain select name from a,b where b.id=a.id;+----+-------------+-------+------------+--------+---------------+---------+---------+-----------+------+----------+-------------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+-------------+-------+------------+--------+---------------+---------+---------+-----------+------+----------+-------------+| 1 | SIMPLE | a | NULL | ALL | PRIMARY | NULL | NULL | NULL | 10 | 100.00 | NULL || 1 | SIMPLE | b | NULL | eq_ref | PRIMARY | PRIMARY | 4 | apex.a.id | 1 | 100.00 | Using index |+----+-------------+-------+------------+--------+---------------+---------+---------+-----------+------+----------+-------------+2 rows in set, 1 warning (0.00 sec)
mysql> explain select name from b,a where b.id=a.id;+----+-------------+-------+------------+--------+---------------+---------+---------+-----------+------+----------+-------------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+-------------+-------+------------+--------+---------------+---------+---------+-----------+------+----------+-------------+| 1 | SIMPLE | b | NULL | index | PRIMARY | PRIMARY | 4 | NULL | 10 | 100.00 | Using index || 1 | SIMPLE | a | NULL | eq_ref | PRIMARY | PRIMARY | 4 | apex.b.id | 1 | 100.00 | NULL |+----+-------------+-------+------------+--------+---------------+---------+---------+-----------+------+----------+-------------+2 rows in set, 1 warning (0.00 sec)
6、向a表插入1条数据,使a表数据量大于b表,查看执行计划,三条语句执行计划都发现了变化
mysql> insert into a values(11,'test');Query OK, 1 row affected (0.01 sec)mysql> select count(*) from a;+----------+| count(*) |+----------+| 11 |+----------+1 row in set (0.00 sec)
mysql> explain select name from a,b where a.id=b.id;+----+-------------+-------+------------+--------+---------------+---------+---------+-----------+------+----------+-------------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+-------------+-------+------------+--------+---------------+---------+---------+-----------+------+----------+-------------+| 1 | SIMPLE | b | NULL | index | PRIMARY | PRIMARY | 4 | NULL | 10 | 100.00 | Using index || 1 | SIMPLE | a | NULL | eq_ref | PRIMARY | PRIMARY | 4 | apex.b.id | 1 | 100.00 | NULL |+----+-------------+-------+------------+--------+---------------+---------+---------+-----------+------+----------+-------------+2 rows in set, 1 warning (0.00 sec)
mysql> explain select name from a,b where b.id=a.id;+----+-------------+-------+------------+--------+---------------+---------+---------+-----------+------+----------+-------------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+-------------+-------+------------+--------+---------------+---------+---------+-----------+------+----------+-------------+| 1 | SIMPLE | b | NULL | index | PRIMARY | PRIMARY | 4 | NULL | 10 | 100.00 | Using index || 1 | SIMPLE | a | NULL | eq_ref | PRIMARY | PRIMARY | 4 | apex.b.id | 1 | 100.00 | NULL |+----+-------------+-------+------------+--------+---------------+---------+---------+-----------+------+----------+-------------+2 rows in set, 1 warning (0.00 sec)
mysql> explain select name from b,a where b.id=a.id;+----+-------------+-------+------------+--------+---------------+---------+---------+-----------+------+----------+-------------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+-------------+-------+------------+--------+---------------+---------+---------+-----------+------+----------+-------------+| 1 | SIMPLE | b | NULL | index | PRIMARY | PRIMARY | 4 | NULL | 10 | 100.00 | Using index || 1 | SIMPLE | a | NULL | eq_ref | PRIMARY | PRIMARY | 4 | apex.b.id | 1 | 100.00 | NULL |+----+-------------+-------+------------+--------+---------------+---------+---------+-----------+------+----------+-------------+2 rows in set, 1 warning (0.01 sec)
数据
语句
三条
变化
测试
环境
结构
不同
索引
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
软件开发原型工具有哪些
数据库事物语句
安徽企业软件开发哪个好
北京正规软件开发服务应用
M60服务器
如何查看服务器的内存
数据库价值评估
天赐软件开发下载
警察学院公共管理软件开发厂家
舒兰市天气预报软件开发
软件开发原型模型
数据库赚钱
服务器安全组有哪些端口
国家网络安全宣传日是几月几日
数据库2008连接服务器
网络安全法策划书
指游网络技术有限公司
计算机网络技术实用教程李畅
连接不上数据库
冰峰网络安全绘画
csol连接服务器中断重新登陆
相机影像数据库忘记受损
小学生网络安全学情分析
工业自动化网络技术
舒兰市天气预报软件开发
数据库安全方面事件
软件开发的企业有哪些上市公司
鲲鹏服务器和华为的关系
长夜萤火服务器
14年网络安全法