<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/why-mysql-or-is-slower-than-in-rwp6gu1c2" -->

---
title: Why MySQL OR is slower than IN | daily.dev
description: MySQL evaluates long chains of OR conditions far less efficiently than equivalent IN lists, because OR checks are a linear scan per row. MySQL turns 'id = 1 OR...
canonical: https://daily.dev/posts/why-mysql-or-is-slower-than-in-rwp6gu1c2
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Why MySQL OR is slower than IN | daily.dev
og:description: MySQL evaluates long chains of OR conditions far less efficiently than equivalent IN lists, because OR checks are a linear scan per row. MySQL turns 'id = 1 OR...
og:url: https://daily.dev/posts/why-mysql-or-is-slower-than-in-rwp6gu1c2
og:image: https://api.daily.dev/og/posts/rWp6gu1c2.png
og:image:alt: Why MySQL OR is slower than IN
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Why MySQL OR is slower than IN

**[Planet MySQL](https://daily.dev/sources/planet-mysql)** · 4 min read · 0 upvotes · 0 comments

## Summary

MySQL evaluates long chains of OR conditions far less efficiently than equivalent IN lists, because OR checks are a linear scan per row. MySQL turns 'id = 1 OR id = 2 OR ...' into an Item_cond_or holding N Item_func_eq elements and checks them one by one, giving O(N) per row and O(N²) overall with range access. With index access, cost becomes O(T × N), so table size matters: a 40,000-value OR query took 3,406 seconds on a 1M-row table but only 265 seconds on 100k rows. Pushing from 34,753 to 34,754 OR conditions crossed the range_optimizer_max_mem_size limit, making the query jump from 37 to 2,907 seconds. Rewriting the same query with IN instead drops the cost to O(R log N), since IN uses binary search instead of a linear scan.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.percona.com/blog/why-mysql-or-is-slower-than-in>

## Questions this post answers

### Why is my MySQL query with many OR conditions so much slower than using IN?

MySQL evaluates a chain of OR conditions as a linear scan through each Item_func_eq element for every row, costing O(N) per row and O(N squared) overall with range access, or O(T x N) with an index scan. An equivalent IN list costs O(R log N) because MySQL can use binary search instead of checking each value one by one.

_Developers tuning slow MySQL queries can track detailed optimization breakdowns like this on daily.dev._

### Why did my MySQL query suddenly get dramatically slower after adding just one more value to a long OR or IN list?

Adding one more value can push the range optimizer past the range_optimizer_max_mem_size limit, which defaults to 8 MB, forcing MySQL to abandon range access for a full index scan. One test went from 34,753 to 34,754 OR conditions and the query time jumped from 37 seconds to 2,907 seconds, a 79 times slowdown.

_Anyone debugging sudden MySQL query regressions can find these edge-case breakdowns on daily.dev._

### Does increasing table size always make a MySQL OR query with many conditions slower?

Not when range access is used: a query with range access scans only N matching rows, so table size T does not appear in the cost and execution time stays close across 100k, 500k, and 1M row tables. But with an index scan, cost becomes O(T x N), so a 40,000-value OR query took 265 seconds on 100k rows versus 3,406 seconds on 1M rows.

_Engineers benchmarking MySQL query plans across table sizes can follow deep dives like this on daily.dev._

## Similar posts on daily.dev

- [ClickHouse Will Let You Be Wrong at Scale](https://daily.dev/posts/clickhouse-will-let-you-be-wrong-at-scale-prxaelmw6) · Mat Duggan · 1 upvotes · 0 comments
- [T-SQL Mistakes: Careless and Careful Write Six Queries](https://daily.dev/posts/t-sql-mistakes-careless-and-careful-write-six-queries-yclc27gxr) · SQL Authority · 0 upvotes · 0 comments
- [System Design: Your Database Is Fast. So Why Is Your API Taking 8 Seconds?](https://daily.dev/posts/system-design-your-database-is-fast-so-why-is-your-api-taking-8-seconds--8w3gkjckf) · Medium · 0 upvotes · 0 comments
- [The SQL Advisor: Seven Answers, All Measured](https://daily.dev/posts/the-sql-advisor-seven-answers-all-measured-ogimiklhz) · SQL Authority · 2 upvotes · 1 comments

---

Tags: [#database](https://daily.dev/tags/database), [#sql](https://daily.dev/tags/sql), [#mysql](https://daily.dev/tags/mysql)

[View this post on daily.dev](https://daily.dev/posts/why-mysql-or-is-slower-than-in-rwp6gu1c2)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Why MySQL OR is slower than IN","url":"https://daily.dev/posts/why-mysql-or-is-slower-than-in-rwp6gu1c2","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/why-mysql-or-is-slower-than-in-rwp6gu1c2"},"datePublished":"2026-11-19T00:42:05.627Z","dateModified":"2026-11-19T00:42:36.720Z","description":"MySQL evaluates long chains of OR conditions far less efficiently than equivalent IN lists, because OR checks are a linear scan per row. MySQL turns 'id = 1 OR...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/eb424260b4d55db730860cc5e9435be9?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/eb424260b4d55db730860cc5e9435be9?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Planet MySQL","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Planet MySQL","logo":"https://media.daily.dev/image/upload/s--UKAtmXTq--/f_auto,q_auto/v1780213421/logos/planet-mysql?_a=BAMAMiWQ0","url":"https://daily.dev/sources/planet-mysql"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/why-mysql-or-is-slower-than-in-rwp6gu1c2","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"database,sql,mysql","timeRequired":"PT4M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Planet MySQL","item":"https://daily.dev/sources/planet-mysql"},{"@type":"ListItem","position":3,"name":"Why MySQL OR is slower than IN"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/why-mysql-or-is-slower-than-in-rwp6gu1c2#faq","mainEntity":[{"@type":"Question","name":"Why is my MySQL query with many OR conditions so much slower than using IN?","acceptedAnswer":{"@type":"Answer","text":"MySQL evaluates a chain of OR conditions as a linear scan through each Item_func_eq element for every row, costing O(N) per row and O(N squared) overall with range access, or O(T x N) with an index scan. An equivalent IN list costs O(R log N) because MySQL can use binary search instead of checking each value one by one. Developers tuning slow MySQL queries can track detailed optimization breakdowns like this on daily.dev."}},{"@type":"Question","name":"Why did my MySQL query suddenly get dramatically slower after adding just one more value to a long OR or IN list?","acceptedAnswer":{"@type":"Answer","text":"Adding one more value can push the range optimizer past the range_optimizer_max_mem_size limit, which defaults to 8 MB, forcing MySQL to abandon range access for a full index scan. One test went from 34,753 to 34,754 OR conditions and the query time jumped from 37 seconds to 2,907 seconds, a 79 times slowdown. Anyone debugging sudden MySQL query regressions can find these edge-case breakdowns on daily.dev."}},{"@type":"Question","name":"Does increasing table size always make a MySQL OR query with many conditions slower?","acceptedAnswer":{"@type":"Answer","text":"Not when range access is used: a query with range access scans only N matching rows, so table size T does not appear in the cost and execution time stays close across 100k, 500k, and 1M row tables. But with an index scan, cost becomes O(T x N), so a 40,000-value OR query took 265 seconds on 100k rows versus 3,406 seconds on 1M rows. Engineers benchmarking MySQL query plans across table sizes can follow deep dives like this on daily.dev."}}]}
```

