# MySQL中OR为何比IN慢？

Original title: Why MySQL OR is slower than IN

> Hacker News story 50676907 · language: zh · submitted 2026-11-28
> Web version: https://zeli.app/zh/story/50676907
> About this feed: https://zeli.app/llms.txt · Agent guide: https://zeli.app/skill.md

▲ 10 · by [millspore](<https://news.ycombinator.com/user?id=millspore>) · 💬 2
[original](<https://www.percona.com/blog/why-mysql-or-is-slower-than-in/>) · [HN discussion](<https://news.ycombinator.com/item?id=50676907>)

## Summary

在MySQL中，看似简单的OR条件查询往往比IN条件慢得多。文章深入剖析了MySQL 8.4内部执行机制：OR条件采用线性搜索，时间复杂度为O(N)，而IN条件利用二分查找，复杂度仅为O(log N)。当OR列表过长时，优化器可能因内存限制放弃范围扫描，转而进行全索引扫描，导致性能呈平方级甚至线性级恶化。通过对比不同访问类型下的成本模型，揭示了为何在大量值匹配场景下，IN是更优选择。

## Key quote

> 由于OR条件的时间复杂度高于IN条件，因此带有OR条件的查询比带有IN条件的查询更慢。

---

## Cite

- Page: https://zeli.app/zh/story/50676907
- Date: 2026-11-28
- Source: percona.com (https://www.percona.com/blog/why-mysql-or-is-slower-than-in/)
- Hacker News: https://news.ycombinator.com/item?id=50676907

## Other languages

- en: https://zeli.app/story/50676907 · markdown: https://zeli.app/story/50676907.md
- ja: https://zeli.app/ja/story/50676907 · markdown: https://zeli.app/ja/story/50676907.md
- ko: https://zeli.app/ko/story/50676907 · markdown: https://zeli.app/ko/story/50676907.md
- de: https://zeli.app/de/story/50676907 · markdown: https://zeli.app/de/story/50676907.md
- es: https://zeli.app/es/story/50676907 · markdown: https://zeli.app/es/story/50676907.md
- ru: https://zeli.app/ru/story/50676907 · markdown: https://zeli.app/ru/story/50676907.md
