mysql 索引 單錶優化

2021-10-02 19:58:16 字數 1425 閱讀 9241

索引分析:

type 為 all , using filesort (自己進行了排序沒有用到索引);

檢視 article 表有什麼索引: show index from  article;

只有乙個主鍵

建立索引:

where 後面的字段需要建索引,但不一定必須建。

嘗試著去建:

alter table 「表名 」 add index 索引名 ('欄位名','欄位名');

或者:create index 索引名 on 表名 (『欄位名』,『欄位名』);

然後檢視字段:

show  index from 表名;

然後看看加完索引的結果:

比較加完索引與不加索引的區別;

type :由 all  變為了 range  ,很好的改變 

但是 extra 還有:using filesort;

如果將 comments> 1 變為 comments =1 

結果會很不一樣

當where 後面的索引字段 出現範圍時 如 上面的comments > 1 ,索引是無法使用的,以及後面的索引字段 view。

進一步優化:

更換索引:

drop index  索引名 on 表明;

跳過   comments ,直接跳過comments 這個字段 ,用categroy 和 view 建立索引:

mysql 單 索引 mysql 單錶索引優化

建表語句 create table if not exists article id int 10 unsigned not null primary key auto increment,author id int 10 unsigned not null,category id int 10 u...

MySQL索引優化(索引單錶優化案例)

1 單錶查詢優化 建表sql 案例 查詢 category id 為1 且 comments 大於 1 的情況下,views 最多的 article id。執行sql 結論 很顯然,type 是 all,即最壞的情況。extra 裡還出現了 using filesort,也是最壞的情況。優化是必須的...

Mysql之索引優化案例 單錶優化 雙表優化

單錶優化 建表create table article id int unsigned notnull primary keyauto increment author id int unsigned notnull category id int unsigned notnull views in...