Index Extensions含義
MySQL5.6開始 InnoDB可以通過主鍵自動(dòng)擴(kuò)展二級(jí)索引的功能稱為Index Extensions,即二級(jí)索引除了存儲(chǔ)本列索引的key值外,還存儲(chǔ)著主鍵列key值。
創(chuàng)建如下測(cè)試表
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),二級(jí)索引是(d),但是InnoDB會(huì)自動(dòng)擴(kuò)展這個(gè)二級(jí)索引,變成(d,i1,i2),這樣對(duì)于一些特殊的查詢,可以提高性能;
開啟index extensions,并測(cè)試使用該功能
即: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)關(guān)閉index extensions,并測(cè)試沒有使用該功能
即: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)總結(jié)
開啟Index Extensions的時(shí)候,使用了索引擴(kuò)展索引(d,i1,i2),從key=k_id,key_len=8,ref=const,const可以看出來,直接通過索引,不用回表(Using index),返回1行結(jié)果;
而關(guān)閉Index Extensions的時(shí)候,使用的是部分PK,并且檢索5行,通過where后面的條件過濾20%,即最終返回1行數(shù)據(jù),需要回表,效率肯定沒有前面一種好;
參考鏈接
https://dev.mysql.com/doc/refman/5.7/en/index-extensions.html
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
網(wǎng)站標(biāo)題:MySQLIndexExtensions-創(chuàng)新互聯(lián)
本文URL:http://www.rwnh.cn/article4/jdsie.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、手機(jī)網(wǎng)站建設(shè)、商城網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)、云服務(wù)器、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容
移動(dòng)網(wǎng)站建設(shè)知識(shí)