本篇內(nèi)容介紹了“什么是MySQL索引提示”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
長沙ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!SQL提示,是優(yōu)化數(shù)據(jù)庫的一個重要手段,簡單來說,就是在SQL語句中加入一些人為的提示來達(dá)到優(yōu)化操作的目的。MySQL數(shù)據(jù)庫支持索引提示(INDEX HINT)顯式的告訴優(yōu)化器使用了哪個索引。有以下幾種情況可能用到索引提示:
1、MySQL數(shù)據(jù)庫的優(yōu)化器錯誤的選擇了某個索引,導(dǎo)致SQL運行很慢。這個在情況比較少見。優(yōu)化器在絕大部分情況下工作的非常有效和正確。
2、某些SQL語句可以選擇的索引非常多,這時優(yōu)化器選擇執(zhí)行計劃時間的開銷可能會大于SQL語句本身。例如優(yōu)化器分析Range查詢本身就是比較耗時的操作。這時DBA或開發(fā)人員分析最優(yōu)的索引選擇,通過index hint來強制使優(yōu)化器不進行各個路徑的成本分析直接選擇指定的索引來完成查詢。
index hint種類
MySql共有三種索引提示,分別是:USE INDEX、IGNORE INDEX和FORCE INDEX,他們之間的區(qū)別是:
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索引提示”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
分享標(biāo)題:什么是MySQL索引提示-創(chuàng)新互聯(lián)
標(biāo)題路徑:http://aaarwkj.com/article34/csocse.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、響應(yīng)式網(wǎng)站、網(wǎng)站設(shè)計公司、微信公眾號、品牌網(wǎng)站制作、動態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容