Improper Indexing in Databases: Slow Query Examples

Explore practical examples showcasing how improper indexing can lead to slow database queries and performance bottlenecks.
By Jamie

Understanding Improper Indexing in Databases

Improper indexing in databases can significantly slow down query performance, leading to frustrating user experiences and inefficient application behavior. Indexes are essential for optimizing data retrieval, but when used incorrectly, they can cause more harm than good. Below are three practical examples illustrating how improper indexing can create performance bottlenecks.

Example 1: Missing Index on a Frequently Queried Column

Context: In an e-commerce application, customer orders are often searched by the ‘customer_id’. Without an appropriate index, the database must scan the entire ’orders’ table for every query, leading to slow response times.

To illustrate:

  • Table Structure:
    • orders (order_id, customer_id, product_id, order_date)
  • Query:

    SELECT * FROM orders WHERE customer_id = 12345;  
    
  • Result:
    Without an index on customer_id, the database performs a full table scan, which can take considerable time as the number of records grows.

Notes:

  • Implementing an index on customer_id would optimize this query, enabling the database to quickly locate all orders for a specific customer.

Example 2: Over-Indexing with Redundant Indexes

Context: A financial application maintains a table for transactions with indexes on multiple columns, including ‘transaction_id’, ‘user_id’, and ‘timestamp’. However, having redundant indexes can increase write operation times and slow down read queries.

For example:

  • Table Structure:
    • transactions (transaction_id, user_id, amount, timestamp)
  • Existing Indexes:
    • Index on user_id
    • Index on timestamp
    • Composite index on user_id and timestamp
  • Query:

    SELECT * FROM transactions WHERE user_id = 67890 AND timestamp > '2023-01-01';  
    
  • Result:
    While the composite index may help, the database may struggle to choose the best index among the redundant ones, leading to slower performance.

Notes:

  • Regularly audit and analyze your indexes to eliminate redundancy and ensure only necessary indexes are maintained, thus optimizing both read and write speeds.

Example 3: Index on Non-Selective Columns

Context: In a social media application, user posts are stored in a database, and an index is created on the ‘visibility’ column, which only has a few distinct values (e.g., public, private, friends). This non-selective index does not help with performance and can actually degrade it.

Consider the following:

  • Table Structure:
    • posts (post_id, user_id, content, visibility)
  • Query:

    SELECT * FROM posts WHERE visibility = 'public';  
    
  • Result:
    Since the visibility column has low cardinality, the database retrieves a vast number of rows, and the index does not sufficiently narrow down the results, causing a performance hit.

Notes:

  • It’s important to ensure that indexes are created on highly selective columns to improve query performance effectively. Consider indexing columns with high cardinality, such as user_id or a combination of user_id and timestamp, to enhance query efficiency.