開發反映有個請求sql很慢,須要3s以上。sql以下:java
SELECT tc.customer_name, cb.service_record_id~~~~, tc.customer_code, tc.customer_desc, tc.customer_account_id, tc.phone FROM tur_customer tc INNER JOIN tur_customer_bind cb ON tc.customer_account_id = cb.customer_account_id WHERE cb.employee_account_id = '181' AND tc.is_delete = 0 ORDER BY cb.service_record_id DESC LIMIT 0,10
表結構:sql
CREATE TABLE `tur_customer` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '主鍵', `customer_code` varchar(64) NOT NULL COMMENT '客戶編碼', `customer_name` varchar(255) NOT NULL COMMENT '客戶姓名', `customer_name_hash` varchar(128) DEFAULT NULL COMMENT '客戶姓名Hash', `customer_account_id` varchar(32) NOT NULL COMMENT '賬號系統對應的惟一標識', `real_name_status` int(11) DEFAULT '10' COMMENT '實名制認證狀態: 10 未認證, 20 已實名, 30 已認證', `sex` int(11) DEFAULT '1' COMMENT '性別;1男;0女', `phone` varchar(128) NOT NULL COMMENT '聯繫電話,可能時手機號,也多是座機號碼', PRIMARY KEY (`id`), UNIQUE KEY `uniq_customercode` (`customer_code`) USING BTREE, UNIQUE KEY `uniq_customeraccountid` (`customer_account_id`) USING BTREE, KEY `idx_customernamehash` (`customer_name_hash`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=267031 DEFAULT CHARSET=utf8 COMMENT='客戶基本信息表'; CREATE TABLE `tur_customer_bind` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'id', `customer_account_id` varchar(32) NOT NULL COMMENT '客戶帳號', `employee_account_id` varchar(32) NOT NULL COMMENT '顧問帳號', `employee_account_name` varchar(32) NOT NULL COMMENT '顧問名稱', `service_record_id` int(11) NOT NULL COMMENT '最新服務記錄ID', `bind_time` datetime NOT NULL COMMENT '綁定時間', `store_code` varchar(32) DEFAULT NULL COMMENT '店面編碼', PRIMARY KEY (`id`) USING BTREE, UNIQUE KEY `uniq_account` (`customer_account_id`) USING BTREE COMMENT '客戶帳號惟一索引', KEY `idx_employee_account_id` (`employee_account_id`) USING BTREE COMMENT '顧問帳號索引' ) ENGINE=InnoDB AUTO_INCREMENT=14583 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPACT COMMENT='當前客戶關係表';
分析表的執行計劃以下:oop
+----+-------------+-------+------+--------------------------------------+-------------------------+---------+-------+-------+--------------------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+--------------------------------------+-------------------------+---------+-------+-------+--------------------------------------------------------+ | 1 | SIMPLE | cb | ref | uniq_account,idx_employee_account_id | idx_employee_account_id | 130 | const | 1 | Using index condition; Using temporary; Using filesort | | 1 | SIMPLE | tc | ALL | NULL | NULL | NULL | NULL | 58697 | Using where; Using join buffer (Block Nested Loop) | +----+-------------+-------+------+--------------------------------------+-------------------------+---------+-------+-------+--------------------------------------------------------+
兩表關聯字段tur_customer.customer_account_id和tur_customer_bind.customer_account_id都有索引,且類型都爲varchar,可是索引沒有用到,查看字符集發現一個表是utf8一個是utf8mb4。編碼
兩表的字符集不一樣,會形成沒法使用索引的狀況發生。 查看庫的字符集爲utf8,排序規則爲utf8_general_ci.code
將表tur_customer修改成utf8mb4格式後,該sql的執行時間僅爲0.1s,再次查看執行計劃以下:排序
+----+-------------+-------+--------+--------------------------------------+-------------------------+---------+-----------------------------+------+----------------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+--------+--------------------------------------+-------------------------+---------+-----------------------------+------+----------------------------------------------------+ | 1 | SIMPLE | cb | ref | uniq_account,idx_employee_account_id | idx_employee_account_id | 130 | const | 1 | Using index condition; Using where; Using filesort | | 1 | SIMPLE | tc | eq_ref | uniq_customeraccountid | uniq_customeraccountid | 130 | test.cb.customer_account_id | 1 | Using where | +----+-------------+-------+--------+--------------------------------------+-------------------------+---------+-----------------------------+------+----------------------------------------------------+
報錯信息以下:索引
SQL state [HY000]; error code [1267]; Illegal mix of collations (utf8mb4_general_ci,IMPLICIT) and (utf8mb4_unicode_ci,IMPLICIT) for operation '='; nested exception is java.sql.SQLException: Illegal mix of collations (utf8mb4_general_ci,IMPLICIT) and (utf8mb4_unicode_ci,IMPLICIT) for operation '='"
查了下兩表的 DDL,一個是utf8mb4_unicode_ci,一個是utf8mb4_general_ci,因此致使了這個問題ci
解決方法: 將這兩個表統一改成 utf8mb4_unicode_ci 或者 utf8mb4_general_ci。unicode
若是不修改表結構,能夠在查詢的時候指定等號一邊的字符集編碼:開發