您好,登錄后才能下訂單哦!
本篇內容介紹了“什么是MySQL索引提示”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
SQL提示,是優化數據庫的一個重要手段,簡單來說,就是在SQL語句中加入一些人為的提示來達到優化操作的目的。MySQL數據庫支持索引提示(INDEX HINT)顯式的告訴優化器使用了哪個索引。有以下幾種情況可能用到索引提示:
1、MySQL數據庫的優化器錯誤的選擇了某個索引,導致SQL運行很慢。這個在情況比較少見。優化器在絕大部分情況下工作的非常有效和正確。
2、某些SQL語句可以選擇的索引非常多,這時優化器選擇執行計劃時間的開銷可能會大于SQL語句本身。例如優化器分析Range查詢本身就是比較耗時的操作。這時DBA或開發人員分析最優的索引選擇,通過index hint來強制使優化器不進行各個路徑的成本分析直接選擇指定的索引來完成查詢。
index hint種類
MySql共有三種索引提示,分別是:USE INDEX、IGNORE INDEX和FORCE INDEX,他們之間的區別是:
1、use index:use index告訴MySql用列表中的其中一個索引去做本次查詢,就可以讓MySQL不再考慮其他可用的索引建議MySQL用這些索引,但是MySQL不一定會用。
MySQL > show create table test2 \G *************************** 1. row *************************** Table: test2 Create Table: CREATE TABLE `test2` ( `id` bigint(16) NOT NULL AUTO_INCREMENT, `order_seq` bigint(16) NOT NULL, `order_type` int(11) DEFAULT NULL, `order_flag` int(11) DEFAULT NULL, PRIMARY KEY (`id`), KEY `idx_id` (`id`), KEY `idx_id_orderseq` (`id`,`order_seq`), KEY `idx_order_seq` (`order_seq`) ) ENGINE=InnoDB AUTO_INCREMENT=15002212 DEFAULT CHARSET=utf8mb4 1 row in set (0.00 sec) MySQL > explain select * from test2 where id>10000 and id<1000000; +----+-------------+-------+------------+-------+--------------------------------+---------+---------+------+---------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+-------+--------------------------------+---------+---------+------+---------+----------+-------------+ | 1 | SIMPLE | test2 | NULL | range | PRIMARY,idx_id,idx_id_orderseq | PRIMARY | 8 | NULL | 2021716 | 100.00 | Using where | +----+-------------+-------+------------+-------+--------------------------------+---------+---------+------+---------+----------+-------------+ 1 row in set, 1 warning (0.00 sec) MySQL > explain select * from test2 use index(idx_id) where id>10000 and id<100000; +----+-------------+-------+------------+-------+---------------+--------+---------+------+--------+----------+-----------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+-------+---------------+--------+---------+------+--------+----------+-----------------------+ | 1 | SIMPLE | test2 | NULL | range | idx_id | idx_id | 8 | NULL | 180580 | 100.00 | Using index condition | +----+-------------+-------+------------+-------+---------------+--------+---------+------+--------+----------+-----------------------+ 1 row in set, 1 warning (0.00 sec) MySQL > explain select * from test2 use index(idx_order_seq) where id=10000; +----+-------------+-------+------------+------+---------------+------+---------+------+----------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+------+---------------+------+---------+------+----------+----------+-------------+ | 1 | SIMPLE | test2 | NULL | ALL | NULL | NULL | NULL | NULL | 14611349 | 0.00 | Using where | +----+-------------+-------+------------+------+---------------+------+---------+------+----------+----------+-------------+ 1 row in set, 1 warning (0.01 sec)
2、ignore index:ignore index告訴mysql不要使用某些索引去做本次查詢
MySQL > show create table test2 \G *************************** 1. row *************************** Table: test2 Create Table: CREATE TABLE `test2` ( `id` bigint(16) NOT NULL AUTO_INCREMENT, `order_seq` bigint(16) NOT NULL, `order_type` int(11) DEFAULT NULL, `order_flag` int(11) DEFAULT NULL, PRIMARY KEY (`id`), KEY `idx_id` (`id`), KEY `idx_id_orderseq` (`id`,`order_seq`), KEY `idx_order_seq` (`order_seq`) ) ENGINE=InnoDB AUTO_INCREMENT=15002212 DEFAULT CHARSET=utf8mb4 1 row in set (0.00 sec) MySQL > explain select * from test2 where id>10000 and id<1000000; +----+-------------+-------+------------+-------+--------------------------------+---------+---------+------+---------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+-------+--------------------------------+---------+---------+------+---------+----------+-------------+ | 1 | SIMPLE | test2 | NULL | range | PRIMARY,idx_id,idx_id_orderseq | PRIMARY | 8 | NULL | 2021716 | 100.00 | Using where | +----+-------------+-------+------------+-------+--------------------------------+---------+---------+------+---------+----------+-------------+ 1 row in set, 1 warning (0.00 sec) MySQL > explain select * from test2 ignore index(primary) where id>10000 and id<1000000; +----+-------------+-------+------------+-------+------------------------+--------+---------+------+---------+----------+-----------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+-------+------------------------+--------+---------+------+---------+----------+-----------------------+ | 1 | SIMPLE | test2 | NULL | range | idx_id,idx_id_orderseq | idx_id | 8 | NULL | 1971862 | 100.00 | Using index condition | +----+-------------+-------+------------+-------+------------------------+--------+---------+------+---------+----------+-----------------------+ 1 row in set, 1 warning (0.00 sec)
3、force index:強制MySQL使用一個特定的索引
MySQL > show create table test2 \G *************************** 1. row *************************** Table: test2 Create Table: CREATE TABLE `test2` ( `id` bigint(16) NOT NULL AUTO_INCREMENT, `order_seq` bigint(16) NOT NULL, `order_type` int(11) DEFAULT NULL, `order_flag` int(11) DEFAULT NULL, PRIMARY KEY (`id`), KEY `idx_id` (`id`), KEY `idx_id_orderseq` (`id`,`order_seq`), KEY `idx_order_seq` (`order_seq`) ) ENGINE=InnoDB AUTO_INCREMENT=15002212 DEFAULT CHARSET=utf8mb4 1 row in set (0.00 sec) MySQL > explain select * from test2 where id>10000 and id<1000000; +----+-------------+-------+------------+-------+--------------------------------+---------+---------+------+---------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+-------+--------------------------------+---------+---------+------+---------+----------+-------------+ | 1 | SIMPLE | test2 | NULL | range | PRIMARY,idx_id,idx_id_orderseq | PRIMARY | 8 | NULL | 2021716 | 100.00 | Using where | +----+-------------+-------+------------+-------+--------------------------------+---------+---------+------+---------+----------+-------------+ 1 row in set, 1 warning (0.00 sec) MySQL > explain select * from test2 force index(idx_id) where id>10000 and id<1000000; +----+-------------+-------+------------+-------+---------------+--------+---------+------+---------+----------+-----------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+-------+---------------+--------+---------+------+---------+----------+-----------------------+ | 1 | SIMPLE | test2 | NULL | range | idx_id | idx_id | 8 | NULL | 1971862 | 100.00 | Using index condition | +----+-------------+-------+------------+-------+---------------+--------+---------+------+---------+----------+-----------------------+ 1 row in set, 1 warning (0.00 sec)
“什么是MySQL索引提示”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。