mysql之explain關鍵字

2021-10-02 10:16:51 字數 3755 閱讀 9557

1、 用mysql儲存過程增加100萬條測試資料

儲存過程**:

#建立儲存過程,資料庫名test,表名student

create

procedure myinst(n int

)begin

declare i int

default0;

set autocommit =0;

repeat

set i = i +1;

insert

into student(id,name)

values

(i,'xiaoli');

until i= n end

repeat

;commit

;set autocommit =1;

end#呼叫儲存過程,n為建立資料條數

call myinst(

1000000);

#一百萬條:45s

儲存過程需要注意的事項:

1、引數如果不顯式指定「in」、「out」、「inout」,則預設為「in」。習慣上,對於是「in」的引數,我們都不會顯式指定。多個引數用』,'分割。

2、mysql 儲存過程名字後面的「()」是必須的,即使沒有乙個引數,也需要「()」。

3、mysql 儲存過程引數,不能在引數名稱前加「@」,如:「@a int」。

4、 mysql 儲存過程中的變數,不需要在變數名字前加「@」,雖然 mysql 客戶端使用者變數要加個「@」。

5、mysql 儲存過程的引數不能指定預設值。

6、mysql 儲存過程不需要在procedure body 前面加「as」。而sql server 儲存過程必須加「as」 關鍵字。

7、如果 mysql 儲存過程中包含多條mysql 語句,則需要begin end 關鍵字。

8、不能在 mysql 儲存過程中使用「return」 關鍵字。

9、呼叫 mysql 儲存過程時候,需要在過程名字後面加「()」,即使沒有乙個引數,也需要「()」

2、 explain關鍵字explain屬性:

id:select查詢的序列號

select_type:select查詢的型別,主要是區別普通查詢和聯合查詢、子查詢之類的複雜查詢

table: 輸出的行所引用的表

type:

聯合查詢所使用的型別,type顯示的是訪問型別,是較為重要的乙個指標,結果值從好到壞依次是:

system > const > eq_ref > ref >fulltext > ref_or_null > index_merge > unique_subquery >index_subquery > range > index > all

一般來說,得保證查詢至少達到range級別,最好能達到ref。

type=const表示通過索引一次就找到了;

type=all,表示為全表掃瞄;

type=ref,因為這時認為是多個匹配行,在聯合查詢中,一般為ref;

key:

顯示mysql實際決定使用的鍵。如果沒有索引被選擇,鍵是null。

key=primary的話,表示使用了主鍵;

key=null表示沒用到索引。

possible_keys:

指出mysql能使用哪個索引在該表中找到行。如果是空的,沒有相關的索引。這時要提高效能,可通過檢驗where子句,看是否引用某些字段,或者檢查字段不是適合索引。

key_len:

顯示mysql決定使用的鍵長度。如果鍵是null,長度就是null。文件提示特別注意這個值可以得出乙個多重主鍵裡mysql實際使用了哪一部分。

ref:

顯示哪個欄位或常數與key一起被使用。

rows:

這個數表示mysql要遍歷多少資料才能找到,在innodb上是不準確的。

extra:

如果是only index,這意味著資訊只用索引樹中的資訊檢索出的,這比掃瞄整個表要快。

如果是where used,就是使用上了where限制。

如果是impossible where 表示用不著where,一般就是沒查出來啥。

示例:#student表-一百萬條測試資料

explain select * from student;//麼有建立索引,沒有使用索引,如果主鍵建立索引,*查詢預設使用主鍵索引
2) explain select id from student;//id作為索引

3) select * from student where id=100;

4) explain select id from studentwhere name like 『%xiaoli%』;

explain select id from student where namelike 『xiaoli%』;

explain select id from student where namelike 『%xiaoli』;

explain select id from student where name=『xiaoli』

//加入where查詢,以上四條執行索引均失效

5) explain select id from student order by id;

6) explain select * from studentorder by id;

7) explain select id from studentorder by name;//以name排序,則id索引失效

8) explain select * from studentgroup by name order by id;//以name分組,則id索引失效

9) explain select u.id from u_useru ,u_user_role ur where u.id=ur.user_id;

//關聯表u_user_role沒有建立索引,u_user表索引(unique)為主鍵id

//同樣的sql,關聯表u_user_role用字段user_id和role_id建立聯合索引(unique)

列』id』在字段列表中重複,其實就是兩張表有相同的字段,但是使用時表字段的名稱前沒有加表名,導致指代不明

問題sql: explain select id from u_user u ,u_user_role urwhere u.id=ur.user_id

修正sql:explain select u.id fromu_user u ,u_user_role ur where u.id=ur.user_id

10) explain select count() fromstudent;//看出count()用了主鍵id做索引

11) explain select count(id) from student;//執行結果同上,直接使用主鍵id做索引

12) explain select count(name) fromstudent;//name為表student中非主鍵字段,可見count(name)沒有用到索引

MySQL 優化之 EXPLAIN 關鍵字

mysql查詢優化之explain的深入解析 首先執行如下的 sql 語句 create table ifnot exists article id int 10 unsigned not null auto increment,author id int 10 unsigned not null,...

Mysql之EXPLAIN關鍵字學習筆記

explain是什麼?使用explain關鍵字可以模擬優化器執行sql查詢語句,從而知道mysql是如何處理你的sql語句的。分析你的查詢與或是表結構的效能瓶頸。explain的如何使用?explain的用法比較簡單,只要要查詢語句前面加上explain即可 1explain select from...

MySQL 中 explain關鍵字

select 查詢的序列號,包含一組數字,表示查詢中執行 select 子句或操作表的順序。三種情況 id 相同 執行順序由上而下 from t1,t2,t3 where t1.id t2.id and t1.id t3.id and t1.other column from t2 where id...