Elasticsearch vs relational databases - Performance Comparison
When comparing Elasticsearch and relational databases, it's important to understand how their query times grow as data size increases.
We want to see how the cost of searching or retrieving data changes with bigger datasets.
Analyze the time complexity of a simple search query in Elasticsearch and a SQL SELECT query in a relational database.
// Elasticsearch query example
GET /products/_search
{
"query": {
"match": { "name": "laptop" }
}
}
-- Equivalent SQL query
SELECT * FROM products WHERE name LIKE '%laptop%';
This code searches for products with "laptop" in their name using Elasticsearch and a relational database.
Both queries scan through data to find matches.
- Primary operation: Searching through indexed data or table rows.
- How many times: Depends on the number of documents or rows (n).
As the number of items grows, the search time changes differently in each system.
| Input Size (n) | Elasticsearch Approx. Operations | Relational DB Approx. Operations |
|---|---|---|
| 10 | Quick, near constant time due to inverted index | May scan many rows, slower |
| 100 | Still fast, index helps find matches quickly | Slower, more rows to check |
| 1000 | Search time grows slowly, index efficient | Search time grows roughly linearly with rows |
Pattern observation: Elasticsearch uses indexes to keep search time low even as data grows, while relational DBs without indexes scan more data and slow down more.
Time Complexity: O(log n) for Elasticsearch with indexes, O(n) for relational DB without indexes
This means Elasticsearch searches grow slowly with data size, while relational DB searches can grow directly with data size if not indexed.
[X] Wrong: "All database searches take the same time no matter the system."
[OK] Correct: Different systems use different ways to find data. Elasticsearch uses special indexes that make searching faster as data grows, unlike some relational databases without proper indexes.
Understanding how search time changes with data size helps you explain why certain databases fit different needs. This skill shows you can think about performance, not just code.
"What if the relational database had proper indexes on the searched column? How would the time complexity change?"
Practice
Solution
Step 1: Understand Elasticsearch's purpose
Elasticsearch is built for fast full-text search and analytics on large volumes of data.Step 2: Compare with relational databases
Relational databases focus on structured data and transactional integrity, not optimized for text search.Final Answer:
Elasticsearch is optimized for fast full-text search and analytics. -> Option AQuick Check:
Elasticsearch = fast text search [OK]
- Confusing Elasticsearch with relational database transactional features
- Thinking Elasticsearch uses SQL for queries
- Assuming Elasticsearch is only for batch processing
Solution
Step 1: Identify relational database query language
Relational databases use SQL (Structured Query Language) to manage and query data.Step 2: Compare other options
JSON Query DSL is for Elasticsearch, XPath is for XML, GraphQL is a query language for APIs.Final Answer:
SQL -> Option CQuick Check:
Relational DBs use SQL [OK]
- Confusing JSON Query DSL with SQL
- Thinking XPath or GraphQL are used for relational databases
- Mixing query languages between technologies
{"query": {"match": {"title": "Elasticsearch"}}}Solution
Step 1: Analyze the query structure
The query uses "match" on the "title" field with the term "Elasticsearch" which searches for documents containing that word.Step 2: Understand Elasticsearch query actions
Queries retrieve data; update, delete, and create require different API calls.Final Answer:
Retrieve documents where the title field contains the word 'Elasticsearch'. -> Option DQuick Check:
Match query = search documents [OK]
- Thinking the query modifies data instead of searching
- Confusing match with exact match or update operations
- Assuming it deletes or creates documents
SELECT * FROM products WHERE name = 'laptop'
Solution
Step 1: Understand query languages
SQL uses its own syntax; Elasticsearch uses JSON Query DSL, not SQL syntax.Step 2: Identify the mismatch
The given query is valid SQL but invalid for Elasticsearch which requires JSON format queries.Final Answer:
Elasticsearch does not use SQL syntax; it uses JSON-based queries. -> Option BQuick Check:
Elasticsearch queries = JSON, not SQL [OK]
- Assuming Elasticsearch supports SQL syntax natively
- Thinking JOINs are mandatory in Elasticsearch queries
- Confusing string quoting rules between SQL and JSON
Solution
Step 1: Identify requirements
The system needs complex transactions, multiple joins, and strict data integrity.Step 2: Match requirements to database features
Relational databases provide ACID transactions and support joins, ensuring data integrity.Step 3: Compare Elasticsearch capabilities
Elasticsearch is optimized for search and analytics, not transactional integrity or joins.Final Answer:
Relational database, because it supports ACID transactions and joins. -> Option AQuick Check:
Transactions + joins = Relational DB [OK]
- Choosing Elasticsearch for transactional systems
- Confusing JSON storage with transactional support
- Assuming Elasticsearch supports complex joins
Ready to prove it?
Pass the test and this pattern moves unknown
