MYSQL 字符集不同引起的join無法走索引
主要是分析《 》中案例的原因,以此記錄下:
再仔細往上回顧。
在以上關聯對sql進行轉換時,可以發現了以下將s表的deptid 轉換成utf8mb4 的情況,即是說,兩個表在left join時存在關聯欄位deptid 字符集不同的情況。
on((`SSS`.`d`.`Deptid` = convert(`ppp`.`s`.`Deptid` using utf8mb4))))
細查發現d表的deptid欄位是utf8mb4,但是s表的deptid欄位是utf8。
而根據經驗,兩邊關聯時欄位字符集不一確實會導致無法走索引,因為這裡是發生了隱式轉換了。此時s表上的索引便無法生效。
此時我有個疑問是當以上將條件d.DEPTID = '00001111' 換成s.DEPTID = '00001111',其可以選擇了索引,當此時字符集不同的情況仍然存在,發現以上選擇d表的主鍵的原因是clustered_pk_chosen_by_heuristics。因為是主鍵而選擇?這個便不是很理解了
"considered_access_paths": [
{
"access_type": "eq_ref",
"index": "PRIMARY",
"rows": 1,
"cost": 1.2,
"chosen": true,
"cause": "clustered_pk_chosen_by_heuristics"
},
在修改字符集統一為utf8mb4後就正常了:
+----+-------------+-------+------------+-------+----------------------------+-----------------+---------+-------+------+----------+-------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+-------+----------------------------+-----------------+---------+-------+------+----------+-------+ | 1 | SIMPLE | d | NULL | const | PRIMARY,INDEX_DEPARTMENT_5 | PRIMARY | 130 | const | 1 | 100.00 | NULL | | 1 | SIMPLE | s | NULL | ref | IND_SHOP_DEPTID | IND_SHOP_DEPTID | 131 | const | 1 | 100.00 | NULL | +----+-------------+-------+------------+-------+----------------------------+-----------------+---------+-------+------+----------+-------+
"ref_optimizer_key_uses": [ { "table": "`sss`.`department` `d`", "field": "Deptid", "equals": "'00001111'", "null_rejecting": false }, { "table": "`sss`.`department` `d`", "field": "Deptid", "equals": "'00001111'", "null_rejecting": false }, { "table": "`ppp`.`shop` `s`", "field": "Deptid", "equals": "`sss`.`d`.`Deptid`", "null_rejecting": false } ] /* ref_optimizer_key_uses */ }, { "rows_estimation": [ { "table": "`sss`.`department` `d`", "rows": 1, "cost": 1, "table_type": "const", "empty": false }, { "table": "`ppp`.`shop` `s`", "range_analysis": { "table_scan": { "rows": 998690, "cost": 207849 } /* table_scan */, "potential_range_indexes": [ { "index": "PRIMARY", "usable": false, "cause": "not_applicable" }, { "index": "IND_SHOP_DEPTID", "usable": true, "key_parts": [ "Deptid", "Shopid" ] /* key_parts */ }, { "index": "IND_SHOP_DOMAIN", "usable": false, "cause": "not_applicable" } ] /* potential_range_indexes */, "setup_range_conditions": [ ] /* setup_range_conditions */, "group_index_range": { "chosen": false, "cause": "not_single_table" } /* group_index_range */, "analyzing_range_alternatives": { "range_scan_alternatives": [ { "index": "IND_SHOP_DEPTID", "ranges": [ "00001111 <= Deptid <= 00001111" ] /* ranges */, "index_dives_for_eq_ranges": true, "rowid_ordered": true, "using_mrr": false, "index_only": false, "rows": 1, "cost": 2.21, "chosen": true } ] /* range_scan_alternatives */, "analyzing_roworder_intersect": { "usable": false, "cause": "too_few_roworder_scans" } /* analyzing_roworder_intersect */ } /* analyzing_range_alternatives */, "chosen_range_access_summary": { "range_access_plan": { "type": "range_scan", "index": "IND_SHOP_DEPTID", "rows": 1, "ranges": [ "00001111 <= Deptid <= 00001111" ] /* ranges */ } /* range_access_plan */, "rows_for_plan": 1, "cost_for_plan": 2.21, "chosen": true } /* chosen_range_access_summary */ } /* range_analysis */ } ] /* rows_estimation */ }, { "considered_execution_plans": [ { "plan_prefix": [ "`sss`.`department` `d`" ] /* plan_prefix */, "table": "`ppp`.`shop` `s`", "best_access_path": { "considered_access_paths": [ { "access_type": "ref", "index": "IND_SHOP_DEPTID", "rows": 1, "cost": 1.2, "chosen": true }, { "access_type": "range", "range_details": { "used_index": "IND_SHOP_DEPTID" } /* range_details */, "chosen": false, "cause": "heuristic_index_cheaper" } ] /* considered_access_paths */ } /* best_access_path */, "condition_filtering_pct": 100, "rows_for_plan": 1, "cost_for_plan": 1.2, "chosen": true } ] /* considered_execution_plans */ },
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29863023/viewspace-2565308/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL Join語法MySql
- MySQL 不同版本預設字符集MySql
- Mysql實現全外部連線(mysql無法使用full join的解決辦法)MySql
- 六條幹貨幫你的MySQL索引起飛MySql索引
- mysql update join,insert select 語法MySql
- 密碼過期引起的ssh無法登陸密碼
- 不同字符集倒庫的方法
- MySQL LEFT JOIN/ INNER JOIN/RIGHT JOINMySql
- MySQL(12)---紀錄一次left join一對多關係而引起的BUGMySql
- Mysql join 的本質,Nested Loop Join 和 Hash JoinMySqlOOP
- MySQL JOIN的使用MySql
- mysql中的left join、right join 、inner join的詳細用法MySql
- mysql left join轉inner joinMySql
- mysql刪除主鍵索引,刪除索引語法MySql索引
- MySQL字符集MySql
- MySQL與SQL的觸發器的不同寫法MySql觸發器
- mysql + left joinMySql
- SQL語句中不同的連線JOIN及SQL中join的各種用法SQL
- MySQL Join原理分析(緩衝塊巢狀與索引巢狀迴圈)MySql巢狀索引
- powershell無法修改字符集,非修改登錄檔修改powershell的方法
- Inner join 寫法
- MySQL的索引MySql索引
- 聊一聊MySQL的字符集MySql
- 本地無法連線Mysql的原因MySql
- MySQL Linux伺服器快照克隆引起的binlog日誌無法正常刪除導致檔案系統滿MySqlLinux伺服器
- MySQL 合併查詢join 查詢出的不同列合併到一個表中MySql
- ORACLE 12c索引分裂引起的會話夯Oracle索引會話
- MySQL 的 join 功能弱爆了?MySql
- mysql字符集說明MySql
- 數倉工具—Hive語法之map join、reduce join、smb join(8)Hive
- MySQL無法識別中文MySql
- conflucen引起mysql奔潰MySql
- mysql 字符集造成的效能問題MySql
- 為什麼索引無法使用is null和is not null索引Null
- MySQL的索引分析MySql索引
- mysql的left join和inner join的效率對比,以及如何優化MySql優化
- 【MySQL】LEFT JOIN 踩坑MySql
- mysql佔用CPU過高的解決辦法(新增索引)MySql索引