Sobes.tech
Middle — Senior

How do you understand that these specific indexes on these columns will help achieve performant queries? How do you determine high selectivity?

sobes.tech AI

Answer from AI

To understand that indexes on certain columns can help speed up queries, you need to evaluate:

  1. Frequency of use of columns in WHERE, JOIN, ORDER BY, GROUP BY conditions. If a column is frequently involved in filtering or sorting, an index on it can help.

  2. Column selectivity — how unique the values are. The higher the selectivity, the more effective the index. For example, a column with unique values (like ID) has high selectivity, while a boolean column has low.

  3. Data statistics — distribution of values, number of unique values (cardinality). Usually, the DBMS collects statistics that can be queried (for example, in PostgreSQL — pg_stats).

  4. Query execution plan. Analyzing the plan (EXPLAIN) shows whether the DBMS uses an index and how efficiently.

High selectivity is defined as the ratio of the number of unique values to the total number of rows. For example, if a table has 1 million rows and 900,000 unique values in a column, the selectivity is very high. If there are only 10 unique values, the selectivity is low.

Indexes on columns with high selectivity allow quickly filtering a small part of the data, speeding up queries. Conversely, indexes on columns with low selectivity may not provide a performance benefit and can even slow down inserts and updates.

sobes.tech AI

Answer from AI

To determine which indexes can help improve query performance, I usually follow these steps:

  1. Analyze the execution plan of the query (EXPLAIN or EXPLAIN ANALYZE) — this shows which tables and columns are involved in filtering, joins, and sorting.

  2. Identify columns used in filtering (WHERE), joins (JOIN ON), and sorting (ORDER BY), as indexes on these columns can significantly speed up searches.

  3. Check the selectivity of columns — an index on a column with high selectivity (many unique values) is usually more effective.

  4. Use database statistics and query profiling to understand where the bottlenecks are.

  5. After adding an index, re-analyze the execution plan and measure execution time to ensure improvement.

Example:

EXPLAIN ANALYZE SELECT * FROM orders WHERE customer_id = 123 AND order_date > '2023-01-01';

If the plan shows a full table scan, I add an index on (customer_id, order_date):

CREATE INDEX idx_orders_customer_date ON orders(customer_id, order_date);

Then, I check the plan and execution time again.