您好,登錄后才能下訂單哦!
Index Extensions含義
MySQL5.6開始 InnoDB可以通過主鍵自動擴展二級索引的功能稱為Index Extensions,即二級索引除了存儲本列索引的key值外,還存儲著主鍵列key值。
創建如下測試表
mysql> show create table t1\G *************************** 1. row *************************** Table: t1 Create Table: CREATE TABLE `t1` ( `i1` int(11) NOT NULL DEFAULT '0', `i2` int(11) NOT NULL DEFAULT '0', `d` date DEFAULT NULL, PRIMARY KEY (`i1`,`i2`), KEY `k_d` (`d`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 1 row in set (0.00 sec) mysql> INSERT INTO t1 VALUES -> (1, 1, '1998-01-01'), (1, 2, '1999-01-01'), -> (1, 3, '2000-01-01'), (1, 4, '2001-01-01'), -> (1, 5, '2002-01-01'), (2, 1, '1998-01-01'), -> (2, 2, '1999-01-01'), (2, 3, '2000-01-01'), -> (2, 4, '2001-01-01'), (2, 5, '2002-01-01'), -> (3, 1, '1998-01-01'), (3, 2, '1999-01-01'), -> (3, 3, '2000-01-01'), (3, 4, '2001-01-01'), -> (3, 5, '2002-01-01'), (4, 1, '1998-01-01'), -> (4, 2, '1999-01-01'), (4, 3, '2000-01-01'), -> (4, 4, '2001-01-01'), (4, 5, '2002-01-01'), -> (5, 1, '1998-01-01'), (5, 2, '1999-01-01'), -> (5, 3, '2000-01-01'), (5, 4, '2001-01-01'), -> (5, 5, '2002-01-01'); Query OK, 25 rows affected (0.08 sec) Records: 25 Duplicates: 0 Warnings: 0
其中PK是(i1,i2),二級索引是(d),但是InnoDB會自動擴展這個二級索引,變成(d,i1,i2),這樣對于一些特殊的查詢,可以提高性能;
開啟index extensions,并測試使用該功能
即:use_index_extensions=on
mysql> show variables like 'optimizer_switch%'\G *************************** 1. row *************************** Variable_name: optimizer_switch Value: index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=on,index_condition_pushdown=on,mrr=on,mrr_cost_based=on, block_nested_loop=on,batched_key_access=off,materialization=on,semijoin=on,loosescan=on,firstmatch=on,duplicateweedout=on,subquery_materialization_cost_based=on,use_index_extensions=on,condition_fanout_filter=on,derived_merge=on 1 row in set (0.00 sec) mysql> FLUSH TABLE t1; Query OK, 0 rows affected (0.02 sec) mysql> FLUSH STATUS; Query OK, 0 rows affected (0.01 sec) mysql> SELECT COUNT(*) FROM t1 WHERE i1 = 3 AND d = '2000-01-01'; +----------+ | COUNT(*) | +----------+ | 1 | +----------+ 1 row in set (0.00 sec) mysql> SHOW STATUS LIKE 'handler_read%'; +-----------------------+-------+ | Variable_name | Value | +-----------------------+-------+ | Handler_read_first | 0 | | Handler_read_key | 1 | | Handler_read_last | 0 | | Handler_read_next | 1 | | Handler_read_prev | 0 | | Handler_read_rnd | 0 | | Handler_read_rnd_next | 0 | +-----------------------+-------+ 7 rows in set (0.00 sec) mysql> desc SELECT COUNT(*) FROM t1 WHERE i1 = 3 AND d = '2000-01-01'; +----+-------------+-------+------------+------+---------------+------+---------+-------------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+------+---------------+------+---------+-------------+------+----------+-------------+ | 1 | SIMPLE | t1 | NULL | ref | PRIMARY,k_d | k_d | 8 | const,const | 1 | 100.00 | Using index | +----+-------------+-------+------------+------+---------------+------+---------+-------------+------+----------+-------------+ 1 row in set, 1 warning (0.00 sec)
關閉index extensions,并測試沒有使用該功能
即:use_index_extensions=off
mysql> set session optimizer_switch='use_index_extensions=off'; Query OK, 0 rows affected (0.00 sec) mysql> FLUSH TABLE t1; Query OK, 0 rows affected (0.02 sec) mysql> FLUSH STATUS; Query OK, 0 rows affected (0.02 sec) mysql> SELECT COUNT(*) FROM t1 WHERE i1 = 3 AND d = '2000-01-01'; +----------+ | COUNT(*) | +----------+ | 1 | +----------+ 1 row in set (0.00 sec) mysql> SHOW STATUS LIKE 'handler_read%'; +-----------------------+-------+ | Variable_name | Value | +-----------------------+-------+ | Handler_read_first | 0 | | Handler_read_key | 1 | | Handler_read_last | 0 | | Handler_read_next | 5 | | Handler_read_prev | 0 | | Handler_read_rnd | 0 | | Handler_read_rnd_next | 0 | +-----------------------+-------+ 7 rows in set (0.00 sec) mysql> desc SELECT COUNT(*) FROM t1 WHERE i1 = 3 AND d = '2000-01-01'; +----+-------------+-------+------------+------+---------------+---------+---------+-------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+------+---------------+---------+---------+-------+------+----------+-------------+ | 1 | SIMPLE | t1 | NULL | ref | PRIMARY,k_d | PRIMARY | 4 | const | 5 | 20.00 | Using where | +----+-------------+-------+------------+------+---------------+---------+---------+-------+------+----------+-------------+ 1 row in set, 1 warning (0.00 sec)
總結
開啟Index Extensions的時候,使用了索引擴展索引(d,i1,i2),從key=k_id,key_len=8,ref=const,const可以看出來,直接通過索引,不用回表(Using index),返回1行結果;
而關閉Index Extensions的時候,使用的是部分PK,并且檢索5行,通過where后面的條件過濾20%,即最終返回1行數據,需要回表,效率肯定沒有前面一種好;
參考鏈接
https://dev.mysql.com/doc/refman/5.7/en/index-extensions.html
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。