-
Notifications
You must be signed in to change notification settings - Fork 55
第21章 SQL优化
xiaoboluo768 edited this page Jun 18, 2020
·
3 revisions
- 4.基数、选择性、回表
mysql> create table t1 (id int , c1 char(20), c2 char(20), c3 char(20));
Query OK, 0 rows affected (0.02 sec)
mysql> insert into t1 values (10, 'a', 'b' , 'c');
Query OK, 1 row affected (0.01 sec)
mysql> insert into t1 values (10, 'a', 'b' , 'c');
Query OK, 1 row affected (0.01 sec)
mysql> insert into t1 values (10, 'a', 'b' , 'c');
Query OK, 1 row affected (0.01 sec)
mysql> insert into t1 values (10, 'a', 'b' , 'c');
Query OK, 1 row affected (0.01 sec)
mysql> insert into t1 values (10, 'a', 'b' , 'c');
Query OK, 1 row affected (0.01 sec)
mysql> insert into t1 values (10, 'a', 'b' , 'c');
Query OK, 1 row affected (0.01 sec)
mysql> create index idx_c1 on t1 (c1);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> explain format=json select * from t1 where c1 = 'a';
......
mysql> drop index idx_c1 on t1;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> explain format=json select * from t1 where c1 = 'a';
......
mysql> truncate table t1;
Query OK, 0 rows affected (0.04 sec)
mysql> insert into t1 values (10, 'a', 'b','c');
Query OK, 1 row affected (0.01 sec)
mysql> insert into t1 values (10, 'b', 'b','c');
Query OK, 1 row affected (0.01 sec)
mysql> insert into t1 values (10, 'c', 'b','c');
Query OK, 1 row affected (0.01 sec)
mysql> insert into t1 values (10, 'd', 'b','c');
Query OK, 1 row affected (0.00 sec)
mysql> insert into t1 values (10, 'e', 'b','c');
Query OK, 1 row affected (0.01 sec)
mysql> explain format=json select * from t1 where c1 = 'a';
......
mysql> drop index idx_c1 on t1;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> explain format=json select * from t1 where c1 = 'a';
......
SELECT stat_value AS pages, index_name
, stat_value * @@innodb_page_size / 1024 / 1024 AS size
FROM mysql.innodb_index_stats
WHERE (table_name = 'sbtest1'
AND database_name = 'sbtest'
AND stat_description = 'Number of pages in the index'
AND stat_name = 'size')
GROUP BY index_name;
SELECT stat_value AS pages, index_name
, SUM(stat_value) * @@innodb_page_size / 1024 / 1024 AS size
FROM mysql.innodb_index_stats
WHERE (table_name LIKE 't#P%'
AND database_name = 'test'
AND stat_description = 'Number of pages in the index'
AND stat_name = 'size')
GROUP BY index_name;
- 1.Nested-Loop Join Algorithm(嵌套循环Join算法)
for each row in t1 matching range {
for each row in t2 matching reference key {
for each row in t3 {
if row satisfies join conditions, send to client
}
}
}
- 2.Block Nested-Loop Join Algorithm(块嵌套循环Join算法,即BNL算法)
for each row in t1 matching range {
for each row in t2 matching reference key {
store used columns from t1, t2 in join buffer
if buffer is full {
for each row in t3 {
for each t1, t2 combination in join buffer {
if row satisfies join conditions, send to client
}
}
empty join buffer
}
}
}
if buffer is not empty {
for each row in t3 {
for each t1, t2 combination in join buffer {
if row satisfies join conditions, send to client
}
}
}
上一篇:第20章 InnoDB锁 | 下一篇:第26章 SQL语句执行慢真假难辨
-
本 WIKI 包含了《千金良方--MySQL 性能优化金字塔法则》一书的代码段加粗命令行命令和SQL语句文本、以及4个附录内容,其中:
- 代码段和高清图单独整理为一个系列文档,如下:
- 每个附录都各自整理成了一个小系列文档,如下:
-
《千金良方--MySQL 性能优化金字塔法则》 一书的作者信息如下:
- 李春、罗小波、董红禹
-
联系人QQ:309969177
-
提示:
-
郑重声明:本WIKI仓库中的资料为电子工业出版社与本书的三位作者共同授权开源,为了在方便大家的同时,避免不必要的纠葛,任何商业与非商业的引用、转载,麻烦大家注明出处,谢谢配合!