可以看小視頻的瀏覽器南寧百度快速優(yōu)化
- 索引本質(zhì)上是數(shù)據(jù)庫引擎用來快速查找數(shù)據(jù)的數(shù)據(jù)結(jié)構(gòu),可以顯著提高查詢的性能,為了加快運(yùn)行較慢的查詢。
- 創(chuàng)建索引
- 默認(rèn)索引
- create index 索引名 on 表名 (列名);
- 通過對(duì)列名進(jìn)行創(chuàng)建索引,在查詢的時(shí)候,數(shù)據(jù)庫就能通過索引找到匹配的那些行,適用于支持高效的范圍查詢。
-
-- 默認(rèn)索引 create index idx_points on customers (points); select customer_id from customers where points>1000;
- 前綴索引
- create index idx_last_name on customers (last_name(5));
- 對(duì)于長(zhǎng)字符串列,建議使用前綴索引來節(jié)省存儲(chǔ)空間和提高查詢效率。前綴索引只索引字符串的前幾個(gè)字符。
-
--前綴索引 create index idx_last_name on customers (last_name(5)); select customer_id from customers where last_name like 'boa%';
- 全文索引
- create fulltext index idx_title_body on posts(title,body);
- 全文索引可以在應(yīng)用程序里打造快速強(qiáng)大的搜索引擎,適用于需要高效文本搜索,如文章,博客等。
-
-- 全文索引 create fulltext index idx_title_body on posts(title,body); select *,match(title,body) against ('react redux') from posts -- where match (全文索引的列名(所有)) against(關(guān)鍵詞)-- where match(title,body) against ('react redux') -- 表示包含 'react' 或 'redux' 的記錄where match(title,body) against('react -redux +form' in boolean mode) -- 表示包含 'react' ,不包含 'redux',必須包含'form' 的記錄
- 復(fù)合索引
- create index idx_state_points on customers (state,points);
- 復(fù)合索引也叫組合索引,就是對(duì)多個(gè)列建立一個(gè)索引,遵循最左前綴原則,適用于需要在多個(gè)列進(jìn)行查詢的場(chǎng)景,顯著提高查詢效率。
-
-- 復(fù)合索引 create index idx_state_points on customers (state,points); explain select customer_id from customers where state='ca' and points>1000;
- 默認(rèn)索引
- 刪除索引
- drop index idx_state on customers;
- 使用索引排序
- 使用索引對(duì)數(shù)據(jù)進(jìn)行排序,當(dāng)你在添加索引時(shí),MySQL會(huì)獲取該列中的所有值,對(duì)其排序,并將它們存儲(chǔ)在索引中。
- 覆蓋索引
- 一個(gè)包含所有滿足查詢需要的數(shù)據(jù)的索引,數(shù)據(jù)庫可以直接從索引中獲取數(shù)據(jù),避免了回表操作,顯著提高查詢性能。
- 維護(hù)索引
- 索引可以極大地提高查詢的性能,但要注意在創(chuàng)建新索引之前,要先查看現(xiàn)有索引,否則容易產(chǎn)生"重復(fù)索引"和"多余索引"。
- "重復(fù)索引",指同一組列上且順序一致的索引,對(duì)于該索引應(yīng)刪除。
- "多于索引",指多個(gè)索引的前綴列相同,或復(fù)合索引中包含了主鍵的索引,對(duì)于該索引應(yīng)合并。