mysql - 基于 WHERE 子句的 SELECT 查询的性能
问题描述
我在session
表中有一些数据,其中包含用户位置以及与用户相关的一些其他数据
会话表由多个列组成,例如
- 身份证-
PRIMARY KEY AUTO-INCREMENT
- session_id -
UNIQUE KEY
(每次都是唯一的,即使用户多次登录,也会生成唯一的) - user_id(此用户 id 对每个用户都是唯一的)
- user_location(保存用户的坐标)
问题陈述
我想知道以下两个查询之间是否存在任何性能差异。
第一次查询
SELECT
user_location
FROM session WHEREsession_id
= 'some-session-id-goes-here';
第二次查询
SELECT
user_location
FROM session WHEREsession_id
= 'some-session-id-goes-here' ANDuser_id
='usome-user-id';
这两个查询都提供了正确的数据,但是如果假设此会话表中存在数百万个数据,是否会对性能产生任何影响。
在较小的集合 上运行这两个查询data < 4000
在.fetching time
解决方案
Normally, when you declare a column with UNIQUE Key
it is indexed by default.
As you have not mentioned if your user_id
is declared unique
or not(you said user_id
is unique, I assume its values are but not defined in your create table
script). So in that sense they will give same performance. But if you index your user_id
too then the case can be different.
When you have multiple column indexed and you are querying using them, you must maintain their index-sequence
after where
clause and your query performance will be better than the current one. Otherwise, the effect will be negative for not maintaining the sequence order.
You can check columns index sequence by
show index from your_table_name
For some more insight, you can check here MySQL: unique field needs to be an index?
推荐阅读
- amazon - 什么是最好的 IMDB REST API?我可以从哪里得到它?
- regex - 使用正则表达式匹配多个条件
- c++ - 打印字节为 longlong
- python - 如何在不中断的情况下运行循环python脚本
- html - 如何在电子邮件正文中正确设置横幅徽标长度和高度
- spring-cloud - 为(Spring)云管道生成 Jenkinsfile
- javascript - 如何在 onClose 中使用日期选择器选项
- java - 确定 TLS 数据包 Java/Android 的数据包大小
- reactjs - 我没有收到来自反应导航推送的上下文
- javascript - 如果我进行了 3 个重复的 GET Ajax 调用,浏览器会等待一个响应还是做出 3 个响应