Shard sizing strategy in Elasticsearch - Time & Space Complexity
When working with Elasticsearch, how we size shards affects how fast queries and indexing run.
We want to understand how the number and size of shards impact the work Elasticsearch does.
Analyze the time complexity of querying data distributed across shards.
GET /my_index/_search
{
"query": { "match_all": {} },
"size": 10
}
This query searches all shards of the index and combines results.
Look at what repeats when Elasticsearch runs this query.
- Primary operation: Searching each shard separately.
- How many times: Once per shard in the index.
As you add more shards, Elasticsearch does more separate searches.
| Number of Shards (n) | Approx. Searches |
|---|---|
| 5 | 5 searches |
| 50 | 50 searches |
| 500 | 500 searches |
Pattern observation: The work grows directly with the number of shards.
Time Complexity: O(n)
This means the total work grows in a straight line as you add more shards.
[X] Wrong: "More shards always make queries faster because work is split more."
[OK] Correct: Each shard adds overhead, so too many shards can slow things down instead of speeding them up.
Understanding shard sizing helps you design Elasticsearch setups that balance speed and resource use, a key skill for real projects.
"What if we reduce the number of shards but increase the size of each shard? How would the time complexity change?"