求助 mysql 查询优化

31 天前
 I2E

表 abc_http_detail_log_2024_04_13 为网络日志表,包含部分字段:

uid int	// 用户 id
account_id int // 子账号 id
flow bingint // 量消耗
ip char // 本机 ip
address varchar // 请求地址

联合索引 idx_uid_account_ip 包含字段 uid, account_id, ip

现在需要查询当日的用户流量使用情况。account_id 可以为 0 或者大于 0 。

表 abc_http_detail_log_2024_04_13 有 2075w 条数据,uid=114708 的数据量最多,有 637w 条。在统计流量消耗时出现上图的情况。如何才能优化查询呢?

982 次点击
所在节点    MySQL
10 条回复
YouZiAndDanJuan
31 天前
分表
I2E
31 天前
有两点疑问:
1 ,为什么加了 account_id > 0 会导致第二条 sql 的索引失效
2 ,为什么加了 limit 索引又会生效
I2E
31 天前
@YouZiAndDanJuan 分表要改很多屎山代码
bianhui
31 天前
无语,你看看字只看一半么,using index 和 using inex condition 是两个东西。百度搜下索引下推。
xlzyxxn
31 天前
@I2E #2 应该有个 uid 、account_id 、flow 的联合索引吧,第二条查询条件 adress 需要回表,uid=114708 占了大部分数据的情况下,mysql 认为不如直接去全表扫描。第三条查询不需要回表。
jshfsym42
30 天前
第二条 SQL 匹配的行数太多了,MySQL 执行器认为不如全盘扫描;可以看看这个。
https://dev.mysql.com/doc/refman/8.0/en/table-scan-avoidance.html
You are using a key with low cardinality (many rows match the key value) through another column. In this case, MySQL assumes that by using the key probably requires many key lookups and that a table scan would be faster.
8355
30 天前
account_id+ 一般索引
你的日志表在查询某一个 id 的时候跳行太多了,加上某 uid 和 account_id orderby 即可。
coderzhangsan
30 天前
能说下 mysql 版本吗?版本不同,可能导致优化的结果也是不同的。
I2E
29 天前
I2E
29 天前
@jshfsym42 应该是这个原因

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1032654

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX