invisible index特色html
默認建立的索引都是visible,若是須要invisible索引建立的時候須要指定invisible參數。mysql
默認沒法使用invisible index索引,須要開啓參數optimizer_switch='use_invisible_indexes=on'才能使用;sql
使用force index也沒法使用,會報錯;ide
索引的visible和invisible屬性能夠相互轉換,即便表很大,這個過程也是很快的;測試
primary key不能改爲invisible;spa
invisible index做用htm
在生產環境中,每每一個表是很是大的,咱們想要測試一條SQL不使用某個索引的執行效率,若是直接刪除這個索引,可能代價比較大,可是把索引改爲不可見模式,再去測試,時間是很快的。索引
mysql> create index idx_emp_no on t_group(emp_no) invisible; Query OK, 0 rows affected (0.22 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> show index from t_group; +---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression | +---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+ | t_group | 1 | idx_emp_no | 1 | emp_no | A | 10 | NULL | NULL | | BTREE | | | NO | NULL | +---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+ 1 row in set (0.08 sec) mysql> desc select * from t_group where emp_no=31112; +----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+ | 1 | SIMPLE | t_group | NULL | ALL | NULL | NULL | NULL | NULL | 10 | 10.00 | Using where | +----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+ 1 row in set, 1 warning (0.00 sec) mysql> desc select * from t_group force index(idx_emp_no) where emp_no=31112; ERROR 1176 (42000): Key 'idx_emp_no' doesn't exist in table 't_group' mysql> desc select /*+ set_var(optimizer_switch='use_invisible_indexes=on') */ * from t_group where emp_no=31112; +----+-------------+---------+------------+------+---------------+------------+---------+-------+------+----------+-------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+---------+------------+------+---------------+------------+---------+-------+------+----------+-------+ | 1 | SIMPLE | t_group | NULL | ref | idx_emp_no | idx_emp_no | 4 | const | 1 | 100.00 | NULL | +----+-------------+---------+------------+------+---------------+------------+---------+-------+------+----------+-------+ 1 row in set, 1 warning (0.00 sec)
參考連接get