跳到正文
原文
Hacker News 首页· millspore·· 5 天前AI 评分42

为什么 MySQL 的 OR 条件比 IN 慢

Why MySQL OR is slower than IN

AI 导读

MySQL 8.4 中 OR 条件被拆成 Item_cond_or 逐项线性比对,复杂度 O(N),而 IN 走二分查找 O(log N)。在约 100 万行表上,34,753 个 OR 条件触发 range 访问耗时 37 秒,多加一个条件越过阈值后涨到 2,907 秒,慢 79 倍。

社媒成稿工作台 · 一键复制

飞书同步推送 · 含数据/日期/出处/启发
核心数据发布日期信源出处独立开发启发
1372
为什么 MySQL 的 OR 条件比 IN 慢

📊 核心数据与事实(2026-11-28):
MySQL 8.4 中 OR 条件被拆成 Item_cond_or 逐项线性比对,复杂度 O(N),而 IN 走二分查找 O(log N)。在约 100 万行表上,34,753 个 OR 条件触发 range 访问耗时 37 秒,多加一个条件越过阈值后涨到 2,907 秒,慢 79 倍。
• 原文实测切片:The same query on tables of 100k, 500k, and 1M rows takes about the same time: Although the number of rows in the table grows 10 times from 100k to 1M, the execution time is still close.
• 原文实测切片:T does not change as N grows, so the cost is linear in N. Linear does not mean cheaper, because T is much larger than N. At N = 40,000 on this table of about 1M rows, range access checks about N²/2 = 0.8 billion terms.
• 原文实测切片:The same 40,000-value OR query with an index scan took 3,406 s on a 1M-row table, 1,752 s on a 500k-row table, and 265 s on a 100k-row table.

💡 对独立开发者的启发:
1/ 供给端造工具的门槛已被 AI 和开源抹平,独立开发者真正的护城河是「找准垂直业务场景 + 做好分发」。
2/ 面对新模型或开源组件先看三件事:许可证是否商用安全、API/推理边际成本降了多少、能不能直接嵌进现有变现工作流。
3/ 别去卷通用开发者轮子,把底层新能力包装成面向具体行业人群(电商、创作者、中小商家)傻瓜式交付结果的产品。

📅 日期:2026-11-28(北京时间)
📰 信息源出处:Hacker News 首页 · millspore(percona.com)
🔗 原文:https://www.percona.com/blog/why-mysql-or-is-slower-than-in/
📡 归档:https://billionare.me/items/oba28hxqymmix7su7cl2szxcd
去 X 发推
每天 08:00 邮件 / 08:30 飞书准点推送一手高信噪比情报与拆解免费订阅简报

来源:Hacker News 首页 · percona.com