问题

在一次生产环境排查性能问题时, 发现有个请求在一些用户的数据量比较大的情况下, 最高耗时差不多要3s. 而且还是一个轮询的请求.

原因

在排查问题时, 定位到是执行某条SQL时在用户的数据比较大的情况下, SQL执行耗时要1.5s.

mysql> SELECT count(1)

-> FROM

-> cc_session cs

-> LEFT JOIN users_platform cp ON cs.user_id = cp.user_id

-> AND cs.to_openid = cp.open_id

-> WHERE

-> cs.`status` = 0

-> AND cs.user_id = 219

-> AND cs.agent_user_id = 219

-> AND cs.create_time < DATE_SUB(now(), INTERVAL 10 MINUTE)

-> AND cp.cc_open = 1;

+----------+

| count(1) |

+----------+

| 0 |

+----------+

1 row in set (1.50 sec)

它的执行计划如下:

mysql> explain SELECT count(1) FROM cc_session cs LEFT JOIN users_platform cp ON cs.user_id = cp.user_id AND cs.to_openid = cp.open_id WHERE cs.`status` = 0 AND cs.user_id = 219 AND cs.agent_user_id = 219 AND cs.create_time < DATE_SUB(now(), INTERVAL 10 MINUTE) AND cp.cc_open = 1;

+----+-------------+-------+------+---------------------------------------+---------------------+---------+------------------------+------+------------------------------------+

| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |

+----+-------------+-------+------+---------------------------------------+---------------------+---------+------------------------+------+------------------------------------+

| 1 | SIMPLE | cp | ref | uid_opid | uid_opid | 4 | const | 50 | Using index condition; Using where |

| 1 | SIMPLE | cs | ref | id_from_to_close_uq,idx_user_agent_id | id_from_to_close_uq | 194 | uniweibo_v2.cp.open_id | 127 | Using index condition; Using where |

+----+-------------+-------+------+---------------------------------------+---------------------+---------+------------------------+------+------------------------------------+

2 rows in set (0.00 sec)

两张表的索引如下:

mysql> show index from cc_session;

+------------+------------+---------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |

+------------+------------+---------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

| cc_session | 0 | PRIMARY | 1 | id | A | 3279492 | NULL | NULL | | BTREE | | |

| cc_session | 0 | id_from_to_close_uq | 1 | to_openid | A | 25822 | NULL | NULL | | BTREE | | |

| cc_session | 0 | id_from_to_close_uq | 2 | from_openid | A | 3279492 | NULL | NULL | | BTREE | | |

| cc_session | 0 | id_from_to_close_uq | 3 | closed_time | A | 3279492 | NULL | NULL | | BTREE | | |

| cc_session | 1 | idx_user_agent_id | 1 | user_id | A | 513 | NULL | NULL | | BTREE | | |

| cc_session | 1 | idx_user_agent_id | 2 | agent_user_id | A | 1886 | NULL | NULL | | BTREE | | |

+------------+------------+---------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

6 rows in set (0.00 sec)

mysql> show index from users_platform;

+----------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |

+----------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

| users_platform | 0 | PRIMARY | 1 | id | A | 373 | NULL | NULL | | BTREE | | |

| users_platform | 1 | uid_opid | 1 | user_id | A | 373 | NULL | NULL | | BTREE | | |

| users_platform | 1 | uid_opid | 2 | open_id | A | 373 | NULL | NULL | | BTREE | | |

+----------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

3 rows in set (0.00 sec)

mysql>

由执行计划可知,它分别使用了cc_session表的id_from_to_close_uq索引, 和users_platform表中的uid_opid索引.

使用 use index 建议MySQL使用其他索引

修改之后的SQL如下:

mysql> SELECT count(1)

-> FROM

-> cc_session cs use index (idx_user_agent_id)

-> LEFT JOIN users_platform cp use INDEX (uid_opid)

-> ON cs.user_id = cp.user_id

-> AND cs.to_openid = cp.open_id

-> WHERE

-> cs.`status` = 0

-> AND cs.user_id = 219

-> AND cs.agent_user_id = 219

-> AND cs.create_time < DATE_SUB(now(), INTERVAL 10 MINUTE)

-> AND cp.cc_open = 1;

+----------+

| count(1) |

+----------+

| 0 |

+----------+

1 row in set (0.01 sec)

mysql>

耗时从1.5秒,降低到0.01秒

执行计划如下:

mysql> explain SELECT count(1)

-> FROM

-> cc_session cs use index (idx_user_agent_id)

-> LEFT JOIN users_platform cp use INDEX (uid_opid)

-> ON cs.user_id = cp.user_id

-> AND cs.to_openid = cp.open_id

-> WHERE

-> cs.`status` = 0

-> AND cs.user_id = 219

-> AND cs.agent_user_id = 219

-> AND cs.create_time < DATE_SUB(now(), INTERVAL 10 MINUTE)

-> AND cp.cc_open = 1;

+----+-------------+-------+------+-------------------+-------------------+---------+--------------------------------+-------+------------------------------------+

| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |

+----+-------------+-------+------+-------------------+-------------------+---------+--------------------------------+-------+------------------------------------+

| 1 | SIMPLE | cs | ref | idx_user_agent_id | idx_user_agent_id | 8 | const,const | 22966 | Using where |

| 1 | SIMPLE | cp | ref | uid_opid | uid_opid | 180 | const,uniweibo_v2.cs.to_openid | 1 | Using index condition; Using where |

+----+-------------+-------+------+-------------------+-------------------+---------+--------------------------------+-------+------------------------------------+

2 rows in set (0.00 sec)

mysql>

use index 和 force index

use index : 是建议MySQL去使用这个索引.最后到底是用不用, 还是由MySQL来决定. 如果MySQL还是觉得全表扫描来得快, 那即使是有索引, 它还是会使用全表扫描.

force index : 是强制MySQL去使用这个索引. 如果用不上, 就全表. 如果能用上, 就一定会使用该索引.

mysql use index用法_MySQL中USE INDEX 和 FORCE INDEX相关推荐

  1. mysql 枚举 enum用法_mysql中的枚举类型ENUM的用法:

    mysql中的枚举类型ENUM的用法: (2010-06-18 13:44:13) mysql中的枚举类型ENUM的用法: mysql中的枚举类型ENUM是一个字符串对象,它的值是自表创建时在列规定中 ...

  2. mysql if exists用法_MySQL中EXISTS的用法

    比如在Northwind数据库中有一个查询为 SELECT c.CustomerId,CompanyName FROM Customers cWHERE EXISTS(SELECT OrderID F ...

  3. MySQL里 unique 用法_mysql中unique key中在查询中的使用

    1.建表语句: sql">CREATE TABLE `employees` ( `emp_no` int(11) NOT NULL,`birth_date` date NOT NULL ...

  4. mysql+rownumber的用法_mysql中如何实现row_number

    创建一个学生表,插入10条数据CREATE TABLE student (id int, name varchar(20), score integer); INSERT INTO student V ...

  5. mysql获取当月最后一天_mysql中获取本月第一天、本月最后一天、上月第一天、上月最后一天

    mysql获取当月最后一天_mysql中获取本月第一天.本月最后一天.上月第一天.上月最后一天等等 转自: https://blog.csdn.net/min996358312/article/det ...

  6. mysql hint 索引倒序_MySQL中的索引提示Index Hint

    MySQL数据库支持索引提示(INDEX HINT)显式的高速优化器使用了哪个索引.以下是可能需要用到INDEX HINT的情况 a)MySQL数据库的优化器错误的选择了某个索引,导致SQL运行很慢. ...

  7. mysql use index用法_mysql use index、ignore index、force index用法

    原创你去了哪里 最后发布于2019-10-18 14:05:48 阅读数 121  收藏 展开 1:use index:在你查询语句表名的后面,添加use index来提供你希望mysql去参考的索引 ...

  8. mysql range用法_MySQL中Explain的用法总结(详细)

    本篇文章给大家带来的内容是关于MySQL中Explain的用法总结(详细),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 执行计划(query Execution plan) 语法e ...

  9. mysql添加临时索引_mysql 中添加索引的三种方法

    在mysql中有多种索引,有普通索引,全文索引,唯一索引,多列索引,小伙伴们可以通过不同的应用场景来进行索引的新建,在此列出三种新建索引的方法 mysql 中添加索引的三种方法 1.1 新建表中添加索 ...

最新文章

  1. Sonya and Informatics
  2. 安装好hadoop集群后,报错如下n org.apache.hadoop.ipc.RemoteException(java.io.IOException): File /data/hadoop-roo
  3. 也可以改为while(input[0])或while(cininput[0])
  4. SpringCloud Ribbon(一)之自定义负载均衡器ILoadBalancer
  5. Apollo进阶课程㉟丨Apollo ROS原理—4
  6. c语言程序申请管理员权限,vc 软件 要管理员运行 vc 管理员权限运行
  7. 【转】史上最简单的软件破解——5行脚本代码完美破解99%的过期软件
  8. lwj_C#_建立一个数学MathTool类包含的方法
  9. 37天熟练掌握百度竞价推广系列视频教程
  10. 【C语言】之实现查找重复元素
  11. 2020-10 补丁日:Oracle多个产品高危漏洞安全风险通告
  12. vue使用百度地图(BMap)去掉百度地图的标志。
  13. VMware未能启动虚拟机解决方法
  14. Java中csv文件读写分析
  15. 物联网试点炼狱阶段就此结束了吗?
  16. [转载]国家天文大地网
  17. linux系统装fluent没有界面,Linux上安装ANSYS后的一些坑(未完待续)
  18. 按键精灵 识别图片形状_精灵形状的2D世界建筑物简介
  19. XP系统测试显示器软件在哪,WinXP系统下如何检测显示器白点
  20. 倡导国稻种芯·中国水稻节 万祥军:稻作文化中国农民丰收节

热门文章

  1. 没有可用软件包 jenkins。_Jenkins分布式构建与并行构建
  2. php7 nts,php7.0.24-nts配置步骤
  3. BOOT INI专辑
  4. mysql 自动备份删除_mysql自动备份删除5天前的备份
  5. centos删除文件夹_等保测评主机安全之centos密码长度
  6. kcbzps oracle_Oracle 11g DRCP配置与使用(上)
  7. python访问mysql_python连接mysql
  8. vb如何测试连接mysql_VB怎么连接访问Access数据库?
  9. java hibernate dto_java – 正确使用Entity和DTO在Restful Web服务中...
  10. 3W+字的设计模式手册