当前位置:   article > 正文

TiDB实战篇-基于索引的SQL优化_tidb 加索引 在线

tidb 加索引 在线

简介

TiDB实战篇-基于索引的SQL优化。

Online DDL

允许直接在线执行ddl操作,不会锁表,但是执行多个ddl的时候会要排队。

原理

 

控制创建索引的速度

 

查看影响

标红的是加了多少行索引。 

添加索引的线上的影响(读写频繁)

 

 

只读场景

 

不涉及读写

 

索引扫描的方式

 

直接从索性计算

 

索引选择的规则

 

实操

优化创建索引的是时间

  1. mysql -h192.168.66.10 -P4000 -uroot -ptidb
  2. #创建数据库
  3. create database test2;
  4. create table t1(a int,b int);
  5. #导入数据
  6. for i in `seq 10000`; do mysql -uroot -P4000 -ptidb -h192.168.66.10 -e "insert into test2.t1 values($i,floor(rand()*10000000))";done;
  7. for i in `seq 33`; do mysql -uroot -P4000 -ptidb -h192.168.66.10 -e "insert into test2.t1 select * from test2.t1 limit 100000";done;
  8. #调节创建索引速度的参数
  9. set global tidb_ddl_reorg_worker_cnt=1;
  10. set global tidb_ddl_reorg_batch_size=32;
  11. #创建索引
  12. mysql -h192.168.66.10 -P4000 -uroot -ptidb
  13. use test2;
  14. create index index_t1_a on t1(a);
  15. #查看索引创建的速度
  16. admin show ddl jobs where table_name = 't1';
  17. mysql>admin show ddl jobs where table_name = 't1';
  18. +--------+---------+------------+--------------+----------------------+-----------+----------+-----------+---------------------+---------------------+---------------------+---------+
  19. | JOB_ID | DB_NAME | TABLE_NAME | JOB_TYPE | SCHEMA_STATE | SCHEMA_ID | TABLE_ID | ROW_COUNT | CREATE_TIME | START_TIME | END_TIME | STATE |
  20. +--------+---------+------------+--------------+----------------------+-----------+----------+-----------+---------------------+---------------------+---------------------+---------+
  21. | 86 | test2 | t1 | add index | write reorganization | 82 | 84 | 479488 | 2023-04-30 14:44:51 | 2023-04-30 14:44:51 | NULL | running |
  22. | 85 | test2 | t1 | create table | public | 82 | 84 | 0 | 2023-04-30 14:39:45 | 2023-04-30 14:39:45 | 2023-04-30 14:39:45 | synced |
  23. | 80 | test2 | t1 | create table | public | 77 | 79 | 0 | 2023-04-30 12:59:23 | 2023-04-30 12:59:23 | 2023-04-30 12:59:23 | synced |
  24. +--------+---------+------------+--------------+----------------------+-----------+----------+-----------+---------------------+---------------------+---------------------+---------+
  25. 3 rows in set (0.01 sec)
  26. Query OK, 0 rows affected (3 min 20.92 sec)
  27. #调节创建索引速度的参数
  28. set global tidb_ddl_reorg_worker_cnt=32;
  29. set global tidb_ddl_reorg_batch_size=512;
  30. #删除掉之前的索引
  31. alter table t1 drop index index_t1_a;
  32. #重新创建索引
  33. create index index_t1_a on t1(a);
  34. 用时Query OK, 0 rows affected (2 min 44.09 sec)

Point_Get 

  1. #point get触发
  2. mysql -h192.168.66.10 -P4000 -uroot -ptidb
  3. create database test3;
  4. use test3;
  5. create table t1(a int,b int);
  6. #导入实验数据
  7. for i in `seq 10000`; do mysql -uroot -P4000 -ptidb -h192.168.66.10 -e "insert into test3.t1 values($i,floor(rand()*10000000))";done;
  8. for i in `seq 33`; do mysql -uroot -P4000 -ptidb -h192.168.66.10 -e "insert into test3.t1 select * from test2.t1 limit 100000";done;
  9. #查看导入数据和执行计划
  10. select count(*) from t1;
  11. explain select * from t1 where a=888;
  12. mysql> explain select * from t1 where a=888;
  13. +-------------------------+---------+-----------+---------------+--------------------------------+
  14. | id | estRows | task | access object | operator info |
  15. +-------------------------+---------+-----------+---------------+--------------------------------+
  16. | TableReader_7 | 3.91 | root | | data:Selection_6 |
  17. | └─Selection_6 | 3.91 | cop[tikv] | | eq(test3.t1.a, 888) |
  18. | └─TableFullScan_5 | 3906.00 | cop[tikv] | table:t1 | keep order:false, stats:pseudo |
  19. +-------------------------+---------+-----------+---------------+--------------------------------+
  20. 3 rows in set (0.00 sec)
  21. #添加索引(非唯一索引)
  22. create index idx on t1(a);
  23. explain select * from t1 where a=888;
  24. mysql> explain select * from t1 where a=888;
  25. +-------------------------------+---------+-----------+------------------------+-----------------------------------+
  26. | id | estRows | task | access object | operator info |
  27. +-------------------------------+---------+-----------+------------------------+-----------------------------------+
  28. | IndexLookUp_10 | 1.00 | root | | |
  29. | ├─IndexRangeScan_8(Build) | 1.00 | cop[tikv] | table:t1, index:idx(a) | range:[888,888], keep order:false |
  30. | └─TableRowIDScan_9(Probe) | 1.00 | cop[tikv] | table:t1 | keep order:false |
  31. +-------------------------------+---------+-----------+------------------------+-----------------------------------+
  32. 3 rows in set (0.00 sec)
  33. #添加唯一索引
  34. alter table t1 drop index idx;
  35. create unique index idx on t1(a);
  36. explain select * from t1 where a=888;
  37. mysql> explain select * from t1 where a=888;
  38. +-------------+---------+------+------------------------+---------------+
  39. | id | estRows | task | access object | operator info |
  40. +-------------+---------+------+------------------------+---------------+
  41. | Point_Get_1 | 1.00 | root | table:t1, index:idx(a) | |
  42. +-------------+---------+------+------------------------+---------------+
  43. 1 row in set (0.00 sec)
  44. #测试非等值查询
  45. explain select * from t1 where a>888;
  46. mysql> explain select * from t1 where a>888;
  47. +-------------------------+----------+-----------+---------------+---------------------+
  48. | id | estRows | task | access object | operator info |
  49. +-------------------------+----------+-----------+---------------+---------------------+
  50. | TableReader_7 | 9112.00 | root | | data:Selection_6 |
  51. | └─Selection_6 | 9112.00 | cop[tikv] | | gt(test3.t1.a, 888) |
  52. | └─TableFullScan_5 | 10000.00 | cop[tikv] | table:t1 | keep order:false |
  53. +-------------------------+----------+-----------+---------------+---------------------+
  54. 3 rows in set (0.01 sec)
  55. #测试空值是否会触发
  56. insert into t1 values(null,10000);
  57. explain select * from t1 where a is null;
  58. mysql> explain select * from t1 where a is null;
  59. +-------------------------------+---------+-----------+------------------------+-------------------------------------+
  60. | id | estRows | task | access object | operator info |
  61. +-------------------------------+---------+-----------+------------------------+-------------------------------------+
  62. | IndexLookUp_10 | 1.00 | root | | |
  63. | ├─IndexRangeScan_8(Build) | 1.00 | cop[tikv] | table:t1, index:idx(a) | range:[NULL,NULL], keep order:false |
  64. | └─TableRowIDScan_9(Probe) | 1.00 | cop[tikv] | table:t1 | keep order:false |
  65. +-------------------------------+---------+-----------+------------------------+-------------------------------------+
  66. 3 rows in set (0.00 sec)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/655269
推荐阅读
相关标签
  

闽ICP备14008679号