mysql語句效能分析(一)

來源:互聯網
上載者:User

標籤:bsp   sel   type   ifnull   mysql   extra   wms   blog   tab   

1.使用explain語句去查看分析結果

如explain select * from test1 where id=1;

會出現:id  selecttype  table  type possible_keys  key key_len  ref rows  extra各列。

其中,

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

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

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

key=null表示沒用到索引。type=ref,因為這時認為是多個匹配行,在聯集查詢中,一般為REF。

 

explain mysql語句;

eg:

EXPLAINSELECT GoodsSku.sku_id, GoodsSku.barcode, GoodsSku.sku_name, GoodsTypeSpec1.spec_name AS sku_spec1, GoodsTypeSpec2.spec_name AS sku_spec2, GoodsCategory1.cat_name AS catName1, GoodsCategory2.cat_name AS catName2, GoodsCategory3.cat_name AS catName3, GoodsUnit.sale_unit, SellerBase.seller_name, IFNULL((SELECT SUM(WmsBatchItem.sku_num) FROM wms_batch_base AS WmsBatchBase INNER JOIN wms_batch_item AS WmsBatchItem ON WmsBatchItem.batch_id = WmsBatchBase.batch_id WHERE WmsBatchItem.sku_id = GoodsSku.sku_id), 0) AS qichuNum, IFNULL((SELECT SUM(WmsBatchItem.sku_num * WmsBatchItem.sku_price) FROM wms_batch_base AS WmsBatchBase INNER JOIN wms_batch_item AS WmsBatchItem ON WmsBatchItem.batch_id = WmsBatchBase.batch_id WHERE WmsBatchItem.sku_id = GoodsSku.sku_id), 0) AS qichuPriceFROM goods_sku AS GoodsSku INNER JOIN goods_base AS GoodsBase ON GoodsSku.goods_id = GoodsBase.goods_id LEFT JOIN goods_category AS GoodsCategory1 ON GoodsCategory1.cat_id = GoodsBase.cat_id1 LEFT JOIN goods_category AS GoodsCategory2 ON GoodsCategory2.cat_id = GoodsBase.cat_id2 LEFT JOIN goods_category AS GoodsCategory3 ON GoodsCategory3.cat_id = GoodsBase.cat_id3 LEFT JOIN goods_unit AS GoodsUnit ON GoodsUnit.unit_id = GoodsBase.unit_id LEFT JOIN seller_base AS SellerBase ON SellerBase.seller_id = GoodsBase.seller_id LEFT JOIN goods_type_spec AS GoodsTypeSpec1 ON GoodsTypeSpec1.spec_id = GoodsSku.sku_spec1 LEFT JOIN goods_type_spec AS GoodsTypeSpec2 ON GoodsTypeSpec2.spec_id = GoodsSku.sku_spec2;

  

 

 

然後可以根據這個來設定資料表的索引

 

mysql語句效能分析(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.