MySQL 因資料型別轉換導致執行計劃使用低效索引
檢視錶的索引情況
mysql> show keys from who_goods; +-----------+------------+-------------------------+--------------+-------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | +-----------+------------+-------------------------+--------------+-------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | who_goods | 0 | PRIMARY | 1 | goods_id | A | 68442 | NULL | NULL | | BTREE | | | | who_goods | 1 | idx_del_sale_cat_gid | 1 | is_delete | A | 4 | NULL | NULL | | BTREE | | | | who_goods | 1 | idx_del_sale_cat_gid | 2 | is_on_sale | A | 8 | NULL | NULL | | BTREE | | | | who_goods | 1 | idx_del_sale_cat_gid | 3 | cat_id | A | 551 | NULL | NULL | | BTREE | | | | who_goods | 1 | idx_del_sale_cat_gid | 4 | goods_id | A | 68442 | NULL | NULL | | BTREE | | | | who_goods | 1 | idx_del_sale_cat_update | 1 | is_delete | A | 4 | NULL | NULL | | BTREE | | | | who_goods | 1 | idx_del_sale_cat_update | 2 | is_on_sale | A | 8 | NULL | NULL | | BTREE | | | | who_goods | 1 | idx_del_sale_cat_update | 3 | cat_id | A | 551 | NULL | NULL | | BTREE | | | | who_goods | 1 | idx_del_sale_cat_update | 4 | last_update | A | 68442 | NULL | NULL | | BTREE | | | | who_goods | 1 | goods_sn | 1 | goods_sn | A | 4888 | 7 | NULL | | BTREE | | | | who_goods | 1 | add_time | 1 | add_time | A | 68442 | NULL | NULL | | BTREE | | | | who_goods | 1 | last_update | 1 | last_update | A | 68442 | NULL | NULL | | BTREE | | | | who_goods | 1 | idx_provider_code | 1 | provider_code | A | 786 | 3 | NULL | | BTREE | | | | who_goods | 1 | inx_code | 1 | goods_search_code | A | 1801 | NULL | NULL | | BTREE | | | +-----------+------------+-------------------------+--------------+-------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 14 rows in set (0.00 sec)
檢視語句的執行計劃
發現SQL沒有走主鍵索引,而是走了一個低效的聯合索引
mysql> explain -> SELECT -> `goods_id`, -> `goods_sn`, -> `goods_name`, -> `market_price`, -> `shop_price`, -> `promote_price`, -> `promote_start_date`, -> `promote_end_date`, -> `goods_thumb`, -> `goods_thumb_small`, -> `goods_img`, -> `goods_brief`, -> `is_new`, -> `is_best`, -> `is_stock`, -> `is_hot`, -> `is_promote`, -> `is_presale`, -> `goods_weight`, -> `cat_id`, -> `is_on_sale`, -> `last_sold_out_reason`, -> `is_forever_offsale` -> FROM -> (`who_goods`) -> WHERE -> `goods_id` IN ( -> 1120872, -> 2875488, -> 2562654, -> 697450, -> 2776492, -> 663476, -> 629658, -> 549306, -> '312946', -> '845004', -> '3103382', -> '3368908', -> '929186', -> '697454' -> ) -> AND `is_delete` = 0 -> AND `is_on_sale` = 1\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: who_goods type: ref possible_keys: PRIMARY,idx_del_sale_cat_gid,idx_del_sale_cat_update key: idx_del_sale_cat_gid key_len: 2 ref: const,const rows: 34221 Extra: Using index condition 1 row in set (0.00 sec)
將IN中的字串改成數字,執行計劃走了主鍵
mysql> explain -> SELECT -> `goods_id`, -> `goods_sn`, -> `goods_name`, -> `market_price`, -> `shop_price`, -> `promote_price`, -> `promote_start_date`, -> `promote_end_date`, -> `goods_thumb`, -> `goods_thumb_small`, -> `goods_img`, -> `goods_brief`, -> `is_new`, -> `is_best`, -> `is_stock`, -> `is_hot`, -> `is_promote`, -> `is_presale`, -> `goods_weight`, -> `cat_id`, -> `is_on_sale`, -> `last_sold_out_reason`, -> `is_forever_offsale` -> FROM -> (`who_goods`) -> WHERE -> `goods_id` IN ( -> 1120872, -> 2875488, -> 2562654, -> 697450, -> 2776492, -> 663476, -> 629658, -> 549306, -> 312946, -> 845004, -> 3103382, -> 3368908, -> 929186, -> 697454 -> ) -> AND `is_delete` = 0 -> AND `is_on_sale` = 1\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: who_goods type: range possible_keys: PRIMARY,idx_del_sale_cat_gid,idx_del_sale_cat_update key: PRIMARY key_len: 3 ref: NULL rows: 14 Extra: Using where 1 row in set (0.00 sec)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26506993/viewspace-2168522/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Oracle隱式型別轉換導致索引失效Oracle型別索引
- 資料型別隱式轉換導致的阻塞資料型別
- mysql索引和執行計劃MySql索引
- sql最佳化-錯誤強制型別轉換導致索引失效SQL型別索引
- C語言中,四則運算導致資料型別的轉換C語言資料型別
- mysql 執行計劃索引分析筆記MySql索引筆記
- 【MySQL】MySQL的執行計劃及索引優化MySql索引優化
- oracle執行計劃------未走索引,隱式轉換的坑Oracle索引
- 資料型別,型別轉換資料型別
- python 與 Mysql 資料型別轉換PythonMySQL 資料型別
- 【Oracle】-【索引-HINT,執行計劃】-帶HINT的索引執行計劃Oracle索引
- 【隱式轉換】注意隱式轉換將導致索引無法使用索引
- Grant許可權導致執行計劃失效
- Mysql索引型別建立錯誤導致SQL查詢緩慢MySql索引型別
- 資料型別轉換資料型別
- MySQL 5.6因為OOM導致資料庫重啟MySqlOOM資料庫
- 理解索引:MySQL執行計劃詳細介紹索引MySql
- MySQL索引型別一覽 讓MySQL高效執行起來MySql索引型別
- 完美的執行計劃導致的效能問題
- 執行計劃的偏差導致的效能問題
- 建了索引執行計劃會有區別了索引
- Java資料型別及型別轉換Java資料型別
- 3. php資料型別、資料型別轉換PHP資料型別
- JavaScript 資料型別轉換JavaScript資料型別
- 【Java】資料型別轉換Java資料型別
- javascript資料型別轉換JavaScript資料型別
- 資料型別的轉換資料型別
- 避免資料型別轉換資料型別
- 資料型別轉換圖資料型別
- oracle 資料型別轉換Oracle資料型別
- JS資料型別轉換JS資料型別
- 資料型別及轉換資料型別
- VC常用資料型別使用轉換詳解 (轉)資料型別
- MySQL與oracle的資料型別轉換總結MySqlOracle資料型別
- Oracle 索引和執行計劃Oracle索引
- 避免在WHERE條件中,在索引列上進行計算或使用函式,因為這將導致索引不被使用索引函式
- autotrace 和explain plan for可能導致執行計劃錯誤AI
- 執行計劃錯誤導致系統負載高負載