1. 程式人生 > >Mysql之sql語句優化:explain

Mysql之sql語句優化:explain

explain的列分析

id: 代表select 語句的編號, 如果是連線查詢,表之間是平等關係, select 編號都是1,從1開始. 如果某select中有子查詢,則編號遞增.

mysql> explain select goods_id,goods_name from  goods where goods_id in (sele
ct goods_id from  goods where cat_id=4) \G
*************************** 1. row ***************************
           id: 1
  select_type: PRIMARY
        table:  goods
type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 31 Extra: Using where *************************** 2. row *************************** id: 2 select_type: DEPENDENT SUBQUERY table: goods type: unique_subquery
possible_keys: PRIMARY,cat_id key: PRIMARY key_len: 3 ref: func rows: 1 Extra: Using where 2 rows in set (0.00 sec)

select_type: 查詢型別

  1. simple (不含子查詢)
  2. primary (含子查詢或派生查詢)
  3. subquery (非from子查詢)
  4. derived (from型子查詢)
  5. union
  6. union result

table: 查詢針對的表

有可能是
實際的表名 如select * from t1;
表的別名 如 select * from t2 as tmp;
derived 如from型子查詢時
null 直接計算得結果,不用走表

possible_key: 可能用到的索引

注意: 系統估計可能用的幾個索引,但最終,只能用1個.

key : 最終用的索引.

key_len: 使用的索引的最大長度

type列: 是指查詢的方式, 非常重要,是分析”查資料過程”的重要依據

可能的值
- all: 意味著從表的第1行,往後,逐行做全表掃描.,運氣不好掃描到最後一行.

例: 把goods_name列上的索引去掉, 並根據goods_name來查詢

mysql> explain select goods_name from goods where goods_name='諾基亞N85' \G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: goods
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 31
        Extra: Using where
1 row in set (0.00 sec)
  • index: 比all效能稍好一點,

    通俗的說: all 掃描所有的資料行,相當於data_all index 掃描所有的索引節點,相當於index_all


2種情況可能出現:
1:索引覆蓋的查詢情況下, 能利用上索引,但是又必須全索引掃描.
mysql> explain select goods_id from  goods where goods_id=1 or goods_id+1>20
\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table:  goods
         type: index
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 3
          ref: NULL
         rows: 31
        Extra: Using where; Using index
1 row in set (0.00 sec)

mysql> explain select goods_id,click_count from  goods where goods_id=1 or go
ods_id+1>20 \G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table:  goods
         type: ALL
possible_keys: PRIMARY
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 31
        Extra: Using where
1 row in set (0.00 sec)
2: 是利用索引來進行排序,但取出所有的節點 select goods_id from goods order by goods_id desc; 分析: 沒有加where條件, 就得取所有索引節點,同時,又沒有回行,只取索引節點. 再排序,經過所有索引節點.
mysql> explain select goods_id from  goods order by goods_id asc\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table:  goods
         type: index
possible_keys: NULL
          key: PRIMARY
      key_len: 3
          ref: NULL
         rows: 31
        Extra: Using index
1 row in set (0.00 sec)
  • range: 意思是查詢時,能根據索引做範圍的掃描
mysql> explain select goods_id,goods_name,shop_price from  goods where goods
id >25 \G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table:  goods
         type: range
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 3
          ref: NULL
         rows: 8
        Extra: Using where
1 row in set (0.00 sec)
  • ref 意思是指 通過索引列,可以直接引用到某些資料行
mysql> explain select goods_id,goods_name from  goods where cat_id=4 \G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table:  goods
         type: ref
possible_keys: cat_id
          key: cat_id
      key_len: 2
          ref: const
         rows: 3
        Extra:
1 row in set (0.00 sec)

在這個例子中,通過cat_id索引 指向N行goods資料,來查得結果.

  • eq_ref 是指,通過索引列,直接引用某1行資料
    常見於連線查詢中
mysql> explain select goods_id,shop_price from  goods innert join ecs_catego
y using(cat_id) where goods_id> 25 \G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: innert
         type: range
possible_keys: PRIMARY,cat_id
          key: PRIMARY
      key_len: 3
          ref: NULL
         rows: 8
        Extra: Using where
*************************** 2. row ***************************
           id: 1
  select_type: SIMPLE
        table: ecs_category
         type: eq_ref
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 2
          ref: shop.innert.cat_id
         rows: 1
        Extra: Using index
2 rows in set (0.00 sec)
  • const, system, null 這3個分別指查詢優化到常量級別, 甚至不需要查詢時間.

一般按照主鍵來查詢時,易出現const,system
或者直接查詢某個表示式,不經過表時, 出現NULL

mysql> explain select goods_id,goods_name,click_count from  goods wher
_id=4 \G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table:  goods
         type: const
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 3
          ref: const
         rows: 1
        Extra:
1 row in set (0.00 sec)

mysql> explain select max(goods_id) from  goods \G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: NULL
         type: NULL myisam表的max,min,count在表中優化過,不需要\真正查詢,為NULL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: NULL
        Extra: Select tables optimized away
1 row in set (0.00 sec)

ref列 指連線查詢時, 表之間的欄位引用關係.

mysql> explain select goods_id,cat_name,goods_name from  goods inner join ec
_category using(cat_id) where ecs_category.cat_name='' \G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table:  goods
         type: ALL
possible_keys: cat_id
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 31
        Extra:
*************************** 2. row ***************************
           id: 1
  select_type: SIMPLE
        table: ecs_category
         type: eq_ref
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 2
          ref: shop. goods.cat_id
         rows: 1
        Extra: Using where
2 rows in set (0.00 sec)

rows : 是指估計要掃描多少行.

extra:

  1. index: 是指用到了索引覆蓋,效率非常高
  2. using where 是指光靠索引定位不了,還得where判斷一下
  3. using temporary 是指用上了臨時表, group by 與order by 不同列時,或group by ,order by 別的表的列.
  4. using filesort : 檔案排序(檔案可能在磁碟,也可能在記憶體), (?????

select sum(shop_price) from goods group by cat_id(???? 這句話,用到了臨時表和檔案排序)