mysql use index用法_MySQL中USE INDEX 和 FORCE INDEX

问题

在一次生产环境排查性能问题时, 发现有个请求在一些用户的数据量比较大的情况下, 最高耗时差不多要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去使用这个索引. 如果用不上, 就全表. 如果能用上, 就一定会使用该索引.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441353.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【AtCoder - 4244 】AtCoder Express 2 (区间dp 或 暴力枚举,思维)

题干&#xff1a; In Takahashi Kingdom, there is a east-west railroad and N cities along it, numbered 1, 2, 3, ..., N from west to east. A company called AtCoder Express possesses Mtrains, and the train i runs from City Li to City Ri (it is possible that L…

【HDU - 3068】最长回文(Manacher算法,马拉车算法求最长回文子串)

题干&#xff1a; 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度. 回文就是正反读都是一样的字符串,如aba, abba等 Input 输入有多组case,不超过120组,每组输入为一行小写英文字符a,b,c...y,z组成的字符串S 两组case之间由空行隔开(该空行不用…

java程序员面试需要英语吗_Java程序员和高级程序员面试30题(英语)

Java程序员和高级程序员面试30题(英语)* Q1. How could Java classes direct program messages to the system console, but error messages, say to a file?A. The class System has a variable out that represents the standard output, and the variable err that represe…

【HDU - 1867 】A + B for you again(KMP,next数组应用)

题干&#xff1a; Generally speaking, there are a lot of problems about strings processing. Now you encounter another such problem. If you get two strings, such as “asdf” and “sdfg”, the result of the addition between them is “asdfg”, for “sdf” is …

wifisetting.java_Wifi 笔记 | 启动流程

csd&#xff1a;csdn_of_coder/article/details/51541094aosp: Android OAndroid网络各个模式中&#xff0c;Wifi应该是目前最常用的一种网络方式了&#xff1b;下面就简单介绍下Android中Wifi的启动流程。当我在Setting菜单里点击打开Wifi时&#xff0c;调用的入口函数是WifiM…

【CodeForces - 589F】Gourmet and Banquet (贪心,思维,二分)

题干&#xff1a; A gourmet came into the banquet hall, where the cooks suggested n dishes for guests. The gourmet knows the schedule: when each of the dishes will be served. For i-th of the dishes he knows two integer moments in time ai and bi (in second…

java 调用动态链接库_JAVA技巧:JNative调用动态链接库问题(SOS)

动态链接库的方法如下&#xff1a;__declspec(dllexport) ret __stdcall rLachTran(const char *pc_trancode,const char *pc_clicode,const char *pc_orgcode,const char *pc_ttycode,const int i_brandid,const char *pc_reqstamp,const int i_reqseqno,const char *pc_svrip…

SPFA算法模板

简单的一个模板吧&#xff0c;留个底。如果要判负环的话&#xff0c;需要加一个cnt数组记录入队次数就可以了。 void spfa(int st) {memset(dis,INF,sizeof dis);queue<int> q;q.push(st);dis[st]0;vis[st]1;while(!q.empty()) {int cur q.front();q.pop();vis[cur]0;i…

js和php能生成一样的随机数_JavaScript_JS生成某个范围的随机数【四种情况详解】,前言: JS没有现成的函数,能 - phpStudy...

JS生成某个范围的随机数【四种情况详解】前言&#xff1a;JS没有现成的函数&#xff0c;能够直接生成指定范围的随机数。但是它有个函数&#xff1a;Math.random() 这个函数可以生成 [0,1) 的一个随机数。利用它&#xff0c;我们就可以生成指定范围内的随机数。而涉及范围的话…

【POJ - 3347 】Kadj Squares (计算几何,思维 或 扫描线)

题干&#xff1a; In this problem, you are given a sequence S1, S2, ..., Sn of squares of different sizes. The sides of the squares are integer numbers. We locate the squares on the positive x-y quarter of the plane, such that their sides make 45 degrees w…

按钮开关java代码,Android自定义实现开关按钮代码

我们在应用中经常看到一些选择开关状态的配置文件&#xff0c;做项目的时候用的是android的Switch控件&#xff0c;但是感觉好丑的样子子个人认为还是自定义的比较好&#xff0c;先上个效果图&#xff1a;实现过程&#xff1a;1.准备开关不同状态的两张图片放入drawable中。2.x…

java创建的zip没写入权限,java中的zip创建错误

我正在使用ZipOutputStream,FileOutputStream和FileInputStream.首先我创建了一个包含一个文件的文件夹它成功创建了.然后我尝试创建zip文件.动态地,它首次正确创建文件,但第二次,第三次在打开文件时出错.Error: zip [path/././file.zip] Cannot open The process cannot acces…

【qduoj - 夏季学期创新题】最长公共子串(水题暴力枚举,不是LCS啊)

题干&#xff1a; 描述 编写一个程序&#xff0c;求两个字符串的最长公共子串。输出两个字符串的长度&#xff0c;输出他们的最长公共子串及子串长度。如果有多个最长公共子串请输出在第一个字符串中先出现的那一个。 特别注意公共子串中可能包含有空格&#xff0c;但不计回车…

向量合并 matlab,MATLAB追加向量

如果有两个行向量 r1 和 r2 这两个行向量中各有 n 和 m 个元素&#xff0c;现在创建行向量 r 并将n和m个元素都放在行向量 r 中&#xff0c;通过附加这些载体&#xff0c;编写&#xff1a;r [r1,r2]通过追加这两个向量&#xff0c;向量r2的&#xff0c;也可以建立一个矩阵R&am…

*【CodeForces - 202C 】Clear Symmetry (思维,找规律,特判)

题干&#xff1a; Consider some square matrix A with side n consisting of zeros and ones. There are nrows numbered from 1 to n from top to bottom and n columns numbered from 1 to n from left to right in this matrix. Well denote the element of the matrix wh…

matlab将模型解封装,模型保护 - MATLAB Simulink - MathWorks 中国

当您要与第三方共享模型而又不能泄露知识产权时&#xff0c;请对模型进行保护。Test your protected model by comparing it to the original model.Attach a digital signature to your protected model.Files to include in the protected model package.Specify a post-proc…

*【CodeForces - 768B】Code For 1 (分治策略,模拟二分思想,模拟线段树思想)

题干&#xff1a; Jon fought bravely to rescue the wildlings who were attacked by the white-walkers at Hardhome. On his arrival, Sam tells him that he wants to go to Oldtown to train at the Citadel to become a maester, so he can return and take the decease…

matlab 自适应噪声对消,基于Matlab的RLS自适应语音噪声对消系统的设计与实现

基于Matlab 的R LS 自适应语音噪声对消系统的设计与实现①肖 哲(湖南工业大学科技学院, 湖南株洲 412008)摘 要:自适应信号处理的理论和技术经过40多年的发展和完善,已逐渐成为人们常用的语音去噪技术.而Matlab 的出现又为其提供了更为方便快捷的方法来对语音信号进行消噪处…

【qduoj - 142】 多重背包(0-1背包的另类处理,dp)

题干&#xff1a; ycb的ACM进阶之路 Description ycb是个天资聪颖的孩子&#xff0c;他的梦想是成为世界上最伟大的ACMer。为此&#xff0c;他想拜附近最有威望的dalao为师。dalao为了判断他的资质&#xff0c;给他出了一个难题。dalao把他带到一个到处都是题的oj里对他说&am…

python数字类型怎么学,python的数字类型学习之数据类型

1、在python中&#xff0c;数字并不是一个真正的对象类型&#xff0c;而是一组类似类型的分类。它支持通常的数字类型&#xff0c;还能够可以通过常量直接创建数字&#xff0c;还可以处理数字表达式。2、数字常量&#xff1a;(1)整数和浮点数常量(2)16进制、8进制、2进制常量(3…