查看MySQL数据存储路径信息

分号不能掉

  1. show variables like '%datadir%';

查询表的每个字段详细信息

  1. SELECT
  2. *
  3. FROM
  4. information_schema.COLUMNS
  5. WHERE
  6. table_name = 'ivr_stay_time_statistic'
  7. AND table_schema = 'ngcc_db'
  8. ORDER BY
  9. ordinal_position

MySQL创建索引后查询效率还是很慢

在10W条数据中如下查询语句很慢

  1. SELECT
  2. t.id,
  3. t.firstCallId,
  4. t.secondCallId,
  5. t.calledNum,
  6. t.callerNum,
  7. t.firstCallTime,
  8. t1.hookNodeId,
  9. t.secondCallTime,
  10. t2.hookNodeId
  11. FROM repeat_call_temp t
  12. LEFT JOIN ivr_node_track_record_list AS t1 ON t.firstCallId = t1.callId
  13. LEFT JOIN ivr_node_track_record_list AS t2 ON t.secondCallId = t2.callId
  14. WHERE t.firstCallTime between '2020-04-14 00:00:00' AND '2020-04-14 23:59:59'

添加 EXPLAIN 关键字模拟Mysql优化器是如何执行SQL查询语句的,从而知道Mysql是如何处理你的SQL语句的。分析你的查询语句或是表结构的性能瓶颈。

  1. EXPLAIN
  2. SELECT
  3. t.id,
  4. t.firstCallId,
  5. t.secondCallId,
  6. t.calledNum,
  7. t.callerNum,
  8. t.firstCallTime,
  9. t1.hookNodeId,
  10. t.secondCallTime,
  11. t2.hookNodeId
  12. FROM repeat_call_temp t
  13. LEFT JOIN ivr_node_track_record_list AS t1 ON t.firstCallId = t1.callId
  14. LEFT JOIN ivr_node_track_record_list AS t2 ON t.secondCallId = t2.callId
  15. WHERE t.firstCallTime between '2020-04-14 00:00:00' AND '2020-04-14 23:59:59'

返回如下内容,可以看到tye列显示的是ALL表示全表扫描没有用到索引
image.png
上述问题可能是因为表中字段的字符集编码不一致导致的。

输入如下语句查看表中的字段信息:

  1. SHOW FULL COLUMNS FROM repeat_call_temp;
  2. SHOW FULL COLUMNS FROM ivr_node_track_record_list;

返回如下信息:
表:repeat_call_temp
image.png
表:ivr_node_track_record_list
image.png
可以看到关联条件中的callId、firstCallId、secondCallId之间的字符编码不同

输入如下语句修改字符编码:

  1. ALTER TABLE ivr_node_track_record_list CHANGE callId callId VARCHAR(64) CHARACTER SET utf8 COLLATE utf8_bin

表:ivr_node_track_record_list 的callId字段的字符集编码已经修改成 utf8_bin
image.png
type从ALL变成了ref ,rows也变小了,重新执行查询语句速度会提升好几倍
image.png