[求优化] mysql 百万数据 IN 查询

2020-05-06 10:54:23 +08:00
 jss

##mysql 城市 IN 查询超时

MySql 代码

SELECT * FROM `user_info` WHERE (  `city_id` IN (45757,45967,46044,46126,46288,46473,46642,46769,46919,47078,47119,45758,45762,45786,45811,45822,45839,45850,45870,45877,45892,45905) ) AND `user_info`.`delete_time` IS NULL ORDER BY `id` DESC
7350 次点击
所在节点    程序员
64 条回复
zhangysh1995
2020-05-07 14:23:28 +08:00
@hauzi `范围查询一般都不走索引的`,这是从经验来的嘛?我看文档 https://dev.mysql.com/doc/refman/5.6/en/range-optimization.html 说的是 indexed key 都会做优化?
kanepan19
2020-05-07 17:09:53 +08:00
@zhangysh1995
我这边的场景每天 200 万, 查询 2 天 用 USE INDEX 就全表扫描了,必须 force index
zhangysh1995
2020-05-08 13:53:57 +08:00
@kanepan19 这样的啊,学习了。谢谢回复!
qwwe01
2022-09-13 20:49:41 +08:00
挖坟。。。最近再找类似问题发现有这么个 BUG
https://bugs.mysql.com/bug.php?id=97001

There is an additional case of the same poor optimization, if queries with large IN() lists are used, however I've not been able to generate a reproducible test case that does not require production data and circumstances. The query could be:

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

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

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

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

© 2021 V2EX