database_engine large_stringclasses 8
values | slow_query large_stringlengths 92 591 | execution_plan large_stringlengths 145 1.86k | diagnostic_thought large_stringlengths 223 929 | remediation_ddl large_stringlengths 47 481 | optimized_query large_stringlengths 97 733 |
|---|---|---|---|---|---|
PostgreSQL 16 | SELECT * FROM audit_logs WHERE (SELECT COUNT(*) FROM audit_logs WHERE user_id = audit_logs.user_id AND event_type = 'LOGIN') > 5; | Seq Scan on audit_logs (cost=0.00..100000.00 rows=10000 width=100) (actual time=1000.000..20000.000 rows=1000 loops=1)
Filter: (subplan)
SubPlan 1
-> Aggregate (cost=10.00..100.00 rows=1 width=8) (actual time=10.000..20.000 rows=1 loops=1000)
-> Seq Scan on audit_logs audit_logs_1 (cost=0.00..10.... | The subquery in the WHERE clause is causing a sequential scan on the audit_logs table for each row, which is highly inefficient. The optimizer is unable to use an index-only scan because the subquery is not correlated in a way that allows the use of an index. The high cost and execution time are due to the repeated ful... | CREATE INDEX idx_audit_logs_user_id_event_type ON audit_logs (user_id, event_type); | SELECT * FROM audit_logs WHERE user_id IN (SELECT user_id FROM audit_logs WHERE event_type = 'LOGIN' GROUP BY user_id HAVING COUNT(*) > 5); |
PostgreSQL 16 | SELECT a.id, a.name, b.value FROM table_a a JOIN table_b b ON a.id = b.a_id WHERE a.status = 'active' AND b.timestamp > '2023-01-01'; | Seq Scan on table_a a (cost=0.00..350000.00 rows=20000000 width=48) (actual time=0.020..10000.000 rows=20000000 loops=1) Filter: (status = 'active') Rows Removed by Filter: 0 -> Seq Scan on table_b b (cost=0.00..350000.00 rows=20000000 width=48) (actual time=0.020..10000.000 rows=20000000 loops=1) Filter: (timestamp ... | The query is performing a sequential scan on both tables due to the lack of appropriate indexes. The filter conditions on 'status' and 'timestamp' are not being utilized effectively, leading to a full scan of 20 million rows in each table. This is highly inefficient and results in a very high execution time. | CREATE INDEX idx_table_a_status ON table_a (status); CREATE INDEX idx_table_b_timestamp ON table_b (timestamp); CREATE INDEX idx_table_b_a_id_timestamp ON table_b (a_id, timestamp); | SELECT a.id, a.name, b.value FROM table_a a JOIN table_b b ON a.id = b.a_id WHERE a.status = 'active' AND b.timestamp > '2023-01-01'; |
PostgreSQL 16 | SELECT t1.id, t1.name, t2.value FROM table1 t1 JOIN table2 t2 ON t1.id = t2.table1_id WHERE t2.status = 'active'; | Seq Scan on table1 t1 (cost=0.00..350000.00 rows=20000000 width=12) (actual time=0.015..12000.000 rows=20000000 loops=1)
Filter: (t2.status = 'active')
Rows Removed by Filter: 0
-> Index Scan using idx_table2_status on table2 t2 (cost=0.42..80000.00 rows=1000000 width=8) (actual time=0.005..2000.000 rows=10000... | The query is performing a sequential scan on table1 due to the lack of an appropriate index to support the JOIN condition and the WHERE clause. The sequential scan is costly because it has to scan all 20 million rows in table1. The index on table2 is being used effectively, but the lack of a composite index on table1 i... | CREATE INDEX idx_table1_id_status ON table1 (id) WHERE status = 'active';
VACUUM ANALYZE table1; | SELECT t1.id, t1.name, t2.value FROM table1 t1 JOIN table2 t2 ON t1.id = t2.table1_id WHERE t2.status = 'active' AND t1.status = 'active'; |
PostgreSQL 16 | SELECT * FROM audit_logs WHERE (SELECT COUNT(*) FROM audit_details WHERE audit_details.log_id = audit_logs.id) > 10; | Seq Scan on audit_logs (cost=0.00..100000.00 rows=10000 width=100) (actual time=1000.000..20000.000 rows=10000 loops=1)
Filter: (subplan)
SubPlan 1
-> Aggregate (cost=10.00..100.00 rows=1 width=8) (actual time=10.000..100.000 rows=1 loops=10000)
-> Seq Scan on audit_details (cost=0.00..10.00 rows... | The optimizer is choosing a sequential scan on the audit_logs table and a nested loop with an aggregate subquery on the audit_details table. This is inefficient because the subquery is executed for each row in the audit_logs table, leading to a high number of sequential scans on audit_details. The lack of an index on a... | CREATE INDEX idx_audit_details_log_id ON audit_details (log_id); | SELECT audit_logs.* FROM audit_logs JOIN (SELECT log_id FROM audit_details GROUP BY log_id HAVING COUNT(*) > 10) AS filtered_details ON audit_logs.id = filtered_details.log_id; |
PostgreSQL 16 | SELECT * FROM large_table WHERE status = 'active' AND updated_at > '2023-01-01' ORDER BY updated_at LIMIT 1000; | Seq Scan on large_table (cost=0.00..1000000.00 rows=1000 width=100) (actual time=10000.000..20000.000 rows=1000 loops=1)
Filter: ((status = 'active'::text) AND (updated_at > '2023-01-01'::date))
Rows Removed by Filter: 9999000
Planning Time: 10.000 ms
Execution Time: 20010.000 ms | The query is performing a sequential scan on a large table, which is highly inefficient. The filter conditions are not being used effectively, likely due to the lack of an appropriate index. Additionally, the presence of long-running idle transactions can cause autovacuum to freeze, increasing the risk of transaction I... | CREATE INDEX idx_large_table_status_updated_at ON large_table (status, updated_at); | SELECT * FROM large_table WHERE status = 'active' AND updated_at > '2023-01-01' ORDER BY updated_at LIMIT 1000; |
PostgreSQL 16 | SELECT * FROM audit_logs WHERE (SELECT COUNT(*) FROM audit_logs WHERE user_id = 123) > 1000; | Seq Scan on audit_logs (cost=10000.00..20000.00 rows=10000 width=100) (actual time=1000.000..2000.000 rows=10000 loops=1)
Filter: (subplan)
SubPlan 1
-> Aggregate (cost=1000.00..1001.00 rows=1 width=8) (actual time=100.000..100.000 rows=1 loops=1)
-> Seq Scan on audit_logs (cost=0.00..999.00 rows... | The optimizer is performing a sequential scan on the audit_logs table because the subquery in the WHERE clause is preventing the use of an index-only scan. The subquery is executed for each row, leading to a high execution cost. The sequential scan is inefficient, especially on a large partitioned table, as it does not... | CREATE INDEX idx_audit_logs_user_id ON audit_logs (user_id); | SELECT * FROM audit_logs WHERE user_id = 123 AND (SELECT COUNT(*) FROM audit_logs WHERE user_id = 123) > 1000; |
PostgreSQL 16 | SELECT * FROM orders WHERE status = 'pending' AND created_at > '2023-01-01' ORDER BY created_at DESC LIMIT 100; | Limit (cost=100000000.00..100000000.01 rows=100 width=1234) (actual time=12345.678..12345.678 rows=100 loops=1)
-> Index Scan Backward using orders_created_at_idx on orders (cost=0.43..100000000.00 rows=1000000 width=1234) (actual time=12345.678..12345.678 rows=100 loops=1)
Filter: ((status = 'pending'::te... | The query is slow due to the large number of rows being scanned and filtered. The index on `created_at` is being used, but the filter on `status` is not selective enough, causing a large number of rows to be scanned and then filtered out. The long-running idle transactions are preventing autovacuum from running, which ... | CREATE INDEX orders_status_created_at_idx ON orders (status, created_at DESC); VACUUM ANALYZE orders; | SELECT * FROM orders WHERE status = 'pending' AND created_at > '2023-01-01' ORDER BY created_at DESC LIMIT 100; |
PostgreSQL 16 | SELECT * FROM large_table WHERE status = 'active' AND created_at > '2023-01-01' ORDER BY created_at LIMIT 1000; | Seq Scan on large_table (cost=0.00..100000.00 rows=1000 width=100) (actual time=1000.000..20000.000 rows=1000 loops=1)
Filter: ((status = 'active'::text) AND (created_at > '2023-01-01'::date))
Rows Removed by Filter: 999000
Planning Time: 10.000 ms
Execution Time: 20010.000 ms | The query is performing a sequential scan on a large table, which is inefficient and time-consuming. The filter conditions are not being effectively utilized, likely due to the lack of an appropriate index. Additionally, the presence of long-running idle transactions can cause autovacuum to freeze, increasing the risk ... | CREATE INDEX idx_large_table_status_created_at ON large_table (status, created_at); | SELECT * FROM large_table WHERE status = 'active' AND created_at > '2023-01-01' ORDER BY created_at LIMIT 1000; |
ClickHouse | SELECT * FROM transactions WHERE customer_id = 12345 AND transaction_date BETWEEN '2023-01-01' AND '2023-01-31' | EXPLAIN ANALYZE SELECT * FROM transactions WHERE customer_id = 12345 AND transaction_date BETWEEN '2023-01-01' AND '2023-01-31' FORMAT TSVRaw
Read 10000000 rows, 1.00 GiB in 10.00 sec.
10000000 rows in set. Elapsed: 10.00 sec. | The query is performing a full partition decompression because the primary key and skip index are not aligned. The primary key is defined as (transaction_date, customer_id), but the query is filtering on customer_id first, which is not the leading column in the primary key. This causes the query to scan the entire part... | ALTER TABLE transactions MODIFY PRIMARY KEY (customer_id, transaction_date); | SELECT * FROM transactions WHERE customer_id = 12345 AND transaction_date BETWEEN '2023-01-01' AND '2023-01-31' |
PostgreSQL 16 | SELECT * FROM audit_logs WHERE (SELECT COUNT(*) FROM audit_logs WHERE user_id = 123 AND timestamp > '2023-10-01') > 0 | Seq Scan on audit_logs (cost=0.00..100000.00 rows=100000 width=100) (actual time=1000.000..2000.000 rows=50000 loops=1) Filter: (subplan) -> Aggregate (cost=10.00..10.01 rows=1 width=8) (actual time=500.000..500.000 rows=1 loops=1) -> Index Scan using idx_audit_logs_user_id on audit_logs (cost=0.42..10.00 rows=100 widt... | The subquery in the WHERE clause is causing a sequential scan on the `audit_logs` table, which is highly inefficient. The optimizer is not able to use an index-only scan because the subquery is evaluated for each row, leading to a full table scan. This is particularly problematic for a partitioned table, as the subquer... | CREATE INDEX idx_audit_logs_user_id_timestamp ON audit_logs (user_id, timestamp); VACUUM ANALYZE audit_logs; | SELECT * FROM audit_logs WHERE user_id = 123 AND timestamp > '2023-10-01' AND EXISTS (SELECT 1 FROM audit_logs WHERE user_id = 123 AND timestamp > '2023-10-01') |
ClickHouse | SELECT * FROM events WHERE event_date >= '2023-10-01' AND event_date < '2023-11-01' AND user_id = 12345 | EXPLAIN ANALYZE SELECT * FROM events WHERE event_date >= '2023-10-01' AND event_date < '2023-11-01' AND user_id = 12345
Read 10000000 rows, 1.00 MiB in 10.000 sec.
10000000.00 rows/sec., 100.00 KiB/sec.
10000000.00 rows in set. Elapsed: 10.000 sec. | The query is performing a full partition decompression because the primary key and skip index are not aligned. The primary key is (event_date, user_id), but the skip index is only on (event_date). This mismatch causes the query to decompress the entire partition for '2023-10-01' to '2023-11-01' and then filter by user_... | ALTER TABLE events ADD INDEX idx_user_id (user_id) TYPE minmax GRANULARITY 1; | SELECT * FROM events WHERE event_date >= '2023-10-01' AND event_date < '2023-11-01' AND user_id = 12345 |
MySQL 8.0 | SELECT a, b, COUNT(*) FROM table_name WHERE c = 'value' GROUP BY a, b ORDER BY a, b LIMIT 100 OFFSET 1000; | EXPLAIN ANALYZE SELECT a, b, COUNT(*) FROM table_name WHERE c = 'value' GROUP BY a, b ORDER BY a, b LIMIT 100 OFFSET 1000; -> Sort: a, b (cost=100000.00 rows=100000) (actual time=12345.678..12345.678 rows=100000 loops=1) -> Table scan on table_name (cost=1000.00 rows=100000) (actual time=123.456..1234.567 rows=100000 l... | The optimizer is performing a full table scan due to the lack of an appropriate index on column 'c'. The GROUP BY and ORDER BY operations are causing the creation of a temporary table and filesort on disk, which is highly inefficient. The OFFSET 1000 is also contributing to the performance degradation as it requires My... | CREATE INDEX idx_c_a_b ON table_name (c, a, b); | SELECT a, b, COUNT(*) FROM table_name WHERE c = 'value' GROUP BY a, b ORDER BY a, b LIMIT 100 OFFSET 1000; |
PostgreSQL 16 | SELECT t1.id, t1.name, t2.value FROM table1 t1 JOIN table2 t2 ON t1.id = t2.table1_id WHERE t1.status = 'active' AND t2.timestamp > '2023-01-01'; | Seq Scan on table1 t1 (cost=0.00..400000.00 rows=20000000 width=12) (actual time=1000.000..20000.000 rows=10000000 loops=1)
Filter: (status = 'active'::text)
Rows Removed by Filter: 10000000
-> Index Scan using idx_table2_table1_id on table2 t2 (cost=0.42..80000.00 rows=1000000 width=8) (actual time=500.000..1... | The query is performing a sequential scan on table1 due to the lack of an appropriate index on the columns used in the WHERE clause and the JOIN condition. The sequential scan is expensive because it has to scan all 20 million rows in table1 to filter by the 'status' column and then perform the JOIN with table2. The in... | CREATE INDEX idx_table1_status_id ON table1 (status, id); VACUUM ANALYZE table1; | SELECT t1.id, t1.name, t2.value FROM table1 t1 JOIN table2 t2 ON t1.id = t2.table1_id WHERE t1.status = 'active' AND t2.timestamp > '2023-01-01'; |
PostgreSQL 16 | SELECT t1.id, t1.name, t2.address FROM table1 t1 JOIN table2 t2 ON t1.id = t2.id WHERE t1.status = 'active' AND t2.city = 'New York'; | Seq Scan on table1 t1 (cost=0.00..350000.00 rows=20000000 width=12) (actual time=0.015..10000.000 rows=20000000 loops=1)
Filter: (status = 'active'::text)
Rows Removed by Filter: 0
-> Index Scan using idx_table2_id on table2 t2 (cost=0.42..8.44 rows=1 width=22) (actual time=0.005..0.005 rows=1 loops=20000000)
... | The query is performing a sequential scan on table1 due to the lack of an appropriate index on the columns used in the WHERE clause (status) and the JOIN condition (id). This results in a high cost and slow execution time. The sequential scan on 20 million rows is particularly expensive, and the subsequent index scan o... | CREATE INDEX idx_table1_id_status ON table1 (id, status); | SELECT t1.id, t1.name, t2.address FROM table1 t1 JOIN table2 t2 ON t1.id = t2.id WHERE t1.status = 'active' AND t2.city = 'New York'; |
PostgreSQL 16.2 | SELECT * FROM orders o JOIN customers c ON o.customer_id = c.customer_id WHERE o.order_date > '2022-01-01' AND c.country = 'USA' | {"Plan": {"Node Type": "Nested Loop", "Join Type": "Inner", "Plans": [{"Node Type": "Seq Scan", "Relation Name": "orders", "Alias": "o", "Rows": 100000, "Cost": 2345.67}, {"Node Type": "Seq Scan", "Relation Name": "customers", "Alias": "c", "Rows": 50000, "Cost": 1234.56}]}} | The query planner chose a Nested Loop join due to the lack of suitable indexes on the join columns (customer_id) and the filter columns (order_date, country). This results in a Seq Scan on both tables, leading to high I/O and disk spills. | CREATE INDEX idx_orders_customer_id ON orders (customer_id); CREATE INDEX idx_orders_order_date ON orders (order_date); CREATE INDEX idx_customers_country ON customers (country); VACUUM ANALYZE orders; VACUUM ANALYZE customers; | SELECT * FROM orders o JOIN customers c ON o.customer_id = c.customer_id WHERE o.order_date > '2022-01-01' AND c.country = 'USA' AND o.customer_id IN (SELECT customer_id FROM customers WHERE country = 'USA') |
PostgreSQL 16.2 | SELECT * FROM orders o INNER JOIN customers c ON o.customer_id = c.customer_id WHERE o.order_date > '2022-01-01' AND c.country = 'USA' | {"Plan": {"Node Type": "Nested Loop", "Join Type": "Inner", "Plans": [{"Node Type": "Seq Scan", "Relation Name": "orders", "Alias": "o", "Rows": 100000, "Cost": 4281.5}, {"Node Type": "Seq Scan", "Relation Name": "customers", "Alias": "c", "Rows": 10000, "Cost": 1415.5}]}} | The query planner chose a nested loop join with sequential scans due to the lack of suitable indexes on the join columns (customer_id) and the filter columns (order_date and country). This results in a high number of row scans and joins, leading to poor performance. | CREATE INDEX idx_orders_customer_id ON orders (customer_id); CREATE INDEX idx_orders_order_date ON orders (order_date); CREATE INDEX idx_customers_country ON customers (country); VACUUM ANALYZE orders; VACUUM ANALYZE customers; | SELECT * FROM orders o INNER JOIN customers c ON o.customer_id = c.customer_id WHERE o.order_date > '2022-01-01' AND c.country = 'USA' AND o.customer_id IN (SELECT customer_id FROM customers WHERE country = 'USA') |
PostgreSQL 16.2 | SELECT * FROM orders WHERE total_amount > 1000 AND order_date BETWEEN '2022-01-01' AND '2022-12-31' | {"Plan": {"Node Type": "Seq Scan", "Relation Name": "orders", "Alias": "orders", "Startup Cost": 0.0, "Total Cost": 2345.67, "Plan Rows": 10000, "Plan Width": 148}} | The query planner chose a sequential scan because the where clause is using two separate conditions (total_amount and order_date) which are not covered by a single index, resulting in a full table scan. The planner estimated the cost of using an index on either column to be higher than a sequential scan due to the perc... | CREATE INDEX idx_orders_total_amount_order_date ON orders (total_amount, order_date); VACUUM ANALYZE orders; | SELECT * FROM orders WHERE total_amount > 1000 AND order_date BETWEEN '2022-01-01' AND '2022-12-31' |
PostgreSQL 16.2 | SELECT * FROM customers c JOIN orders o ON c.customer_id = o.customer_id WHERE c.country = 'USA' AND o.order_date > '2022-01-01' | {"Plan": {"Node Type": "Nested Loop", "Join Type": "Inner", "Plans": [{"Node Type": "Seq Scan", "Relation Name": "customers", "Alias": "c", "Rows": 100000, "Filter": "((country = 'USA'::text) AND (customer_id IS NOT NULL))"}, {"Node Type": "Seq Scan", "Relation Name": "orders", "Alias": "o", "Rows": 500000, "Filter": "... | The query planner chose a nested loop join because it didn't have enough statistical information to determine the most efficient join order. The sequential scans on both tables are causing high I/O and disk spills, leading to poor performance. | CREATE INDEX idx_customers_country ON customers (country); CREATE INDEX idx_orders_customer_id ON orders (customer_id); CREATE INDEX idx_orders_order_date ON orders (order_date); VACUUM ANALYZE customers; VACUUM ANALYZE orders; | SELECT * FROM customers c JOIN orders o ON c.customer_id = o.customer_id AND o.order_date > '2022-01-01' WHERE c.country = 'USA' |
PostgreSQL 16.2 | SELECT * FROM orders WHERE customer_id = 123 AND order_date BETWEEN '2022-01-01' AND '2022-12-31' | {"Plan": {"Node Type": "Seq Scan", "Relation Name": "orders", "Alias": "orders", "Startup Cost": 0.0, "Total Cost": 2345.67, "Plan Rows": 100, "Plan Width": 136}, "Planning Time": 0.128, "Execution Time": 1234.567} | The query planner chose a sequential scan because the "customer_id" and "order_date" columns are not indexed together, leading to a full table scan. Additionally, the lack of a covering index for the "orders" table results in a high number of disk blocks being read. | CREATE INDEX idx_orders_customer_id_order_date ON orders (customer_id, order_date); VACUUM ANALYZE orders; | SELECT * FROM orders WHERE customer_id = 123 AND order_date BETWEEN '2022-01-01' AND '2022-12-31' |
PostgreSQL 16.2 | SELECT * FROM orders o JOIN customers c ON o.customer_id = c.customer_id WHERE o.order_date BETWEEN '2022-01-01' AND '2022-01-31' AND c.country = 'USA' | {"Plan": {"Node Type": "Hash Join", "Join Type": "inner", "Plans": [{"Node Type": "Seq Scan", "Relation Name": "orders", "Alias": "o", "Rows": 100000, "Filter": "(order_date >= '2022-01-01'::date) AND (order_date <= '2022-01-31'::date)"}, {"Node Type": "Seq Scan", "Relation Name": "customers", "Alias": "c", "Rows": 100... | The query planner chose a sub-optimal route because it didn't have sufficient statistics to accurately estimate the number of rows that would be returned by the join operation. This led to a full table scan of both the orders and customers tables, resulting in high I/O and execution time. Additionally, the lack of an i... | CREATE INDEX idx_orders_customer_id ON orders (customer_id); CREATE INDEX idx_customers_country ON customers (country); VACUUM ANALYZE orders; VACUUM ANALYZE customers; | SELECT * FROM orders o JOIN customers c ON o.customer_id = c.customer_id WHERE o.order_date BETWEEN '2022-01-01' AND '2022-01-31' AND c.country = 'USA' AND o.customer_id IN (SELECT customer_id FROM customers WHERE country = 'USA') |
PostgreSQL 16.2 | SELECT * FROM orders o JOIN customers c ON o.customer_id = c.customer_id WHERE o.order_total > 1000 AND c.country = 'USA' ORDER BY o.order_date DESC LIMIT 100; | {"Plan": {"Node Type": "Limit", "Plans": [{"Node Type": "Sort", "Key": "o.order_date", "Plans": [{"Node Type": "Hash Join", "Join Type": "Inner", "Plans": [{"Node Type": "Seq Scan", "Relation Name": "orders", "Filter": "o.order_total > 1000"}, {"Node Type": "Seq Scan", "Relation Name": "customers", "Filter": "c.country... | The query planner chose a sub-optimal route because it did not have sufficient statistics to determine the selectivity of the join and filter conditions, resulting in a full table scan of both the orders and customers tables. Additionally, the lack of an index on the join columns and the filter columns led to a hash jo... | CREATE INDEX idx_orders_customer_id ON orders (customer_id); CREATE INDEX idx_orders_order_total ON orders (order_total); CREATE INDEX idx_customers_country ON customers (country); VACUUM ANALYZE orders; VACUUM ANALYZE customers; | SELECT * FROM orders o JOIN customers c ON o.customer_id = c.customer_id AND c.country = 'USA' WHERE o.order_total > 1000 ORDER BY o.order_date DESC LIMIT 100; |
PostgreSQL 16.2 | SELECT * FROM orders WHERE total_amount > 1000 AND created_at > '2022-01-01' AND customer_id IN (SELECT id FROM customers WHERE country = 'USA') | {"Plan": {"Node Type": "Seq Scan", "Relation Name": "orders", "Alias": "orders", "Startup Cost": 0.0, "Total Cost": 2354.83, "Plan Rows": 100, "Plan Width": 268}, "Planning Time": 0.128, "Execution Time": 2345.112} | The query planner chose a sequential scan because it couldn't effectively use any existing indexes due to the combination of range conditions (total_amount, created_at) and the subquery on customer_id. The IN clause with a subquery also prevents the use of an index on customer_id. As a result, the planner opted for a f... | CREATE INDEX idx_orders_total_amount ON orders (total_amount); CREATE INDEX idx_orders_created_at ON orders (created_at); CREATE INDEX idx_orders_customer_id ON orders (customer_id); VACUUM ANALYZE orders; | WITH usa_customers AS (SELECT id FROM customers WHERE country = 'USA') SELECT * FROM orders WHERE total_amount > 1000 AND created_at > '2022-01-01' AND customer_id = ANY($1) AND customer_id IN (SELECT id FROM usa_customers) |
PostgreSQL 16.2 | SELECT * FROM orders WHERE total_amount > 1000 AND created_at > '2022-01-01' AND customer_id IN (SELECT customer_id FROM customers WHERE country = 'USA') | {"Plan": {"Node Type": "Seq Scan", "Relation Name": "orders", "Alias": "orders", "Startup Cost": 0.0, "Total Cost": 23543.29, "Plan Rows": 1000, "Plan Width": 268, "Filter": "(total_amount > 1000) AND (created_at > '2022-01-01'::date) AND (customer_id = ANY ($1))", "Rows Removed by Filter": 84211, "SubPlan": {"Node Typ... | The query planner chose a sequential scan on the orders table because it couldn't effectively use any existing indexes due to the combination of conditions in the WHERE clause, particularly the IN condition referencing the customers table. The lack of a suitable index on the orders table for the columns used in the WHE... | CREATE INDEX idx_orders_total_amount ON orders (total_amount); CREATE INDEX idx_orders_created_at ON orders (created_at); CREATE INDEX idx_orders_customer_id ON orders (customer_id); CREATE INDEX idx_customers_country ON customers (country); VACUUM ANALYZE orders; VACUUM ANALYZE customers; | SELECT * FROM orders WHERE total_amount > 1000 AND created_at > '2022-01-01' AND customer_id IN (SELECT customer_id FROM customers WHERE country = 'USA') ORDER BY total_amount, created_at, customer_id |
PostgreSQL 16.2 | SELECT * FROM orders o JOIN customers c ON o.customer_id = c.customer_id WHERE o.order_date BETWEEN '2022-01-01' AND '2022-12-31' AND c.country = 'USA' | {"Plan": {"Node Type": "Hash Join", "Join Type": "Inner", "Output": ["o.*", "c.*"], "Inner Unique": false, "Hash Cond": "(o.customer_id = c.customer_id)", "Plans": [{"Node Type": "Seq Scan", "Relation Name": "orders", "Alias": "o", "Start Up Cost": 0.0, "Total Cost": 10345.83, "Plan Rows": 1000000, "Plan Width": 244, "... | The query planner chose a sub-optimal route due to the lack of efficient indexing on the join and filter columns. The Seq Scan on the orders table is particularly costly, resulting in high I/O and disk spills. Additionally, the filter condition on the order_date column is not efficiently utilized, leading to a full tab... | CREATE INDEX idx_orders_customer_id ON orders (customer_id); CREATE INDEX idx_orders_order_date ON orders (order_date); CREATE INDEX idx_customers_country ON customers (country); VACUUM ANALYZE orders; VACUUM ANALYZE customers; | SELECT * FROM orders o JOIN customers c ON o.customer_id = c.customer_id WHERE o.order_date BETWEEN '2022-01-01' AND '2022-12-31' AND c.country = 'USA' ORDER BY o.order_date |
PostgreSQL 16.2 | SELECT * FROM orders o JOIN customers c ON o.customer_id = c.customer_id WHERE o.order_date >= '2022-01-01' AND o.order_total > 1000 | {"Plan": {"Node Type": "Hash Join", "Join Type": "Inner", "Columns": ["o.order_id", "o.customer_id", "o.order_date", "o.order_total", "c.customer_id", "c.name"], "Rows": 100000, "Cost": 23451.23, "Actual Time": 1234.56, "Actual Rows": 50000, "Plans": [{"Node Type": "Seq Scan", "Relation Name": "orders", "Alias": "o", "... | The query planner chose a sub-optimal route because the join order was not optimized, and there were no indexes on the join columns or the filter columns. This resulted in a sequential scan of both tables, leading to high I/O and a large number of rows being joined, causing performance issues. | CREATE INDEX idx_orders_customer_id ON orders (customer_id); CREATE INDEX idx_orders_order_date ON orders (order_date); CREATE INDEX idx_orders_order_total ON orders (order_total); VACUUM ANALYZE orders; VACUUM ANALYZE customers; | SELECT * FROM orders o JOIN customers c ON o.customer_id = c.customer_id WHERE o.order_date >= '2022-01-01' AND o.order_total > 1000 AND o.customer_id IN (SELECT customer_id FROM customers) |
PostgreSQL 16.2 | SELECT * FROM orders WHERE total_amount > 1000 AND order_date BETWEEN '2022-01-01' AND '2022-12-31'; | {"Plan": {"Node Type": "Seq Scan", "Relation Name": "orders", "Alias": "orders", "Startup Cost": 0.0, "Total Cost": 23543.29, "Plan Rows": 14731, "Plan Width": 268}, "Planning Time": 0.129, "Execution Time": 2341.129} | The query planner chose a sequential scan because the column 'total_amount' is not indexed, and the 'order_date' range is too broad, leading to a full table scan. This results in high I/O operations, causing performance degradation. | CREATE INDEX idx_orders_total_amount ON orders (total_amount); CREATE INDEX idx_orders_order_date ON orders (order_date); VACUUM ANALYZE orders; | SELECT * FROM orders WHERE total_amount > 1000 AND order_date BETWEEN '2022-01-01' AND '2022-12-31' ORDER BY total_amount, order_date; |
PostgreSQL 16.2 | SELECT * FROM orders WHERE total_amount > 1000 AND order_date BETWEEN '2022-01-01' AND '2022-12-31' ORDER BY order_id LIMIT 100; | {"Plan": {"Node Type": "Limit", "Plan Rows": 100, "Plans": [{"Node Type": "Sort", "Plan Rows": 100, "Plans": [{"Node Type": "Seq Scan", "Plan Rows": 10000, "Relation Name": "orders", "Filter": "(total_amount > 1000) AND (order_date >= '2022-01-01'::date) AND (order_date <= '2022-12-31'::date)"}]}]}, "Planning Time": 0.... | The query planner chose a sequential scan due to the lack of a suitable index on the total_amount and order_date columns, leading to a full table scan and high I/O. The ORDER BY clause requires an additional sort operation, further increasing the execution time. | CREATE INDEX idx_orders_total_amount_order_date ON orders (total_amount, order_date); VACUUM ANALYZE orders; | SELECT * FROM orders WHERE total_amount > 1000 AND order_date BETWEEN '2022-01-01' AND '2022-12-31' ORDER BY order_id LIMIT 100; |
PostgreSQL 16.2 | SELECT * FROM orders o JOIN customers c ON o.customer_id = c.customer_id WHERE o.order_date >= '2022-01-01' AND c.country = 'USA' | {"Plan": {"Node Type": "Hash Join", "Join Type": "Inner", "Plans": [{"Node Type": "Seq Scan", "Relation Name": "orders", "Alias": "o", "Startup Cost": 0.0, "Total Cost": 10345.83, "Plan Rows": 100000, "Plan Width": 244}, {"Node Type": "Seq Scan", "Relation Name": "customers", "Alias": "c", "Startup Cost": 0.0, "Total C... | The query planner chose a sub-optimal route due to the lack of indexes on the join and filter columns, resulting in sequential scans of both tables. The hash join is also inefficient due to the large number of rows being joined. | CREATE INDEX idx_orders_customer_id ON orders (customer_id); CREATE INDEX idx_orders_order_date ON orders (order_date); CREATE INDEX idx_customers_country ON customers (country); VACUUM ANALYZE orders; VACUUM ANALYZE customers; | SELECT * FROM orders o JOIN customers c ON o.customer_id = c.customer_id WHERE o.order_date >= '2022-01-01' AND c.country = 'USA' AND o.customer_id IN (SELECT customer_id FROM customers WHERE country = 'USA') |
PostgreSQL 16.2 | SELECT * FROM orders WHERE total_amount > 1000 AND created_at > '2022-01-01' ORDER BY created_at LIMIT 100; | {"Plan": {"Node Type": "Limit", "Startup Cost": 219.43, "Total Cost": 219.46, "Plan Rows": 100, "Plans": [{"Node Type": "Sort", "Startup Cost": 219.43, "Total Cost": 219.46, "Plan Rows": 100, "Sort Key": ["created_at"], "Plans": [{"Node Type": "Seq Scan", "Relation Name": "orders", "Alias": "orders", "Startup Cost": 0.... | The query planner chose a sequential scan because there are no suitable indexes on the total_amount or created_at columns. As a result, the database has to scan the entire table, leading to high I/O and slow performance. The ORDER BY clause also requires a sort operation, which adds to the overall cost. | CREATE INDEX idx_orders_total_amount ON orders (total_amount); CREATE INDEX idx_orders_created_at ON orders (created_at); VACUUM ANALYZE orders; | SELECT * FROM orders WHERE total_amount > 1000 AND created_at > '2022-01-01' ORDER BY created_at LIMIT 100; -- With the new indexes, the query planner can choose a more efficient index scan or bitmap heap scan, reducing the overall cost and improving performance. |
PostgreSQL 16.2 | SELECT * FROM orders o JOIN customers c ON o.customer_id = c.customer_id WHERE o.order_date >= '2022-01-01' AND o.order_date < '2023-01-01' AND c.country = 'USA' | {"Plan": {"Node Type": "Hash Join", "Join Type": "inner", "Parent Relationship": "outer", "Parallel Aware": false, "Join Filter": "(o.customer_id = c.customer_id)", "Rows": 1000000, "Cost": 23451.23, "CPU Cost": 14567.89, "Memory Cost": 2345.12, "Actual Startup Time": 123.45, "Actual Total Time": 4567.89, "Actual Rows"... | The query planner chose a sub-optimal route due to the lack of a composite index on the orders table, which led to a full table scan. Additionally, the filter on the customers table is not selective enough, resulting in a large number of rows being joined. The planner also chose a hash join, which can be expensive for ... | CREATE INDEX idx_orders_customer_id_order_date ON orders (customer_id, order_date); CREATE INDEX idx_customers_country ON customers (country); VACUUM ANALYZE orders; VACUUM ANALYZE customers; | SELECT * FROM orders o JOIN customers c ON o.customer_id = c.customer_id WHERE o.order_date >= '2022-01-01' AND o.order_date < '2023-01-01' AND c.country = 'USA' AND o.customer_id IN (SELECT customer_id FROM customers WHERE country = 'USA') |
PostgreSQL 16.2 | SELECT * FROM orders o JOIN customers c ON o.customer_id = c.customer_id WHERE o.order_date >= '2022-01-01' AND o.order_date < '2022-01-31' AND c.country = 'USA' | {"Plan": {"Node Type": "Hash Join", "Join Type": "Inner", "Plans": [{"Node Type": "Seq Scan", "Relation Name": "orders", "Alias": "o", "Rows": 100000, "Filter": "(order_date >= '2022-01-01'::date) AND (order_date < '2022-01-31'::date)"}, {"Node Type": "Seq Scan", "Relation Name": "customers", "Alias": "c", "Rows": 1000... | The query planner chose a sequential scan on both tables because there are no suitable indexes on the join columns (customer_id) or the filter columns (order_date, country). This results in a full table scan, leading to high I/O and disk usage. Additionally, the join operation is performed using a hash join, which can ... | CREATE INDEX idx_orders_customer_id ON orders (customer_id); CREATE INDEX idx_orders_order_date ON orders (order_date); CREATE INDEX idx_customers_country ON customers (country); VACUUM ANALYZE orders; VACUUM ANALYZE customers; | SELECT * FROM orders o JOIN customers c ON o.customer_id = c.customer_id WHERE o.order_date >= '2022-01-01' AND o.order_date < '2022-01-31' AND c.country = 'USA' ORDER BY o.order_date |
PostgreSQL 16.2 | SELECT * FROM customers c JOIN orders o ON c.customer_id = o.customer_id WHERE o.order_date > '2022-01-01' AND c.country = 'USA' | {"Plan": {"Node Type": "Nested Loop", "Join Type": "Inner", "Plans": [{"Node Type": "Seq Scan", "Relation Name": "orders", "Alias": "o", "Startup Cost": 0.0, "Total Cost": 10345.83, "Plan Rows": 10000, "Plan Width": 136}, {"Node Type": "Seq Scan", "Relation Name": "customers", "Alias": "c", "Startup Cost": 0.0, "Total ... | The query planner chose a nested loop join with sequential scans on both tables because it didn't have sufficient statistics or indexes to efficiently join the tables. The lack of an index on the join columns (customer_id) and the filter columns (order_date, country) led to a full table scan, resulting in high I/O and ... | CREATE INDEX idx_orders_customer_id ON orders (customer_id); CREATE INDEX idx_customers_country ON customers (country); CREATE INDEX idx_orders_order_date ON orders (order_date); VACUUM ANALYZE customers; VACUUM ANALYZE orders; | SELECT * FROM customers c JOIN orders o ON c.customer_id = o.customer_id WHERE o.order_date > '2022-01-01' AND c.country = 'USA' AND c.customer_id IN (SELECT customer_id FROM orders WHERE order_date > '2022-01-01') |
PostgreSQL 16.2 | SELECT * FROM orders WHERE order_date >= '2022-01-01' AND order_date < '2023-01-01' AND customer_id IN (SELECT customer_id FROM customers WHERE country = 'USA') | {"Plan": {"Node Type": "Seq Scan", "Relation Name": "orders", "Alias": "orders", "Startup Cost": 0.0, "Total Cost": 12345.67, "Plan Rows": 10000, "Plan Width": 200, "Actual Startup Time": 12.123, "Actual Total Time": 4567.89, "Actual Rows": 10000, "Actual Loops": 1, "Filter": "(order_date >= '2022-01-01'::date) AND (or... | The query planner chose a sequential scan on the orders table because it didn't have an efficient way to filter the rows based on the order_date and customer_id conditions. The lack of a suitable index on the order_date column and the customer_id column led to a full table scan, resulting in high I/O and slow performan... | CREATE INDEX idx_orders_order_date ON orders (order_date); CREATE INDEX idx_orders_customer_id ON orders (customer_id); CREATE INDEX idx_customers_country ON customers (country); VACUUM ANALYZE orders; VACUUM ANALYZE customers; | SELECT * FROM orders WHERE order_date >= '2022-01-01' AND order_date < '2023-01-01' AND customer_id IN (SELECT customer_id FROM customers WHERE country = 'USA') AND order_date >= '2022-01-01' AND order_date < '2023-01-01' |
PostgreSQL 16.2 | SELECT * FROM orders o JOIN customers c ON o.customer_id = c.customer_id WHERE o.order_total > 1000 AND c.country = 'USA' | {"Plan": {"Node Type": "Nested Loop", "Join Type": "Inner", "Plans": [{"Node Type": "Seq Scan", "Relation Name": "orders", "Alias": "o", "Rows": 100000, "Filter": "order_total > 1000"}, {"Node Type": "Seq Scan", "Relation Name": "customers", "Alias": "c", "Rows": 10000, "Filter": "country = 'USA'"}]}} | The query planner chose a Nested Loop join due to the lack of suitable indexes on the join columns, resulting in a full table scan of both tables. This leads to high I/O and disk spills, causing the query to perform poorly. | CREATE INDEX idx_orders_customer_id ON orders (customer_id); CREATE INDEX idx_customers_country ON customers (country); VACUUM ANALYZE orders; VACUUM ANALYZE customers; | SELECT * FROM orders o JOIN customers c ON o.customer_id = c.customer_id WHERE o.order_total > 1000 AND c.country = 'USA' AND o.customer_id IN (SELECT customer_id FROM customers WHERE country = 'USA') |
PostgreSQL 16.2 | SELECT * FROM orders WHERE order_date BETWEEN '2022-01-01' AND '2022-12-31' AND customer_id IN (SELECT customer_id FROM customers WHERE country = 'USA') | {"Plan": {"Node Type": "Seq Scan", "Relation Name": "orders", "Alias": "orders", "Startup Cost": 0.0, "Total Cost": 23451.83, "Plan Rows": 10000, "Plan Width": 244, "Filter": "(order_date >= '2022-01-01'::date) AND (order_date <= '2022-12-31'::date) AND (customer_id = ANY ($1))", "Rows Removed by Filter": 9000, "SubPla... | The query planner chose a sequential scan on the orders table because the filter condition on order_date does not use an available index, and the subquery on customers table is not optimized, resulting in a high number of rows being scanned and filtered. Additionally, the IN operator with a subquery can lead to ineffic... | CREATE INDEX idx_orders_order_date ON orders (order_date); CREATE INDEX idx_orders_customer_id ON orders (customer_id); CREATE INDEX idx_customers_country ON customers (country); VACUUM ANALYZE orders; VACUUM ANALYZE customers; | SELECT * FROM orders WHERE order_date BETWEEN '2022-01-01' AND '2022-12-31' AND customer_id IN (SELECT customer_id FROM customers WHERE country = 'USA' AND customer_id = orders.customer_id) ORDER BY order_date |
PostgreSQL 16.2 | SELECT * FROM orders WHERE total_amount > 100 AND order_date BETWEEN '2022-01-01' AND '2022-12-31' | {"Plan": {"Node Type": "Seq Scan", "Relation Name": "orders", "Alias": "orders", "Startup Cost": 0.0, "Total Cost": 2354.3, "Plan Rows": 1350, "Plan Width": 268}, "Planning Time": 0.128, "Execution Time": 2345.021} | The query planner chose a sequential scan because the WHERE clause filters on two columns (total_amount and order_date) and there is no suitable index that covers both columns. As a result, the planner has to scan the entire table, leading to high I/O and execution time. | CREATE INDEX idx_orders_total_amount_order_date ON orders (total_amount, order_date); VACUUM ANALYZE orders; | SELECT * FROM orders WHERE total_amount > 100 AND order_date BETWEEN '2022-01-01' AND '2022-12-31' |
PostgreSQL 16.2 | SELECT * FROM customers c JOIN orders o ON c.customer_id = o.customer_id WHERE c.country = 'USA' AND o.order_total > 1000 | {"Plan": {"Node Type": "Hash Join", "Join Type": "inner", "Parent Relationship": "outer", "Parallel Aware": false, "Join Filter": "(c.customer_id = o.customer_id)", "Rows": 12345, "Cost": 23456.78, "Plans": [{"Node Type": "Seq Scan", "Parent Relationship": "outer", "Parallel Aware": false, "Relation Name": "customers",... | The query planner chose a sub-optimal route due to the lack of suitable indexes on the join columns (customer_id) and the filter columns (country, order_total). This led to a full table scan on both the customers and orders tables, resulting in high I/O and disk usage. | CREATE INDEX idx_customers_country ON customers (country); CREATE INDEX idx_orders_customer_id ON orders (customer_id); CREATE INDEX idx_orders_order_total ON orders (order_total); VACUUM ANALYZE customers; VACUUM ANALYZE orders; | SELECT * FROM customers c JOIN orders o ON c.customer_id = o.customer_id WHERE c.country = 'USA' AND o.order_total > 1000 AND c.customer_id IN (SELECT customer_id FROM orders WHERE order_total > 1000) |
PostgreSQL 16.2 | SELECT * FROM orders WHERE total_amount > 1000 AND order_date > '2022-01-01' ORDER BY order_id LIMIT 100 | {"Plan": {"Node Type": "Limit", "Cost": 1234.56, "Rows": 100, "Plans": [{"Node Type": "Sort", "Cost": 2345.67, "Rows": 100, "Plans": [{"Node Type": "Seq Scan", "Cost": 3456.78, "Rows": 1000, "Filter": "(total_amount > 1000) AND (order_date > '2022-01-01')"}]}]}} | The query planner chose a sequential scan because there are no suitable indexes on the total_amount and order_date columns, resulting in a full table scan. The sort operation adds additional overhead, contributing to the high cost of the query. | CREATE INDEX idx_orders_total_amount ON orders (total_amount); CREATE INDEX idx_orders_order_date ON orders (order_date); VACUUM ANALYZE orders; | SELECT * FROM orders WHERE total_amount > 1000 AND order_date > '2022-01-01' ORDER BY order_id LIMIT 100; -- with indexes, the query planner will choose an index scan, reducing the cost and improving performance |
PostgreSQL 16.2 | SELECT * FROM orders WHERE total_amount > 1000 AND order_date BETWEEN '2022-01-01' AND '2022-12-31' ORDER BY order_date DESC | {"Plan": {"Node Type": "Sort", "Parent Relationship": "Outer", "Parallel Aware": false, "Startup Cost": 4353.43, "Total Cost": 8513.19, "Plan Rows": 10000, "Plan Width": 471}, "Plans": [{"Node Type": "Seq Scan", "Parent Relationship": "Outer", "Parallel Aware": false, "Relation Name": "orders", "Alias": "orders", "Star... | The query planner chose a sub-optimal route because it couldn't effectively utilize any index on the orders table due to the lack of a suitable composite index. This led to a full table scan, resulting in high I/O and CPU usage, thus degrading performance. The planner also chose to sort the entire result set, which fur... | CREATE INDEX idx_orders_total_amount_order_date ON orders (total_amount, order_date) INCLUDE (id, customer_id); VACUUM ANALYZE orders; | SELECT * FROM orders WHERE total_amount > 1000 AND order_date BETWEEN '2022-01-01' AND '2022-12-31' ORDER BY order_date DESC LIMIT 10000 |
PostgreSQL 16.2 | SELECT * FROM orders WHERE total_amount > 1000 AND customer_id IN (SELECT customer_id FROM customers WHERE country='USA') | {"Plan": {"Node Type": "Seq Scan", "Relation Name": "orders", "Alias": "orders", "Startup Cost": 0.0, "Total Cost": 2345.23, "Plan Rows": 135, "Plan Width": 268}, "Plans": [{"Node Type": "Seq Scan", "Relation Name": "customers", "Alias": "customers", "Startup Cost": 0.0, "Total Cost": 123.45, "Plan Rows": 1000, "Plan W... | The query planner chose a sequential scan on the orders table because the total_amount column is not indexed, and the subquery on the customers table is also not optimized, leading to a high number of rows being scanned and joined, resulting in high I/O and disk spills. | CREATE INDEX idx_orders_total_amount ON orders (total_amount); CREATE INDEX idx_customers_country ON customers (country); VACUUM ANALYZE orders; VACUUM ANALYZE customers; | SELECT * FROM orders WHERE total_amount > 1000 AND customer_id IN (SELECT customer_id FROM customers WHERE country='USA' AND customer_id = ANY (SELECT customer_id FROM orders WHERE total_amount > 1000)) |
PostgreSQL 16.2 | SELECT * FROM customers c JOIN orders o ON c.customer_id = o.customer_id WHERE c.country = 'USA' AND o.order_date > '2022-01-01' ORDER BY c.customer_name; | {"Plan": {"Node Type": "Merge Join", "Join Type": "Inner", "Sync": true, " Plans": [{"Node Type": "Sort", "Sort Key": ["customers.customer_name"], "Plans": [{"Node Type": "Seq Scan", "Relation Name": "customers", "Filter": "(customers.country = 'USA')"}]}, {"Node Type": "Seq Scan", "Relation Name": "orders", "Filter": ... | The planner chose a sub-optimal merge join because it incorrectly estimated the number of rows returned by the filter conditions. This led to a full table scan on both the customers and orders tables, causing excessive I/O operations. Additionally, the lack of relevant indexes on the join columns and filter conditions ... | CREATE INDEX idx_customers_country ON customers (country); CREATE INDEX idx_orders_order_date ON orders (order_date); CREATE INDEX idx_orders_customer_id ON orders (customer_id); VACUUM ANALYZE customers; VACUUM ANALYZE orders; | SELECT * FROM customers c JOIN orders o ON c.customer_id = o.customer_id WHERE c.country = 'USA' AND o.order_date > '2022-01-01' ORDER BY c.customer_name LIMIT 100; |
PostgreSQL 16.2 | SELECT * FROM orders WHERE total_amount > 1000 AND order_date > '2022-01-01' ORDER BY order_id LIMIT 100; | {"Plan": {"Node Type": "Limit", "Plan Rows": 100, "Plan Width": 244, "Startup Cost": 25810.21, "Total Cost": 25821.21, "Plans": [{"Node Type": "Sort", "Plan Rows": 100, "Plan Width": 244, "Startup Cost": 25810.21, "Total Cost": 25821.21, "Plans": [{"Node Type": "Seq Scan", "Plan Rows": 13579, "Plan Width": 244, "Startu... | The query planner chose a sequential scan due to the lack of a suitable index on the total_amount and order_date columns. The ORDER BY clause further increased the cost by requiring a sort operation, exacerbating the performance bottleneck. | CREATE INDEX idx_orders_total_amount_order_date ON orders (total_amount, order_date); VACUUM ANALYZE orders; | SELECT * FROM orders WHERE total_amount > 1000 AND order_date > '2022-01-01' ORDER BY order_id LIMIT 100; -- Utilizes the newly created composite index |
PostgreSQL 16.2 | SELECT * FROM orders WHERE total_amount > 1000 AND customer_id IN (SELECT id FROM customers WHERE country='USA') | [
{
"Plan": "Nested Loop",
"Nested Loop": {
"Join Filter": "(orders.customer_id = customers.id)",
" Plans": [
{
"Plan": "Seq Scan on orders",
"Filter": "(total_amount > 1000)",
"Rows": 10000,
... | The query planner chose a nested loop join because it had to filter orders based on a subquery that wasn't using an index, resulting in a full table scan on both the orders and customers tables. The subquery was materialized and stored in memory, causing additional overhead. | CREATE INDEX idx_orders_customer_id ON orders (customer_id); CREATE INDEX idx_customers_country ON customers (country); VACUUM ANALYZE orders; VACUUM ANALYZE customers; | SELECT o.* FROM orders o INNER JOIN customers c ON o.customer_id = c.id AND c.country = 'USA' WHERE o.total_amount > 1000 |
PostgreSQL 16.2 | SELECT * FROM orders WHERE total_amount > 1000AND order_date BETWEEN '2022-01-01' AND '2022-12-31' | {"Plan": {"Node Type": "Seq Scan", "Relation Name": "orders", "Alias": "orders", "Startup Cost": 0.0, "Total Cost": 12345.67, "Plan Rows": 1000, "Plan Width": 250}, "Planning Time": 1.234, "Execution Time": 1234.56} | The query planner chose a sequential scan due to the lack of a suitable index on the total_amount and order_date columns. This resulted in a full table scan, leading to high I/O and disk spills. The planner also failed to utilize any existing indexes, leading to inefficient data retrieval. | CREATE INDEX idx_orders_total_amount ON orders (total_amount); CREATE INDEX idx_orders_order_date ON orders (order_date); VACUUM ANALYZE orders; | SELECT * FROM orders WHERE total_amount > 1000AND order_date BETWEEN '2022-01-01' AND '2022-12-31' ORDER BY total_amount, order_date |
PostgreSQL 16.2 | SELECT * FROM orders WHERE order_date BETWEEN '2022-01-01' AND '2022-12-31' AND customer_id IN (SELECT customer_id FROM customers WHERE country='USA') | {"Plan": {"Node Type": "Seq Scan", "Relation Name": "orders", "Alias": "orders", "Startup Cost": 0.0, "Total Cost": 12345.67, "Plan Rows": 10000, "Plan Width": 200, "Filter": "(order_date >= '2022-01-01'::date) AND (order_date <= '2022-12-31'::date) AND (customer_id = ANY ($1))", "SubPlan": {"Node Type": "Seq Scan", "R... | The query planner chose a sequential scan on the orders table because there is no index on the order_date column, and the filter condition on customer_id is not very selective due to the subquery. Additionally, the subquery on the customers table also results in a sequential scan due to the lack of an index on the coun... | CREATE INDEX idx_orders_order_date ON orders (order_date); CREATE INDEX idx_customers_country ON customers (country); VACUUM ANALYZE orders; VACUUM ANALYZE customers; | SELECT * FROM orders WHERE order_date BETWEEN '2022-01-01' AND '2022-12-31' AND customer_id IN (SELECT customer_id FROM customers WHERE country='USA' AND customer_id = ANY (SELECT customer_id FROM orders WHERE order_date BETWEEN '2022-01-01' AND '2022-12-31')) |
PostgreSQL 16.2 | SELECT * FROM orders o JOIN customers c ON o.customer_id = c.customer_id WHERE o.order_date >= '2022-01-01' AND c.country = 'USA' ORDER BY o.order_total DESC LIMIT 100; | {"Plan": {"Node Type": "Limit", "Plan Rows": 100, "Plan Width": 208, "Actual Rows": 100, "Actual Total Time": 2351.123, "Plans": [{"Node Type": "Sort", "Plan Rows": 100, "Plan Width": 208, "Actual Rows": 100, "Actual Total Time": 2347.112, "Plans": [{"Node Type": "Nested Loop", "Plan Rows": 10000, "Plan Width": 208, "A... | The query planner chose a nested loop join due to the lack of a suitable index on the orders table for the join and filter conditions. This resulted in a full table scan of the orders table, leading to high I/O and a significant increase in query execution time. The sort operation also added to the overall cost, as the... | CREATE INDEX orders_customer_id_order_date ON orders (customer_id, order_date); CREATE INDEX customers_country ON customers (country); VACUUM ANALYZE orders; VACUUM ANALYZE customers; | SELECT * FROM orders o JOIN customers c ON o.customer_id = c.customer_id WHERE o.order_date >= '2022-01-01' AND c.country = 'USA' ORDER BY o.order_total DESC LIMIT 100; |
PostgreSQL 16.2 | SELECT * FROM orders WHERE total_amount > 1000 AND created_at BETWEEN '2022-01-01' AND '2022-12-31' | {"Plan": {"Node Type": "Seq Scan", "Relation Name": "orders", "Alias": "orders", "Startup Cost": 0.0, "Total Cost": 23451.83, "Plan Rows": 1150, "Plan Width": 268}, "Planning Time": 0.128, "Execution Time": 2345.112} | The query planner chose a sequential scan because the existing index on the 'created_at' column is not being utilized due to the additional filter condition 'total_amount > 1000'. This results in a full table scan, leading to high I/O and disk usage. Furthermore, the lack of a composite index on 'total_amount' and 'cre... | CREATE INDEX idx_orders_total_amount_created_at ON orders (total_amount, created_at); VACUUM ANALYZE orders; | SELECT * FROM orders WHERE total_amount > 1000 AND created_at BETWEEN '2022-01-01' AND '2022-12-31' ORDER BY total_amount, created_at; |
PostgreSQL 16.2 | SELECT * FROM orders WHERE total_amount > 1000 AND order_date BETWEEN '2022-01-01' AND '2022-12-31' ORDER BY order_id LIMIT 100 | {"Plan": {"Node Type": "Limit", "Plans": [{"Node Type": "Sort", "Plans": [{"Node Type": "Seq Scan", "Relation Name": "orders", "Alias": "orders", "Rows": 100000, "Total Cost": 25123.89}]}]}} | The query planner chose a sequential scan because the existing index on order_date does not cover the total_amount column, resulting in a full table scan. Additionally, the ORDER BY clause requires a sort operation, which further increases the query execution time. | CREATE INDEX orders_total_amount_order_date_idx ON orders (total_amount, order_date) INCLUDE (order_id); VACUUM ANALYZE orders; | SELECT * FROM orders WHERE total_amount > 1000 AND order_date BETWEEN '2022-01-01' AND '2022-12-31' ORDER BY order_id LIMIT 100; CREATE INDEX IF NOT EXISTS orders_order_id_idx ON orders (order_id); |
PostgreSQL 16.2 | SELECT * FROM orders WHERE order_total > 100 AND order_date > '2022-01-01' ORDER BY order_id LIMIT 100; | {"Plan": {"Node Type": "Limit", "Rows": 100, "Cost": 4321.23, "Plans": [{"Node Type": "Sort", "Rows": 100, "Cost": 4321.23, "Plans": [{"Node Type": "Seq Scan", "Rows": 100000, "Cost": 3211.11, "Filter": "(order_total > 100)", "Tables": "orders"}]}]}} | The query planner chose a sequential scan because there are no suitable indexes on the order_total column, resulting in a full table scan. Additionally, the ORDER BY clause requires a sort operation, which further increases the cost of the query. The planner also chose not to use an index on order_date, likely due to t... | CREATE INDEX idx_orders_order_total ON orders (order_total); CREATE INDEX idx_orders_order_date ON orders (order_date); VACUUM ANALYZE orders; | SELECT * FROM orders WHERE order_total > 100 AND order_date > '2022-01-01' ORDER BY order_id LIMIT 100; -- with index idx_orders_order_total and idx_orders_order_date, the query planner will choose an index scan, reducing the cost of the query. |
PostgreSQL 16.2 | SELECT * FROM orders WHERE total_amount > 1000 AND customer_id IN (SELECT customer_id FROM customers WHERE country='USA'); | {"Plan": {"Node Type": "Seq Scan", "Relation Name": "orders", "Alias": "orders", "Startup Cost": 0.0, "Total Cost": 2391.23, "Plan Rows": 1000, "Plan Width": 268}, "Planning Time": 0.128, "Execution Time": 234.53} | The query planner chose a sequential scan on the orders table because it couldn't effectively use any existing indexes due to the lack of a suitable index on the total_amount column and the subquery on customer_id. This led to a full table scan, resulting in high I/O and disk usage. | CREATE INDEX idx_orders_total_amount ON orders (total_amount); CREATE INDEX idx_customers_country ON customers (country); | SELECT * FROM orders WHERE total_amount > 1000 AND customer_id IN (SELECT customer_id FROM customers WHERE country='USA' AND EXISTS (SELECT 1 FROM orders o2 WHERE o2.customer_id = customers.customer_id AND o2.total_amount > 1000)); |
PostgreSQL 16.2 | SELECT * FROM sales WHERE total_amount > 1000 AND sale_date BETWEEN '2022-01-01' AND '2022-12-31' | {"Plan": {"Node Type": "Seq Scan", "Relation Name": "sales", "Alias": "sales", "Startup Cost": 0.0, "Total Cost": 23451.23, "Plan Rows": 10000, "Plan Width": 148}, "Planning Time": 0.123, "Execution Time": 1234.567, "Buffers": {"shared hit": 123, "shared read": 456, "shared dirtied": 0, "shared written": 0}} | The query planner chose a sequential scan because the WHERE clause filters on two columns (total_amount and sale_date), but there are no indexes on these columns that cover both conditions. As a result, the planner decided to scan the entire table, leading to high I/O and disk spills. | CREATE INDEX idx_sales_total_amount_sale_date ON sales (total_amount, sale_date); VACUUM ANALYZE sales; | SELECT * FROM sales WHERE total_amount > 1000 AND sale_date BETWEEN '2022-01-01' AND '2022-12-31' INDEXED BY idx_sales_total_amount_sale_date |
PostgreSQL 16.2 | SELECT * FROM orders WHERE order_date BETWEEN '2022-01-01' AND '2022-12-31' AND customer_id IN (SELECT customer_id FROM customers WHERE country='USA'); | {"Plan": {"Node Type": "Seq Scan", "Relation Name": "orders", "Alias": "orders", "Startup Cost": 0.0, "Total Cost": 23145.62, "Plan Rows": 13143, "Plan Width": 244, "Filter": "(order_date >= '2022-01-01'::date) AND (order_date <= '2022-12-31'::date) AND (customer_id = ANY ($1))", "Rows Removed by Filter": 81234, "SubPl... | The query planner chose a sequential scan because there are no suitable indexes on the 'order_date' and 'customer_id' columns in the 'orders' table. Additionally, the subquery in the IN clause is not optimized, leading to an additional sequential scan on the 'customers' table. The planner also failed to use an efficien... | CREATE INDEX idx_orders_order_date ON orders (order_date); CREATE INDEX idx_orders_customer_id ON orders (customer_id); CREATE INDEX idx_customers_country ON customers (country); VACUUM ANALYZE orders; VACUUM ANALYZE customers; | WITH usa_customers AS (SELECT customer_id FROM customers WHERE country='USA') SELECT * FROM orders WHERE order_date BETWEEN '2022-01-01' AND '2022-12-31' AND customer_id IN (SELECT customer_id FROM usa_customers); |
PostgreSQL 16.2 | SELECT * FROM orders WHERE order_date > '2022-01-01' AND customer_id IN (SELECT customer_id FROM customers WHERE country = 'USA'); | {"Plan": {"Node Type": "Seq Scan", "Relation Name": "orders", "Alias": "orders", "Startup Cost": 0.0, "Total Cost": 10543.63, "Plan Rows": 10000, "Plan Width": 268}, "Planning Time": 0.128, "Execution Time": 234.542} | The query planner chose a sequential scan on the orders table because the query filters on a non-indexed column (order_date) and uses a subquery to filter on customer_id. This results in high I/O and disk spills due to the large number of rows being scanned. Additionally, the subquery is not optimized, leading to an ad... | CREATE INDEX idx_orders_order_date ON orders (order_date); CREATE INDEX idx_orders_customer_id ON orders (customer_id); CREATE INDEX idx_customers_country ON customers (country); VACUUM ANALYZE orders; VACUUM ANALYZE customers; | SELECT * FROM orders WHERE order_date > '2022-01-01' AND customer_id IN (SELECT customer_id FROM customers WHERE country = 'USA' AND customer_id > 0) AND customer_id > 0; |
PostgreSQL 16.2 | SELECT * FROM customers c JOIN orders o ON c.customer_id = o.customer_id WHERE o.order_total > 1000 AND c.country = 'USA' | {"Plan": {"Node Type": "Hash Join", "Join Type": "inner", "Keys": "customer_id", "Plan Rows": 13579, "Plan Width": 444, "Actual Rows": 13579, "Actual Total Time": 2351.235, "Plans": [{"Node Type": "Seq Scan", "Relation Name": "customers", "Alias": "c", "Plan Rows": 100000, "Plan Width": 200, "Actual Rows": 100000, "Act... | The query planner chose a sub-optimal route because it didn't have sufficient indexing to support the join operation. The seq scans on both tables lead to high I/O and disk spills. Additionally, the lack of statistics on the join columns made it difficult for the planner to estimate the number of rows accurately. | CREATE INDEX idx_orders_customer_id ON orders (customer_id); CREATE INDEX idx_customers_country ON customers (country); VACUUM ANALYZE customers, orders; | SELECT * FROM customers c JOIN orders o ON c.customer_id = o.customer_id WHERE o.order_total > 1000 AND c.country = 'USA' AND c.customer_id IN (SELECT customer_id FROM orders WHERE order_total > 1000) |
PostgreSQL 16.2 | SELECT * FROM orders WHERE total_amount > 1000 AND customer_id IN (SELECT customer_id FROM customers WHERE country = 'USA') | {"Plan": {"Node Type": "Seq Scan", "Parent Relationship": "Outer", "Parallel Aware": false, "Scan Direction": "Forward", "Index Name": null, "Relation Name": "orders", "Alias": "orders", "Startup Cost": 0.0, "Total Cost": 12345.67, "Plan Rows": 1000, "Plan Width": 268, "Filter": "(total_amount > 1000) AND (customer_id ... | The query planner chose a sub-optimal route because the IN operator is not able to utilize indexes efficiently, resulting in a sequential scan of the orders table. Additionally, the subquery in the IN operator is also performing a sequential scan of the customers table, which further increases the execution time. | CREATE INDEX idx_orders_customer_id ON orders (customer_id); CREATE INDEX idx_customers_country ON customers (country); VACUUM ANALYZE customers; VACUUM ANALYZE orders; | SELECT o.* FROM orders o JOIN customers c ON o.customer_id = c.customer_id WHERE o.total_amount > 1000 AND c.country = 'USA' |
PostgreSQL 16.2 | SELECT * FROM orders WHERE customer_id = 123 AND order_date >= '2022-01-01' AND order_date < '2023-01-01' | {"Plan": {"Node Type": "Seq Scan", "Relation Name": "orders", "Alias": "orders", "Startup Cost": 0.0, "Total Cost": 1042.4, "Plan Rows": 100, "Plan Width": 268}, "Planning Time": 0.128, "Execution Time": 10.532} | The query planner chose a sequential scan because the existing index on customer_id does not cover the order_date column, resulting in a full table scan. Additionally, the lack of a composite index on (customer_id, order_date) prevents the optimizer from using a more efficient index scan. | CREATE INDEX orders_customer_id_order_date_idx ON orders (customer_id, order_date); VACUUM ANALYZE orders; | SELECT * FROM orders WHERE customer_id = 123 AND order_date >= '2022-01-01' AND order_date < '2023-01-01'; -- Utilizes the new composite index |
PostgreSQL 16.2 | SELECT * FROM orders WHERE total_amount > 1000 AND customer_id IN (SELECT id FROM customers WHERE country = 'USA') | {"Plan": {"Node Type": "Seq Scan", "Relation Name": "orders", "Alias": "orders", "Startup Cost": 0.0, "Total Cost": 2381.83, "Plan Rows": 1000, "Plan Width": 136}, "Plans": [{"Node Type": "Seq Scan", "Relation Name": "customers", "Alias": "customers", "Startup Cost": 0.0, "Total Cost": 10.7, "Plan Rows": 100, "Plan Wid... | The query planner chose a sequential scan due to the lack of a suitable index on the total_amount column and the subquery on the customers table. This results in a full table scan of the orders table, leading to high I/O and disk spills. Furthermore, the subquery is not optimized, causing additional overhead. | CREATE INDEX idx_orders_total_amount ON orders (total_amount); CREATE INDEX idx_customers_country ON customers (country); VACUUM ANALYZE orders; VACUUM ANALYZE customers; | SELECT * FROM orders WHERE total_amount > 1000 AND customer_id IN (SELECT id FROM customers WHERE country = 'USA' AND id IN (SELECT customer_id FROM orders WHERE total_amount > 1000)) |
PostgreSQL 16.2 | SELECT * FROM customers JOIN orders ON customers.customer_id = orders.customer_id WHERE customers.country = 'USA' AND orders.order_date > '2022-01-01' | {"Plan": {"Node Type": "Hash Join", "Join Type": "inner", "Startup Cost": 1234.56, "Total Cost": 7890.12, "Rows": 10000, "Plans": [{"Node Type": "Seq Scan", "Relation Name": "customers", "Alias": "customers", "Startup Cost": 0.0, "Total Cost": 1234.56, "Rows": 10000, "Filter": "((country = 'USA'::text) AND (customer_id... | The query planner chose a sub-optimal route due to the lack of suitable indexes on the join columns (customer_id) and filter columns (country, order_date). This resulted in a Hash Join with two sequential scans, leading to high I/O and computation costs. | CREATE INDEX idx_customers_country ON customers (country); CREATE INDEX idx_customers_customer_id ON customers (customer_id); CREATE INDEX idx_orders_customer_id ON orders (customer_id); CREATE INDEX idx_orders_order_date ON orders (order_date); VACUUM ANALYZE customers; VACUUM ANALYZE orders; | SELECT * FROM customers JOIN orders ON customers.customer_id = orders.customer_id WHERE customers.country = 'USA' AND orders.order_date > '2022-01-01' AND customers.customer_id > 0 |
PostgreSQL 16.2 | SELECT * FROM orders WHERE total_amount > 1000 AND created_at > '2022-01-01' ORDER BY created_at DESC LIMIT 100; | {"Plan": {"Node Type": "Limit", "Plan Rows": 100, "Plan Width": 244, "Actual Rows": 100, "Actual Total Time": 2351.235, "Plans": [{"Node Type": "Sort", "Plan Rows": 100, "Plan Width": 244, "Actual Rows": 100, "Actual Total Time": 2347.124, "Plans": [{"Node Type": "Seq Scan", "Plan Rows": 10000, "Plan Width": 244, "Actu... | The query planner chose a sequential scan because there are no suitable indexes on the total_amount column, and the created_at column is not used as a filter, only for ordering. The planner estimated that scanning the entire table would be faster than using an index on created_at, given the relatively low selectivity o... | CREATE INDEX idx_orders_total_amount ON orders (total_amount); CREATE INDEX idx_orders_created_at ON orders (created_at DESC); VACUUM ANALYZE orders; | SELECT * FROM orders WHERE total_amount > 1000 AND created_at > '2022-01-01' ORDER BY created_at DESC LIMIT 100; |
PostgreSQL 16.2 | SELECT * FROM orders WHERE order_date BETWEEN '2022-01-01' AND '2022-01-31' AND customer_id IN (SELECT customer_id FROM customers WHERE country='USA') | {"Plan": {"Node Type": "Seq Scan", "Relation Name": "orders", "Alias": "orders", "Startup Cost": 0.0, "Total Cost": 2391.23, "Plan Rows": 1000, "Plan Width": 244}, "Plans": [{"Node Type": "Seq Scan", "Relation Name": "customers", "Alias": "customers", "Startup Cost": 0.0, "Total Cost": 10.7, "Plan Rows": 100, "Plan Wid... | The query planner chose a sequential scan on the orders table because the query filter is not effectively utilizing an index. Although there is an index on the order_date column, the additional filter condition on customer_id requires a sequential scan to evaluate the subquery. Furthermore, the subquery is not optimize... | CREATE INDEX idx_orders_order_date ON orders (order_date); CREATE INDEX idx_orders_customer_id ON orders (customer_id); CREATE INDEX idx_customers_country ON customers (country); VACUUM ANALYZE orders; VACUUM ANALYZE customers; | SELECT * FROM orders WHERE order_date BETWEEN '2022-01-01' AND '2022-01-31' AND customer_id IN (SELECT customer_id FROM customers WHERE country='USA' AND customer_id = orders.customer_id) ORDER BY order_date |
PostgreSQL 16.2 | SELECT * FROM orders WHERE order_date >= '2022-01-01' AND order_date <= '2022-12-31' AND customer_id IN (SELECT customer_id FROM customers WHERE country = 'USA') | {"Plan": {"Node Type": "Seq Scan", "Relation Name": "orders", "Alias": "orders", "Startup Cost": 0.0, "Total Cost": 23543.29, "Plan Rows": 10000, "Plan Width": 268, "Filter": "(order_date >= '2022-01-01'::date) AND (order_date <= '2022-12-31'::date) AND (customer_id = ANY ($1))", "Rows Removed by Filter": 5000, "SubPla... | The query planner chose a sequential scan on the orders table because it did not have sufficient statistics or an index to support a more efficient access method. The filter condition on order_date and customer_id did not effectively reduce the number of rows, resulting in a full table scan. Additionally, the subquery ... | CREATE INDEX idx_orders_order_date ON orders (order_date); CREATE INDEX idx_orders_customer_id ON orders (customer_id); CREATE INDEX idx_customers_country ON customers (country); VACUUM ANALYZE orders; VACUUM ANALYZE customers; | SELECT * FROM orders WHERE order_date >= '2022-01-01' AND order_date <= '2022-12-31' AND customer_id IN (SELECT customer_id FROM customers WHERE country = 'USA') AND order_date BETWEEN '2022-01-01' AND '2022-12-31'; |
PostgreSQL 16.2 | SELECT * FROM customers c JOIN orders o ON c.customer_id = o.customer_id WHERE c.country = 'USA' AND o.order_date >= '2022-01-01' | {"Plan": {"Node Type": "Nested Loop", "Join Type": "Inner", "Plans": [{"Node Type": "Seq Scan", "Relation Name": "customers", "Alias": "c", "Rows": 100000, "Filter": "((country = 'USA'::text))"}, {"Node Type": "Seq Scan", "Relation Name": "orders", "Alias": "o", "Rows": 1000000, "Filter": "((order_date >= '2022-01-01':... | The query planner chose a Nested Loop join because the join condition is not supported by an index on either table. Additionally, the filters on country and order_date are not selective enough to reduce the number of rows being scanned, resulting in a full table scan of both tables. This leads to high I/O and CPU usage... | CREATE INDEX idx_customers_country ON customers (country); CREATE INDEX idx_orders_customer_id ON orders (customer_id); CREATE INDEX idx_orders_order_date ON orders (order_date); VACUUM ANALYZE customers; VACUUM ANALYZE orders; | SELECT * FROM customers c JOIN orders o ON c.customer_id = o.customer_id AND c.country = 'USA' WHERE o.order_date >= '2022-01-01' AND c.country = 'USA' |
PostgreSQL 16.2 | SELECT * FROM orders WHERE total_amount > 1000 AND created_at > '2022-01-01' ORDER BY total_amount DESC LIMIT 10 | {"Plan": {"Node Type": "Limit", "Plan Rows": 10, "Plan Width": 148, "Startup Cost": 2345.32, "Total Cost": 2346.13, "Plans": [{"Node Type": "Sort", "Plan Rows": 10, "Plan Width": 148, "Startup Cost": 2345.32, "Total Cost": 2346.13, "Plans": [{"Node Type": "Seq Scan", "Plan Rows": 10000, "Plan Width": 148, "Startup Cost... | The query planner chose a sequential scan on the orders table because there were no suitable indexes to support the filter conditions. The lack of an index on the total_amount and created_at columns led to a full table scan, resulting in high I/O and slower performance. Additionally, the ORDER BY clause required a sort... | CREATE INDEX idx_orders_total_amount_created_at ON orders (total_amount, created_at); VACUUM ANALYZE orders; | SELECT * FROM orders WHERE total_amount > 1000 AND created_at > '2022-01-01' ORDER BY total_amount DESC LIMIT 10 |
PostgreSQL 16.2 | SELECT * FROM orders WHERE total_amount > 100 AND created_at > '2022-01-01' AND customer_id IN (SELECT id FROM customers WHERE country = 'USA') | {"Plan": {"Node Type": "Seq Scan", "Relation Name": "orders", "Alias": "orders", "Startup Cost": 0.0, "Total Cost": 10234.2, "Plan Rows": 1000, "Plan Width": 268}, "Planning Time": 0.129, "Execution Time": 1056.231} | The query planner chose a sequential scan on the orders table due to the lack of a suitable index on the total_amount and created_at columns. Additionally, the subquery on the customers table is not optimized, leading to a full table scan. The planner also failed to leverage the customer_id column as a join key, result... | CREATE INDEX idx_orders_total_amount ON orders (total_amount); CREATE INDEX idx_orders_created_at ON orders (created_at); CREATE INDEX idx_customers_country ON customers (country); VACUUM ANALYZE orders; VACUUM ANALYZE customers; | SELECT o.* FROM orders o JOIN customers c ON o.customer_id = c.id WHERE o.total_amount > 100 AND o.created_at > '2022-01-01' AND c.country = 'USA' |
PostgreSQL 16.2 | SELECT * FROM orders WHERE order_date > '2022-01-01' AND customer_id IN (SELECT customer_id FROM customers WHERE country = 'USA') | {"Plan": {"Node Type": "Nested Loop", "Join Type": "Inner", "Plans": [{"Node Type": "Seq Scan", "Relation Name": "orders", "Alias": "orders", "Startup Cost": 0.0, "Total Cost": 1042.4, "Plan Rows": 1000, "Plan Width": 268}, {"Node Type": "Seq Scan", "Relation Name": "customers", "Alias": "customers", "Startup Cost": 0.... | The query planner chose a nested loop join because it estimated that the subquery would return a small number of rows, but the actual number of rows returned by the subquery is large, leading to a high number of sequential scans on the orders table. Additionally, the lack of an index on the order_date column and the cu... | CREATE INDEX idx_orders_order_date ON orders (order_date); CREATE INDEX idx_orders_customer_id ON orders (customer_id); CREATE INDEX idx_customers_country ON customers (country); VACUUM ANALYZE orders; VACUUM ANALYZE customers; | SELECT * FROM orders WHERE order_date > '2022-01-01' AND customer_id IN (SELECT customer_id FROM customers WHERE country = 'USA' AND customer_id = orders.customer_id) ORDER BY order_date |
ClickHouse 24.3 | SELECT l.season_id, l.player_id, SUM(ps.kills) AS total_kills, AVG(ps.score) AS average_score FROM leaderboards AS l INNER JOIN player_stats AS ps ON l.player_id = ps.player_id INNER JOIN match_sessions AS ms ON ps.match_id = ms.match_id WHERE ms.region = 'EU' AND ms.game_mode = 'Ranked' AND l.mmr_rating > 1500 AND ms.... | EXPLAIN
SELECT l.season_id, l.player_id, SUM(ps.kills) AS total_kills, AVG(ps.score) AS average_score
FROM leaderboards AS l
INNER JOIN player_stats AS ps ON l.player_id = ps.player_id
INNER JOIN match_sessions AS ms ON ps.match_id = ms.match_id
WHERE ms.region = 'EU' AND ms.game_mode = 'Ranked' AND l.mmr_rating >... | The query planner chose this sub-optimal route because it was attempting to join the leaderboards table with the player_stats table on the player_id column, and then join the result with the match_sessions table on the match_id column, which resulted in a large number of rows being scanned and joined, leading to a high... | CREATE INDEX idx_leaderboards_player_id ON leaderboards (player_id); CREATE INDEX idx_player_stats_match_id ON player_stats (match_id); CREATE INDEX idx_match_sessions_region ON match_sessions (region); CREATE INDEX idx_match_sessions_game_mode ON match_sessions (game_mode); CREATE INDEX idx_match_sessions_started_at O... | SELECT l.season_id, l.player_id, SUM(ps.kills) AS total_kills, AVG(ps.score) AS average_score FROM leaderboards AS l INNER JOIN player_stats AS ps ON l.player_id = ps.player_id INNER JOIN match_sessions AS ms ON ps.match_id = ms.match_id WHERE ms.region = 'EU' AND ms.game_mode = 'Ranked' AND l.mmr_rating > 1500 AND ms.... |
PostgreSQL 16.2 | SELECT oi.item_id, oi.sku_id, i.available_qty, p.discount_pct FROM order_items oi JOIN inventory_stock i ON oi.sku_id = i.sku_id AND oi.warehouse_id = i.warehouse_id JOIN promotions p ON oi.sku_id = p.sku_id WHERE oi.unit_price > 49.99 AND i.available_qty > 50 AND p.valid_from < '2024-09-16' AND p.valid_until > '2024-0... | EXPLAIN (ANALYZE, BUFFERS) SELECT oi.item_id, oi.sku_id, i.available_qty, p.discount_pct FROM order_items oi JOIN inventory_stock i ON oi.sku_id = i.sku_id AND oi.warehouse_id = i.warehouse_id JOIN promotions p ON oi.sku_id = p.sku_id WHERE oi.unit_price > 49.99 AND i.available_qty > 50 AND p.valid_from < '2024-09-16' ... | The query planner chose this sub-optimal route because it had to perform a sequential scan on the order_items table due to the lack of a suitable index on the unit_price column, resulting in excessive heap fetches on the wide table. Furthermore, the planner decided to use a nested loop join for the order_items and inve... | CREATE INDEX order_items_unit_price_idx ON order_items (unit_price); CREATE INDEX inventory_stock_available_qty_idx ON inventory_stock (available_qty) INCLUDE (sku_id, warehouse_id); VACUUM ANALYZE order_items; VACUUM ANALYZE inventory_stock; | SELECT oi.item_id, oi.sku_id, i.available_qty, p.discount_pct FROM order_items oi JOIN inventory_stock i ON oi.sku_id = i.sku_id AND oi.warehouse_id = i.warehouse_id JOIN promotions p ON oi.sku_id = p.sku_id WHERE oi.unit_price > 49.99 AND i.available_qty > 50 AND p.valid_from < '2024-09-16' AND p.valid_until > '2024-0... |
PostgreSQL 16.2 | UPDATE l AS leaderboards SET mmr_rating = mmr_rating + 10 FROM ( SELECT player_id, AVG(kills) AS avg_kills FROM ps AS player_stats WHERE match_id IN ( SELECT match_id FROM ms AS match_sessions WHERE region = 'EU' AND game_mode = 'Ranked' ) GROUP BY player_id HAVING AVG(kills) > 5 ) AS subquery WHERE l.player_id = subqu... | -> Update on leaderboards l (cost=2345.67..3456.89 rows=1234 width=234)
-> Hash Join (cost=1234.56..2345.67 rows=1234 width=345)
Hash Cond: (l.player_id = subquery.player_id)
-> Seq Scan on leaderboards l (cost=0.00..1234.56 rows=1234 width=234)
-> Hash (cost=1234.56..1234.56 rows=... | The query planner chose this sub-optimal route because it had to perform a sequential scan on the match_sessions table to filter the region and game mode, and then another sequential scan on the player_stats table to get the average kills for each player. The lack of an index on the player_id column in the leaderboards... | CREATE INDEX idx_leaderboards_player_id ON leaderboards (player_id); CREATE INDEX idx_match_sessions_region_game_mode ON match_sessions (region, game_mode); VACUUM ANALYZE leaderboards; VACUUM ANALYZE match_sessions; VACUUM ANALYZE player_stats; | WITH ranked_matches AS ( SELECT match_id FROM match_sessions WHERE region = 'EU' AND game_mode = 'Ranked' ), player_kills AS ( SELECT player_id, AVG(kills) AS avg_kills FROM player_stats WHERE match_id IN ( SELECT match_id FROM ranked_matches ) GROUP BY player_id HAVING AVG(kills) > 5 ) UPDATE leaderboards l SET mmr_ra... |
ClickHouse 24.3 | SELECT d_t.metric_type, AVG(d_t.val) AS avg_val FROM device_telematics d_t INNER JOIN devices d ON d_t.device_id = d.device_id WHERE d_t.recorded_at > '2022-01-01' AND d_t.firmware_v = '2.5.1' AND d.region = 'North America' GROUP BY d_t.metric_type ORDER BY avg_val DESC LIMIT 10; | {"roots":[{"id":"1","type":"Aggregating","rows":"10000","bytes":"100000","filter":"(device_telematics.recorded_at > 2022-01-01) AND (device_telematics.firmware_v = '2.5.1') AND (devices.region = 'North America')","key":"device_telematics.metric_type","aggregates":[{"name":"avg_val","type":"AggregateFunction","args":[{"... | The query planner chose this sub-optimal route because the skip index on the device_telematics table has a granularity that is too broad, resulting in excessive granule loading. The query is trying to filter on recorded_at, firmware_v, and region, but the skip index is only defined on device_id, which is not selective ... | CREATE INDEX idx_device_telematics_recorded_at ON device_telematics (recorded_at) TYPE minmax GRANULARITY 4; CREATE INDEX idx_devices_region ON devices (region) TYPE set GRANULARITY 4; ALTER TABLE device_telematics ADD COLUMN firmware_v_idx AS intHash32(firmware_v); CREATE INDEX idx_device_telematics_firmware_v ON devi... | SELECT d_t.metric_type, AVG(d_t.val) AS avg_val FROM device_telematics d_t INNER JOIN devices d ON d_t.device_id = d.device_id AND d.region = 'North America' WHERE d_t.recorded_at > '2022-01-01' AND d_t.firmware_v = '2.5.1' GROUP BY d_t.metric_type ORDER BY avg_val DESC LIMIT 10; |
MySQL 8.0 | SELECT s.tracking_num, cr.base_rate, pe.checkpoint FROM shipments s JOIN parcel_events pe ON s.tracking_num = pe.tracking_num JOIN carrier_rates cr ON s.carrier_id = cr.carrier_id WHERE s.departed_at > '2022-01-01' AND s.status = 'in_transit' AND pe.scan_time > '2022-06-01' AND cr.zone_id = 5 AND cr.weight_kg > 10 | {"query_block": {"select_id": 1, "cost_info": {"query_cost": "12345.67"}, "table": {"table_name": "s", "access_type": "ALL", "rows": 12345, "filtered": 10, "extra": "Using filesort"}, "table": {"table_name": "pe", "access_type": "ALL", "rows": 67890, "filtered": 5, "extra": "Using join buffer (Block Nested Loop)"}, "ta... | The query planner chose this sub-optimal route because it had to perform a merge join on the shipments, parcel_events, and carrier_rates tables. Since there were no suitable indexes on the departed_at and scan_time columns, the planner had to use a filesort operation to sort the rows, resulting in high memory usage and... | CREATE INDEX idx_shipments_departed_at ON shipments (departed_at); CREATE INDEX idx_parcel_events_scan_time ON parcel_events (scan_time); CREATE INDEX idx_carrier_rates_zone_id ON carrier_rates (zone_id); CREATE INDEX idx_shipments_carrier_id ON shipments (carrier_id); | SELECT s.tracking_num, cr.base_rate, pe.checkpoint FROM shipments s JOIN parcel_events pe ON s.tracking_num = pe.tracking_num AND pe.scan_time > '2022-06-01' JOIN carrier_rates cr ON s.carrier_id = cr.carrier_id AND cr.zone_id = 5 AND cr.weight_kg > 10 WHERE s.departed_at > '2022-01-01' AND s.status = 'in_transit' |
MariaDB 11.2 | SELECT t1.imp_id, t2.click_id, t2.conversion_val FROM ad_impressions t1 JOIN click_events t2 ON t1.imp_id = t2.imp_id WHERE t1.bid_cpm > 5.25 AND t2.clicked_at > '2022-07-15' AND t1.publisher_id IN (123, 456, 789) ORDER BY t1.served_at DESC LIMIT 10000; | EXPLAIN format=json SELECT t1.imp_id, t2.click_id, t2.conversion_val FROM ad_impressions t1 JOIN click_events t2 ON t1.imp_id = t2.imp_id WHERE t1.bid_cpm > 5.25 AND t2.clicked_at > '2022-07-15' AND t1.publisher_id IN (123, 456, 789) ORDER BY t1.served_at DESC LIMIT 10000; | The query planner chose this sub-optimal route because it failed to utilize an efficient index for the join operation between the ad_impressions and click_events tables. This resulted in a full table scan of the ad_impressions table, followed by a nested loop join with the click_events table, leading to excessive I/O a... | CREATE INDEX idx_ad_impressions_imp_id ON ad_impressions (imp_id); CREATE INDEX idx_click_events_imp_id ON click_events (imp_id); CREATE INDEX idx_ad_impressions_publisher_id ON ad_impressions (publisher_id); CREATE INDEX idx_ad_impressions_bid_cpm ON ad_impressions (bid_cpm); CREATE INDEX idx_click_events_clicked_at O... | SELECT t1.imp_id, t2.click_id, t2.conversion_val FROM ad_impressions t1 JOIN click_events t2 ON t1.imp_id = t2.imp_id AND t2.clicked_at > '2022-07-15' WHERE t1.bid_cpm > 5.25 AND t1.publisher_id IN (123, 456, 789) ORDER BY t1.served_at DESC LIMIT 10000; |
ClickHouse 24.3 | WITH ranked_matches AS (SELECT match_id, region, game_mode, started_at, ended_at, ROW_NUMBER() OVER (PARTITION BY region ORDER BY started_at DESC) AS row_num FROM match_sessions WHERE started_at > '2022-01-01' AND game_mode = 'ranked') SELECT rm.match_id, ps.player_id, ps.kills, ps.score, ps.ping_ms FROM ranked_matches... | {"stages":{"1":{"name":"NestedLoopJoin","rows":"100000","bytes":"10000000","filter":"(rm.match_id = ps.match_id)"},"2":{"name":"Filter","rows":"10000","bytes":"1000000","filter":"(rm.row_num <= 10)"},"3":{"name":"Window","rows":"10000","bytes":"1000000","filter":"(started_at > \u00272022-01-01\u0027)"},"4":{"name":"Tab... | The query planner chose this sub-optimal route because the Window function in the materialized CTE prevents predicate pushdown into the partitioned match_sessions table. As a result, the query planner is forced to perform a full table scan on match_sessions, leading to a significant increase in execution time. Furtherm... | CREATE INDEX idx_match_sessions_started_at ON match_sessions (started_at); CREATE INDEX idx_match_sessions_game_mode ON match_sessions (game_mode); CREATE INDEX idx_player_stats_match_id ON player_stats (match_id); | SELECT rm.match_id, ps.player_id, ps.kills, ps.score, ps.ping_ms FROM (SELECT match_id, region, game_mode, started_at, ended_at, ROW_NUMBER() OVER (PARTITION BY region ORDER BY started_at DESC) AS row_num FROM match_sessions WHERE started_at > '2022-01-01' AND game_mode = 'ranked' AND started_at <= (SELECT MAX(started_... |
MySQL 8.0 | SELECT t1.tracking_num, t2.base_rate, t3.scan_time FROM shipments t1 INNER JOIN carrier_rates t2 ON t1.carrier_id = t2.carrier_id INNER JOIN parcel_events t3 ON t1.tracking_num = t3.tracking_num WHERE t1.status = 'IN_TRANSIT' AND t2.zone_id = 5 AND t3.checkpoint = 'HUB_SCAN' | {"query_block": {"select_id": 1, "cost_info": {"query_cost": "20312.60"}, "table": {"table_name": "t1", "access_type": "ALL", "rows": 100021, "filtered": 33.33, "cost_info": {"read_cost": "10212.10", "eval_cost": "1010.50", "prefix_cost": "11322.60"}}, "table": {"table_name": "t2", "access_type": "ALL", "rows": 50012, ... | The query planner chose this sub-optimal route because it was unable to effectively utilize indexes due to the absence of a composite join key, leading to full table scans and Cartesian product row multiplication. This resulted in a significant increase in query cost, with a total estimated cost of 20312.60. Furthermor... | CREATE INDEX idx_shipments_carrier_id ON shipments (carrier_id); CREATE INDEX idx_carrier_rates_carrier_id_zone_id ON carrier_rates (carrier_id, zone_id); CREATE INDEX idx_parcel_events_tracking_num_checkpoint ON parcel_events (tracking_num, checkpoint); | SELECT t1.tracking_num, t2.base_rate, t3.scan_time FROM shipments t1 INNER JOIN carrier_rates t2 ON t1.carrier_id = t2.carrier_id AND t2.zone_id = 5 INNER JOIN parcel_events t3 ON t1.tracking_num = t3.tracking_num AND t3.checkpoint = 'HUB_SCAN' WHERE t1.status = 'IN_TRANSIT' |
SQLite 3.45 | SELECT T1.imp_id, T2.click_id, T1.campaign_id, T2.conversion_val FROM ad_impressions AS T1 INNER JOIN click_events AS T2 ON T1.imp_id = T2.imp_id WHERE T1.publisher_id = 54321 AND T1.served_at > '2022-01-01' AND T2.clicked_at < '2022-12-31' AND T1.bid_cpm > 5.00 AND T2.conversion_val > 0.50 | EXPLAIN QUERY PLAN SELECT T1.imp_id, T2.click_id, T1.campaign_id, T2.conversion_val FROM ad_impressions AS T1 INNER JOIN click_events AS T2 ON T1.imp_id = T2.imp_id WHERE T1.publisher_id = 54321 AND T1.served_at > '2022-01-01' AND T2.clicked_at < '2022-12-31' AND T1.bid_cpm > 5.00 AND T2.conversion_val > 0.50; -- The p... | The query planner chose this sub-optimal route because the join between ad_impressions and click_events is not properly indexed, leading to a full table scan of ad_impressions and a nested loop join with click_events. This results in high I/O and CPU costs, as the database engine needs to scan the entire ad_impressions... | CREATE INDEX idx_ad_impressions_imp_id ON ad_impressions (imp_id); CREATE INDEX idx_click_events_imp_id ON click_events (imp_id); CREATE INDEX idx_ad_impressions_publisher_id_served_at ON ad_impressions (publisher_id, served_at); CREATE INDEX idx_click_events_clicked_at ON click_events (clicked_at); | SELECT T1.imp_id, T2.click_id, T1.campaign_id, T2.conversion_val FROM ad_impressions AS T1 INNER JOIN click_events AS T2 ON T1.imp_id = T2.imp_id AND T2.clicked_at < '2022-12-31' AND T2.conversion_val > 0.50 WHERE T1.publisher_id = 54321 AND T1.served_at > '2022-01-01' AND T1.bid_cpm > 5.00 |
MariaDB 11.2 | SELECT * FROM audit_logs al JOIN workspace_users wu ON al.workspace_id = wu.workspace_id WHERE wu.role = 'admin' AND al.event_time > '2022-01-01 00:00:00' ORDER BY al.id LIMIT 10 OFFSET 1000000 | {"query_block": {"select_id": 1, "cost": 234151.62, "rows": 10, "filtered": 100, "table": {"table_name": "al", "access_type": "range", "possible_keys": ["PRIMARY", "idx_workspace_id"], "key": "PRIMARY", "key_len": "8", "ref": null, "rows": 1126975, "filtered": 100, "extra": "Using index condition; Using filesort"}}} | The query planner chose this sub-optimal route because the large OFFSET value of 1000000 is causing the database to sequentially traverse over a million rows of historical audit records, resulting in a high cost of 234151.62, which indicates a significant performance bottleneck. Furthermore, the lack of a suitable inde... | CREATE INDEX idx_event_time ON audit_logs (event_time); CREATE INDEX idx_role ON workspace_users (role); ALTER TABLE audit_logs ADD COLUMN id_int INT; UPDATE audit_logs SET id_int = id; CREATE INDEX idx_id_int ON audit_logs (id_int); | SELECT * FROM audit_logs al JOIN workspace_users wu ON al.workspace_id = wu.workspace_id WHERE wu.role = 'admin' AND al.event_time > '2022-01-01 00:00:00' ORDER BY al.id_int LIMIT 10 OFFSET 1000000 |
MySQL 8.0 | SELECT oi.item_id, oi.order_id, oi.sku_id, oi.quantity, oi.unit_price, i.available_qty, p.discount_pct FROM order_items oi JOIN inventory_stock i ON oi.sku_id = i.sku_id AND oi.warehouse_id = i.warehouse_id JOIN promotions p ON oi.sku_id = p.sku_id WHERE oi.unit_price > 19.99 AND i.available_qty > 50 AND p.valid_from <... | {"query_block": {"select_id": 1, "cost_info": {"query_cost": 2345.67}, "table": {"table_name": "oi", "access_type": "ALL", "rows": 12345, "filtered": 12.34, "index_condition": null}, "table": {"table_name": "i", "access_type": "ALL", "rows": 6789, "filtered": 23.45, "index_condition": null}, "table": {"table_name": "p"... | The query planner chose this sub-optimal route because it had to perform a full table scan on order_items, inventory_stock, and promotions tables due to the lack of a suitable index on the join columns and filter conditions. The absence of an index on the unit_price column in order_items and available_qty column in inv... | CREATE INDEX idx_order_items_sku_warehouse_unit_price ON order_items (sku_id, warehouse_id, unit_price); CREATE INDEX idx_inventory_stock_sku_warehouse_available_qty ON inventory_stock (sku_id, warehouse_id, available_qty); CREATE INDEX idx_promotions_sku_valid_from_valid_until ON promotions (sku_id, valid_from, valid_... | SELECT oi.item_id, oi.order_id, oi.sku_id, oi.quantity, oi.unit_price, i.available_qty, p.discount_pct FROM order_items oi USE INDEX (idx_order_items_sku_warehouse_unit_price) JOIN inventory_stock i USE INDEX (idx_inventory_stock_sku_warehouse_available_qty) ON oi.sku_id = i.sku_id AND oi.warehouse_id = i.warehouse_id ... |
ClickHouse 24.3 | SELECT SUM(amount) AS total_amount FROM ledger_entries AS le JOIN accounts AS a ON le.account_id = a.id WHERE le.posting_date > '2022-01-01' AND a.risk_tier = 3 AND le.status = 'pending' | {"trees":{"0":{"type":"Aggregate","inputs":{"0":{"type":"Join","inputs":{"0":{"type":"Filter","inputs":{"0":{"type":"TableScan","arguments":{"table":"ledger_entries"}}},"filter":"(posting_date > \u00272022-01-01\u0027)"},"1":{"type":"TableScan","arguments":{"table":"accounts"}}},"join_on":"(account_id = id)","join_type... | The query planner chose this sub-optimal route because the ledger_entries table does not have a composite index on the account_id and posting_date columns, which are used in the WHERE clause. As a result, the query planner performed a full table scan on the ledger_entries table, joining it with the accounts table on th... | CREATE INDEX idx_ledger_entries_account_id_posting_date ON ledger_entries (account_id, posting_date); | SELECT SUM(amount) AS total_amount FROM ledger_entries AS le JOIN accounts AS a ON le.account_id = a.id AND le.posting_date > '2022-01-01' WHERE a.risk_tier = 3 AND le.status = 'pending' |
SQLite 3.45 | SELECT t1.admitted_at, t2.numeric_result, t3.charge_cents FROM patient_admissions t1 INNER JOIN lab_orders t2 ON t1.admission_id = t2.admission_id INNER JOIN billing_codes t3 ON t1.admission_id = t3.admission_id WHERE t2.test_code = 'HB_A1C' AND t1.discharge_date IS NOT NULL AND t3.icd10 = 'E11.9' AND t1.clinic_id = 42 | EXPLAIN QUERY PLAN SELECT t1.admitted_at, t2.numeric_result, t3.charge_cents FROM patient_admissions t1 INNER JOIN lab_orders t2 ON t1.admission_id = t2.admission_id INNER JOIN billing_codes t3 ON t1.admission_id = t3.admission_id WHERE t2.test_code = 'HB_A1C' AND t1.discharge_date IS NOT NULL AND t3.icd10 = 'E11.9' AN... | The query planner chose this sub-optimal route because the admission_id columns in lab_orders and billing_codes tables are not indexed, resulting in a full table scan for each join operation, leading to a significant increase in the number of rows being scanned and joined, and ultimately causing the query to perform po... | CREATE INDEX idx_admission_id ON lab_orders (admission_id); CREATE INDEX idx_admission_id_2 ON billing_codes (admission_id); CREATE INDEX idx_test_code ON lab_orders (test_code); CREATE INDEX idx_icd10 ON billing_codes (icd10); CREATE INDEX idx_clinic_id ON patient_admissions (clinic_id); | SELECT t1.admitted_at, t2.numeric_result, t3.charge_cents FROM patient_admissions t1 INNER JOIN lab_orders t2 ON t1.admission_id = t2.admission_id INNER JOIN billing_codes t3 ON t1.admission_id = t3.admission_id WHERE t2.test_code = 'HB_A1C' AND t1.discharge_date IS NOT NULL AND t3.icd10 = 'E11.9' AND t1.clinic_id = 42 |
CockroachDB v23.2 | SELECT * FROM ad_impressions ai WHERE ai.imp_id IN (SELECT ce.imp_id FROM click_events ce WHERE ce.conversion_val > 10.50 AND ce.clicked_at > '2022-01-01') AND ai.publisher_id = 854 AND (ai.campaign_id ->> 'advertiser_name') = 'EpicTech Inc.' AND ai.served_at > '2022-06-01' | {"planning_time": 12.43, "execution_time": 2341.19, "plans": [{"plan": {"nodes": [{"type": "Filter", "filter": "(imp_id IN (...)) AND (publisher_id = 854) AND ((campaign_id ->> 'advertiser_name') = 'EpicTech Inc.') AND (served_at > '2022-06-01')", "nodes": [{"type": "Seq Scan", "relation": "ad_impressions", " alias": "... | The query planner chose a sequential scan of the ad_impressions table because the filter condition involves a JSONB payload extraction using the ->> operator, which prevents the optimizer from using any existing indexes on the campaign_id column. The lack of a GIN expression index on the campaign_id column forces the p... | CREATE INDEX idx_ad_impressions_campaign_id_gin ON ad_impressions USING GIN (campaign_id jsonb_path_ops); | SELECT * FROM ad_impressions ai WHERE ai.imp_id IN (SELECT ce.imp_id FROM click_events ce WHERE ce.conversion_val > 10.50 AND ce.clicked_at > '2022-01-01') AND ai.publisher_id = 854 AND ai.campaign_id @> '{"advertiser_name": "EpicTech Inc."}' AND ai.served_at > '2022-06-01' |
CockroachDB v23.2 | SELECT firmware_v, AVG(val) AS avg_val, MAX(recorded_at) AS max_recorded_at FROM d_t AS dt JOIN devices AS d ON dt.device_id = d.device_id WHERE d.region = 'North America' AND dt.metric_type = 'temperature' GROUP BY firmware_v, d.fleet_id ORDER BY avg_val DESC LIMIT 100; | EXPLAIN (ANALYZE, BUFFERS) SELECT firmware_v, AVG(val) AS avg_val, MAX(recorded_at) AS max_recorded_at FROM d_t AS dt JOIN devices AS d ON dt.device_id = d.device_id WHERE d.region = 'North America' AND dt.metric_type = 'temperature' GROUP BY firmware_v, d.fleet_id ORDER BY avg_val DESC LIMIT 100;
-> GroupAggregate
... | The query planner chose this sub-optimal route because it had to perform a high-cardinality GROUP BY operation on the large device_telematics table, which led to the exhaustion of work_mem and spilling to disk, causing significant performance degradation. Additionally, the lack of indexes on the join and filter columns... | CREATE INDEX idx_device_telematics_device_id ON device_telematics (device_id); CREATE INDEX idx_devices_device_id ON devices (device_id); CREATE INDEX idx_device_telematics_metric_type ON device_telematics (metric_type); ALTER TABLE devices ADD INDEX idx_devices_region (region); VACUUM ANALYZE device_telematics; VACUUM... | WITH ranked_firmware AS (SELECT firmware_v, d.fleet_id, AVG(dt.val) AS avg_val, MAX(dt.recorded_at) AS max_recorded_at, ROW_NUMBER() OVER (PARTITION BY firmware_v ORDER BY AVG(dt.val) DESC) AS row_num FROM device_telematics dt JOIN devices d ON dt.device_id = d.device_id WHERE d.region = 'North America' AND dt.metric_t... |
MariaDB 11.2 | SELECT oi.item_id, oi.sku_id, oi.quantity, oi.unit_price, i.available_qty FROM order_items oi INNER JOIN inventory_stock i ON oi.sku_id = i.sku_id AND oi.warehouse_id = i.warehouse_id WHERE oi.unit_price > 50.00 AND i.available_qty > 100 AND oi.order_id IN (SELECT order_id FROM order_items WHERE sku_id = 12345) LIMIT 1... | EXPLAIN FORMAT=JSON SELECT oi.item_id, oi.sku_id, oi.quantity, oi.unit_price, i.available_qty FROM order_items oi INNER JOIN inventory_stock i ON oi.sku_id = i.sku_id AND oi.warehouse_id = i.warehouse_id WHERE oi.unit_price > 50.00 AND i.available_qty > 100 AND oi.order_id IN (SELECT order_id FROM order_items WHERE sku... | The query planner chose this sub-optimal route because it had to perform excessive heap fetches on the wide order_items table due to the missing covering index on the columns used in the WHERE and JOIN clauses. This resulted in a full table scan, leading to poor performance. Additionally, the subquery in the WHERE clau... | CREATE INDEX idx_order_items_sku_id_warehouse_id_unit_price ON order_items (sku_id, warehouse_id, unit_price); CREATE INDEX idx_inventory_stock_sku_id_warehouse_id_available_qty ON inventory_stock (sku_id, warehouse_id, available_qty); | SELECT oi.item_id, oi.sku_id, oi.quantity, oi.unit_price, i.available_qty FROM order_items oi INNER JOIN inventory_stock i ON oi.sku_id = i.sku_id AND oi.warehouse_id = i.warehouse_id WHERE oi.unit_price > 50.00 AND i.available_qty > 100 AND EXISTS (SELECT 1 FROM order_items o2 WHERE o2.order_id = oi.order_id AND o2.sk... |
SQLite 3.45 | SELECT T1.id, T2.account_id FROM ledger_entries AS T1 INNER JOIN accounts AS T2 ON T1.account_id = T2.id WHERE T1.posting_date > '2022-01-01' AND T2.risk_tier = 'high' AND T2.balance > 1000 AND T1.currency = 'USD' | EXPLAIN QUERY PLAN SELECT T1.id, T2.account_id FROM ledger_entries AS T1 INNER JOIN accounts AS T2 ON T1.account_id = T2.id WHERE T1.posting_date > '2022-01-01' AND T2.risk_tier = 'high' AND T2.balance > 1000 AND T1.currency = 'USD'; -- Output: SCAN TABLE ledger_entries AS T1 (~10000 rows), SEARCH TABLE accounts AS T2 ... | The query planner chose a sub-optimal route because the join condition between ledger_entries and accounts is based on the account_id column, which is of type TEXT in the ledger_entries table and INTEGER in the accounts table. This implicit type coercion prevents the optimizer from using the B-Tree index on the account... | CREATE INDEX idx_ledger_entries_account_id ON ledger_entries (account_id); CREATE INDEX idx_accounts_risk_tier_balance ON accounts (risk_tier, balance); | SELECT T1.id, T2.account_id FROM ledger_entries AS T1 INNER JOIN accounts AS T2 ON CAST(T1.account_id AS INTEGER) = T2.id WHERE T1.posting_date > '2022-01-01' AND T2.risk_tier = 'high' AND T2.balance > 1000 AND T1.currency = 'USD' |
ClickHouse 24.3 | SELECT t1.tracking_num, t2.base_rate * 1.1 AS total_rate FROM shipments t1 INNER JOIN carrier_rates t2 ON t1.carrier_id = t2.carrier_id WHERE t1.departed_at > '2022-01-01' AND t2.zone_id = 5 AND t1.status = 'in_transit' GROUP BY t1.tracking_num, t2.base_rate | {"trees":{"0":{"type":"Aggregate","inputs":[{"type":"HashJoin","inputs":[{"type":"Filter","inputs":[{"type":"Scan","arguments":{"table":"shipments"}}],"filter":"departed_at > \u00272022-01-01\u0027 AND status = \u0027in_transit\u0027"},{"type":"Filter","inputs":[{"type":"Scan","arguments":{"table":"carrier_rates"}}],"f... | The query planner chose to use a Hash Aggregate due to the GROUP BY clause, but the work_mem setting was too low, causing the aggregate to spill to temporary disk files, resulting in a significant performance degradation. The HashJoin operation was chosen over a MergeJoin because the join keys were not sorted, and the ... | CREATE INDEX idx_shipments_carrier_id ON shipments (carrier_id); CREATE INDEX idx_carrier_rates_zone_id ON carrier_rates (zone_id); | SELECT t1.tracking_num, t2.base_rate * 1.1 AS total_rate FROM shipments t1 INNER JOIN carrier_rates t2 ON t1.carrier_id = t2.carrier_id AND t2.zone_id = 5 WHERE t1.departed_at > '2022-01-01' AND t1.status = 'in_transit' GROUP BY t1.tracking_num, t2.base_rate |
MySQL 8.0 | SELECT ps.match_id, ps.player_id, ps.kills, ps.score, ps.ping_ms FROM player_stats ps WHERE ps.kills > 10 OR ps.score > 500 AND ps.ping_ms < 100 | {"query_block": {"select_id": 1, "cost_info": {"query_cost": "3432.10"}, "table": {"table_name": "ps", "access_type": "ALL", "possible_keys": null, "key": null, "used_key_parts": null, "key_length": null, "ref": null, "rows": 102154, "filtered": 10.0, "cost": 2154.10}}} | The query planner chose a full table scan on the player_stats table because the OR condition across unindexed columns kills and score prevents the optimizer from using any available indexes, resulting in a costly scan of the entire table, and the additional filter on ping_ms further reduces the effectiveness of any pot... | CREATE INDEX idx_player_stats_kills_score ON player_stats (kills, score); | SELECT ps.match_id, ps.player_id, ps.kills, ps.score, ps.ping_ms FROM player_stats ps WHERE ps.kills > 10 OR ps.score > 500 AND ps.ping_ms < 100 |
ClickHouse 24.3 | SELECT d_t.device_id, d_t.recorded_at, d_t.val FROM device_telemetrics AS d_t INNER JOIN devices AS d ON d_t.device_id = d.device_id WHERE d.region = 'North America' AND d_t.firmware_v = '2.1.4' AND d_t.recorded_at > '2022-01-01 00:00:00' AND d_t.metric_type = 'temperature' | {"rows":"10000","bytes":"1000000","filtered":"0.1","partitions":"10","marks":{"FullScan":"10"},"explain":"Full partition scan of device_telemetrics due to mismatch in primary key"} | The query planner chose this sub-optimal route because the primary key of the device_telemetrics table is defined as (device_id, recorded_at), but the query filters on recorded_at with a range condition, forcing the database to decompress the entire partition. Additionally, the join with the devices table on device_id ... | CREATE INDEX idx_device_telemetrics_firmware_v ON device_telemetrics (firmware_v); CREATE INDEX idx_device_telemetrics_metric_type ON device_telemetrics (metric_type); ALTER TABLE device_telemetrics ADD COLUMN recorded_at_date Date DEFAULT toDate(recorded_at); CREATE INDEX idx_device_telemetrics_recorded_at_date ON dev... | SELECT d_t.device_id, d_t.recorded_at, d_t.val FROM device_telemetrics AS d_t INNER JOIN devices AS d ON d_t.device_id = d.device_id WHERE d.region = 'North America' AND d_t.firmware_v = '2.1.4' AND d_t.recorded_at_date >= '2022-01-01' AND d_t.metric_type = 'temperature' |
ClickHouse 24.3 | SELECT d.device_id, dt.val, dt.recorded_at FROM device_telematics AS dt INNER JOIN devices AS d ON dt.device_id = d.device_id WHERE dt.metric_type = 'temperature' AND dt.recorded_at > '2022-01-01 00:00:00' AND d.region = 'North America' AND dt.val > 50 | {"rows":"10000","blocks":"10","filter_columns":["dt.metric_type","dt.recorded_at","d.region","dt.val"],"steps":[{"name":"NestedLoopJoin","rows":"10000","blocks":"10"},{"name":"Filter","rows":"10000","blocks":"10"},{"name":"ReadFromStorage","rows":"1000000","blocks":"100"}]} | The query planner chose this sub-optimal route because the primary key of the device_telematics table is (device_id, metric_type, recorded_at), but the leading column of the primary key (device_id) is not filtered effectively, causing a full partition decompression, and the filter on recorded_at is not very selective, ... | CREATE INDEX idx_device_telematics_recorded_at ON device_telematics (recorded_at, device_id, metric_type); ALTER TABLE devices ADD INDEX idx_devices_region (region); | SELECT d.device_id, dt.val, dt.recorded_at FROM devices AS d INNER JOIN device_telematics AS dt ON dt.device_id = d.device_id AND dt.metric_type = 'temperature' WHERE dt.recorded_at > '2022-01-01 00:00:00' AND d.region = 'North America' AND dt.val > 50 |
MariaDB 11.2 | SELECT ps.match_id, ps.player_id, ps.kills, ps.score, ps.ping_ms FROM player_stats ps WHERE ps.kills > 15 OR ps.ping_ms < 80 AND ps.match_id IN (SELECT ms.match_id FROM match_sessions ms WHERE ms.region = 'EU' AND ms.game_mode = 'Ranked'); | {"query_block": {"select_id": 1, "cost_info": {"query_cost": "3432.10"}, "table": {"table_name": "ps", "access_type": "ALL", "rows": 1048435, "filtered": 10.0, "cost_info": {"read_cost": "1234.23", "eval_cost": "456.12", "prefix_cost": "3432.10", "data_caching": {"cache_misses": 23456, "cache_hits": 1234}}}}} | The query planner chose a full table scan on the player_stats table because the OR condition across the kills and ping_ms columns, which are not indexed, prevents the optimizer from using any existing indexes. Additionally, the subquery in the IN clause adds to the complexity, leading the planner to opt for a full scan... | CREATE INDEX idx_player_stats_kills ON player_stats (kills); CREATE INDEX idx_player_stats_ping_ms ON player_stats (ping_ms); CREATE INDEX idx_match_sessions_region_game_mode ON match_sessions (region, game_mode); | SELECT ps.match_id, ps.player_id, ps.kills, ps.score, ps.ping_ms FROM player_stats ps USE INDEX (idx_player_stats_kills, idx_player_stats_ping_ms) WHERE ps.kills > 15 OR ps.ping_ms < 80 AND ps.match_id IN (SELECT ms.match_id FROM match_sessions ms USE INDEX (idx_match_sessions_region_game_mode) WHERE ms.region = 'EU' A... |
MySQL 8.0 | SELECT su.id, su.plan, su.renewal_date FROM subscriptions su INNER JOIN workspace_users wu ON su.workspace_id = wu.workspace_id WHERE su.is_active = 1 AND wu.role = 'admin' AND su.renewal_date > '2022-01-01' | {"query_block": {"select_id": 1, "cost_info": {"query_cost": "12345.67"}, "table": {"table_name": "su", "access_type": "ALL", "rows": 100000, "filtered": 10, "cost": 12345.67}, "nested_loop": [ {"table": {"table_name": "wu", "access_type": "ALL", "rows": 50000, "filtered": 20, "cost": 6789.01 } } ] }} | The query planner chose a sub-optimal route because it failed to utilize an index on the is_active column, resulting in a full table scan of the subscriptions table, which has a large number of rows. Additionally, the join with the workspace_users table on the workspace_id column also resulted in a full table scan, fur... | CREATE INDEX idx_subscriptions_is_active ON subscriptions (is_active); CREATE INDEX idx_workspace_users_workspace_id ON workspace_users (workspace_id); | SELECT su.id, su.plan, su.renewal_date FROM subscriptions su INNER JOIN workspace_users wu ON su.workspace_id = wu.workspace_id AND wu.role = 'admin' WHERE su.is_active = 1 AND su.renewal_date > '2022-01-01' |
ClickHouse 24.3 | SELECT * FROM patient_admissions AS pa INNER JOIN lab_orders AS lo ON pa.admission_id = lo.admission_id WHERE lo.test_code ILIKE '%thyroid%' AND pa.clinic_id = 12345 | EXPLAIN SELECT * FROM patient_admissions AS pa INNER JOIN lab_orders AS lo ON pa.admission_id = lo.admission_id WHERE lo.test_code ILIKE '%thyroid%' AND pa.clinic_id = 12345;
Result =
{
'rows_read': 1000000,
'bytes_read': 100000000,
'filter_columns': {
'lo.test_code': {
'eq': 0,
'ne': 1000... | The query planner chose this sub-optimal route because the ILIKE operator with a leading wildcard ('%thyroid%') prevents the usage of the B-Tree index on the test_code column. As a result, the database engine has to perform a full table scan, leading to a large number of rows being read and filtered, causing significan... | CREATE INDEX idx_lab_orders_test_code ON lab_orders (test_code) TYPE ngrambf_v1; CREATE INDEX idx_patient_admissions_clinic_id ON patient_admissions (clinic_id) TYPE set(8); | SELECT * FROM patient_admissions AS pa INNER JOIN lab_orders AS lo ON pa.admission_id = lo.admission_id WHERE lo.test_code = 'thyroid' AND pa.clinic_id = 12345; Alternatively, if the ILIKE operator is necessary, consider using a full-text search index or a specialized search engine to improve query performance. |
SQLite 3.45 | SELECT * FROM order_items AS oi JOIN inventory_stock AS isk ON oi.sku_id = isk.sku_id WHERE strftime('%Y-%m', isk.updated_at) = '2022-07' AND oi.quantity > 5 AND isk.available_qty > 10 | EXPLAIN QUERY PLAN SELECT * FROM order_items AS oi JOIN inventory_stock AS isk ON oi.sku_id = isk.sku_id WHERE strftime('%Y-%m', isk.updated_at) = '2022-07' AND oi.quantity > 5 AND isk.available_qty > 10; -- Output: SCAN TABLE isk (~25000 rows), SCAN TABLE oi (~50000 rows) | The query planner chose a sub-optimal route because the non-SARGable function strftime('%Y-%m', isk.updated_at) prevents the use of an index on the updated_at column, resulting in a full table scan of the inventory_stock table, which has approximately 25,000 rows. Additionally, the join with the order_items table, whic... | CREATE INDEX idx_updated_at ON inventory_stock (strftime('%Y-%m', updated_at)); CREATE INDEX idx_sku_id ON order_items (sku_id); CREATE INDEX idx_sku_id_inv ON inventory_stock (sku_id); | SELECT * FROM order_items AS oi JOIN inventory_stock AS isk ON oi.sku_id = isk.sku_id WHERE isk.updated_at >= '2022-07-01' AND isk.updated_at < '2022-08-01' AND oi.quantity > 5 AND isk.available_qty > 10 |
CockroachDB v23.2 | SELECT s.tracking_num, cr.base_rate, pe.scan_time FROM shipments s INNER JOIN carrier_rates cr ON s.carrier_id = cr.carrier_id INNER JOIN parcel_events pe ON s.tracking_num = pe.tracking_num WHERE s.departed_at > '2022-01-01' AND s.status = 'IN_TRANSIT' AND pe.checkpoint = 'HUB_123' AND cr.zone_id = 5 AND cr.weight_kg ... | EXPLAIN (ANALYZE, BUFFERS) SELECT s.tracking_num, cr.base_rate, pe.scan_time FROM shipments s INNER JOIN carrier_rates cr ON s.carrier_id = cr.carrier_id INNER JOIN parcel_events pe ON s.tracking_num = pe.tracking_num WHERE s.departed_at > '2022-01-01' AND s.status = 'IN_TRANSIT' AND pe.checkpoint = 'HUB_123' AND cr.zo... | The query planner chose this sub-optimal route because it had to perform a merge join on the shipments and carrier_rates tables, which required sorting both tables on the carrier_id column, resulting in expensive memory sort operations on unindexed departure timestamps. This was further exacerbated by the lack of index... | CREATE INDEX idx_shipments_departed_at ON shipments (departed_at); CREATE INDEX idx_shipments_status ON shipments (status); CREATE INDEX idx_carrier_rates_zone_id_weight_kg ON carrier_rates (zone_id, weight_kg); CREATE INDEX idx_parcel_events_checkpoint ON parcel_events (checkpoint); VACUUM ANALYZE; | SELECT s.tracking_num, cr.base_rate, pe.scan_time FROM shipments s INNER JOIN carrier_rates cr ON s.carrier_id = cr.carrier_id INNER JOIN parcel_events pe ON s.tracking_num = pe.tracking_num WHERE s.departed_at > '2022-01-01' AND s.status = 'IN_TRANSIT' AND pe.checkpoint = 'HUB_123' AND cr.zone_id = 5 AND cr.weight_kg ... |
CockroachDB v23.2 | SELECT s.tracking_num, cr.base_rate, pe.scan_time FROM shipments s JOIN parcel_events pe ON s.tracking_num = pe.tracking_num JOIN carrier_rates cr ON s.carrier_id = cr.carrier_id WHERE s.departed_at > '2022-01-01' AND s.status = 'in_transit' AND pe.checkpoint = 'departure_scan' AND cr.zone_id = 5 AND s.weight_kg > 10 O... | EXPLAIN (ANALYZE, BUFFERS) SELECT s.tracking_num, cr.base_rate, pe.scan_time FROM shipments s JOIN parcel_events pe ON s.tracking_num = pe.tracking_num JOIN carrier_rates cr ON s.carrier_id = cr.carrier_id WHERE s.departed_at > '2022-01-01' AND s.status = 'in_transit' AND pe.checkpoint = 'departure_scan' AND cr.zone_id... | The query planner chose this sub-optimal route because the departed_at column in the shipments table is not indexed, resulting in a full table scan and subsequent merge join operation that requires sorting the entire result set in memory. This is exacerbated by the presence of multiple join operations and filter condit... | CREATE INDEX idx_shipments_departed_at ON shipments (departed_at DESC); CREATE INDEX idx_shipments_tracking_num ON shipments (tracking_num); CREATE INDEX idx_parcel_events_tracking_num ON parcel_events (tracking_num); CREATE INDEX idx_carrier_rates_carrier_id ON carrier_rates (carrier_id); VACUUM ANALYZE shipments; VAC... | SELECT s.tracking_num, cr.base_rate, pe.scan_time FROM shipments s JOIN parcel_events pe ON s.tracking_num = pe.tracking_num JOIN carrier_rates cr ON s.carrier_id = cr.carrier_id WHERE s.departed_at > '2022-01-01' AND s.status = 'in_transit' AND pe.checkpoint = 'departure_scan' AND cr.zone_id = 5 AND s.weight_kg > 10 O... |
MariaDB 11.2 | SELECT oi.item_id, oi.sku_id, oi.quantity, i.available_qty FROM order_items oi JOIN inventory_stock i ON oi.sku_id = i.sku_id AND oi.warehouse_id = i.warehouse_id WHERE oi.unit_price > 50.00 AND i.available_qty > 10 AND oi.order_id IN (SELECT order_id FROM promotions WHERE promo_id = 123 AND valid_from < '2022-01-01' A... | {"query_block": {"select_id": 1, "table": {"table_name": "oi", "access_type": "range", "possible_keys": ["idx_order_items_sku_id", "idx_order_items_warehouse_id"], "key": "idx_order_items_sku_id", "key_len": "5", "ref": "db.i.sku_id", "rows": 12345, "filtered": 10.00, "extra": "Using where; Using index"}, "table": {"ta... | The query planner chose this sub-optimal route because the available work_mem is too low, causing the bitmap heap scan to degrade to lossy page mode. This is evident from the high number of rows being scanned and the low filtered percentage. Additionally, the lack of an index on the order_items table for the unit_price... | CREATE INDEX idx_order_items_unit_price ON order_items (unit_price); CREATE INDEX idx_order_items_warehouse_id ON order_items (warehouse_id); ALTER TABLE order_items ADD CONSTRAINT fk_order_items_warehouse_id FOREIGN KEY (warehouse_id) REFERENCES inventory_stock (warehouse_id); VACUUM ANALYZE order_items; VACUUM ANALYZ... | SELECT oi.item_id, oi.sku_id, oi.quantity, i.available_qty FROM order_items oi JOIN inventory_stock i ON oi.sku_id = i.sku_id AND oi.warehouse_id = i.warehouse_id JOIN promotions p ON oi.order_id = p.order_id WHERE oi.unit_price > 50.00 AND i.available_qty > 10 AND p.promo_id = 123 AND p.valid_from < '2022-01-01' AND p... |
ClickHouse 24.3 | SELECT t1.admission_id, t2.test_code, t3.charge_cents FROM patient_admissions AS t1 INNER JOIN lab_orders AS t2 ON t1.admission_id = t2.admission_id INNER JOIN billing_codes AS t3 ON t1.admission_id = t3.admission_id WHERE t1.clinic_id = 145 AND t2.flagged = 1 AND t3.icd10 = 'R07.9' AND t1.admitted_at > '2022-01-01' | {"trees":{"0":{"type":"NestedLoopJoin","join_type":"Inner","est_rows":100,"rows":100,"time":0.001,"join":"(t1.admission_id = t2.admission_id) AND (t1.admission_id = t3.admission_id)","table":"t1","est_rows":100,"rows":100,"time":0.001,"join":"(t1.admission_id = t2.admission_id)","table":"t2","est_rows":100,"rows":100,"... | The query planner chose the sub-optimal route of using a nested loop join because the foreign key columns 'admission_id' in tables 'lab_orders' and 'billing_codes' are not indexed, resulting in a full table scan for each join operation. This is further exacerbated by the fact that the filter conditions on 'clinic_id', ... | CREATE INDEX idx_lab_orders_admission_id ON lab_orders (admission_id); CREATE INDEX idx_billing_codes_admission_id ON billing_codes (admission_id); CREATE INDEX idx_patient_admissions_clinic_id ON patient_admissions (clinic_id); | SELECT t1.admission_id, t2.test_code, t3.charge_cents FROM patient_admissions AS t1 INNER JOIN lab_orders AS t2 ON t1.admission_id = t2.admission_id INNER JOIN billing_codes AS t3 ON t1.admission_id = t3.admission_id WHERE t1.clinic_id = 145 AND t2.flagged = 1 AND t3.icd10 = 'R07.9' AND t1.admitted_at > '2022-01-01' |
MySQL 8.0 | SELECT oi.item_id, oi.order_id, i.available_qty FROM order_items oi INNER JOIN inventory_stock i ON oi.sku_id = i.sku_id AND oi.warehouse_id = i.warehouse_id WHERE oi.unit_price > 50.00 AND i.available_qty < 100 AND oi.quantity > 5 | EXPLAIN SELECT oi.item_id, oi.order_id, i.available_qty FROM order_items oi INNER JOIN inventory_stock i ON oi.sku_id = i.sku_id AND oi.warehouse_id = i.warehouse_id WHERE oi.unit_price > 50.00 AND i.available_qty < 100 AND oi.quantity > 5; ID: 1, Select_type: SIMPLE, Table: oi, Type: ALL, Possible_keys: PRIMARY, Key: ... | The query planner is choosing to perform a full table scan on both the order_items and inventory_stock tables due to the lack of a suitable index on the join columns and filter conditions. This results in a large number of rows being scanned, leading to increased I/O and CPU usage. Additionally, the join operation is b... | CREATE INDEX idx_order_items_sku_id_warehouse_id ON order_items (sku_id, warehouse_id); CREATE INDEX idx_inventory_stock_sku_id_warehouse_id ON inventory_stock (sku_id, warehouse_id); CREATE INDEX idx_order_items_unit_price_quantity ON order_items (unit_price, quantity); CREATE INDEX idx_inventory_stock_available_qty O... | SELECT oi.item_id, oi.order_id, i.available_qty FROM order_items oi INNER JOIN inventory_stock i ON oi.sku_id = i.sku_id AND oi.warehouse_id = i.warehouse_id WHERE oi.unit_price > 50.00 AND i.available_qty < 100 AND oi.quantity > 5 AND oi.sku_id IN (SELECT sku_id FROM order_items WHERE unit_price > 50.00 AND quantity >... |
PostgreSQL 16.2 | SELECT ROW_NUMBER() OVER (PARTITION BY a.campaign_id ORDER BY a.served_at DESC) AS row_num, a.imp_id, a.campaign_id, a.publisher_id, a.bid_cpm, a.user_segment, a.served_at FROM ad_impressions a JOIN campaigns c ON a.campaign_id = c.campaign_id WHERE c.advertiser_id = 54321 AND c.status = 'active' AND a.served_at > '202... | {"Plan": {"Node Type": "Subquery Scan", "Parent Relationship": "Outer", "Plans": [{"Node Type": "WindowAgg", "Strategy": "Sorted", "Plans": [{"Node Type": "Merge Join", "Inner Unique": false, "Join Type": "Inner", " Plans": [{"Node Type": "Seq Scan", "Relation Name": "ad_impressions", "Alias": "a", "Rows": 50000000, "S... | The PostgreSQL query planner chose this sub-optimal route because it lacked a suitable composite index on the ad_impressions table that could support the window function's partitioning and sorting requirements, leading to a costly sequential scan and sort of the entire table, which contains 50 million rows. Additionall... | CREATE INDEX idx_ad_impressions_campaign_id_served_at ON ad_impressions (campaign_id, served_at); CREATE INDEX idx_campaigns_campaign_id_advertiser_id ON campaigns (campaign_id, advertiser_id); VACUUM ANALYZE ad_impressions; VACUUM ANALYZE campaigns; | WITH ranked_impressions AS (SELECT a.imp_id, a.campaign_id, a.publisher_id, a.bid_cpm, a.user_segment, a.served_at, ROW_NUMBER() OVER (PARTITION BY a.campaign_id ORDER BY a.served_at DESC) AS row_num FROM ad_impressions a JOIN campaigns c ON a.campaign_id = c.campaign_id WHERE c.advertiser_id = 54321 AND c.status = 'ac... |
MariaDB 11.2 | SELECT dt.device_id, dt.metric_type, AVG(dt.val) AS avg_val, MAX(ge.lat) AS max_lat, MIN(ge.lon) AS min_lon FROM device_telematics dt JOIN geofence_events ge ON dt.device_id = ge.device_id WHERE dt.recorded_at > '2022-01-01' AND dt.firmware_v = '2.4.1' AND ge.logged_at < '2023-01-01' GROUP BY dt.device_id, dt.metric_ty... | {"query_block": {"select_id": 1, "cost_info": {"query_cost": "12345.67"}, "table": {"table_name": "dt", "access_type": "ALL", "rows": "1000000", "filtered": "10.00", "cost": "12345.67"}, "extra": ["Using filesort"]}} | The query planner chose this sub-optimal route because the join operation between device_telematics and geofence_events resulted in a large intermediate result set, which was then grouped by the high-cardinality columns device_id and metric_type, leading to excessive memory usage and disk spilling due to the inefficien... | CREATE INDEX idx_device_telematics_device_id ON device_telematics (device_id); CREATE INDEX idx_device_telematics_recorded_at ON device_telematics (recorded_at); CREATE INDEX idx_geofence_events_device_id ON geofence_events (device_id); CREATE INDEX idx_geofence_events_logged_at ON geofence_events (logged_at); | WITH ranked_telematics AS (SELECT dt.device_id, dt.metric_type, AVG(dt.val) AS avg_val, MAX(ge.lat) AS max_lat, MIN(ge.lon) AS min_lon, ROW_NUMBER() OVER (PARTITION BY dt.device_id, dt.metric_type ORDER BY AVG(dt.val) DESC) AS row_num FROM device_telematics dt JOIN geofence_events ge ON dt.device_id = ge.device_id WHER... |
CockroachDB v23.2 | SELECT t1.account_id, t2.balance FROM ledger_entries AS t1 INNER JOIN accounts AS t2 ON CAST(t1.account_id AS STRING) = CAST(t2.id AS STRING) WHERE t1.posting_date > '2022-01-01' AND t2.risk_tier = 'high' AND t1.currency = 'USD' AND t1.amount > 1000 | EXPLAIN (ANALYZE, BUFFERS) SELECT t1.account_id, t2.balance FROM ledger_entries AS t1 INNER JOIN accounts AS t2 ON CAST(t1.account_id AS STRING) = CAST(t2.id AS STRING) WHERE t1.posting_date > '2022-01-01' AND t2.risk_tier = 'high' AND t1.currency = 'USD' AND t1.amount > 1000
-> Giovanni Scan on ledger_entries AS t1 (... | The query planner chose this sub-optimal route because the join condition between ledger_entries and accounts involves implicit type coercion between the account_id column of type UUID in ledger_entries and the id column of type STRING in accounts. This prevents the optimizer from using the B-Tree index on the id colum... | CREATE INDEX idx_ledger_entries_account_id ON ledger_entries (account_id); CREATE INDEX idx_accounts_id ON accounts (id); CREATE INDEX idx_ledger_entries_posting_date ON ledger_entries (posting_date); CREATE INDEX idx_ledger_entries_currency ON ledger_entries (currency); CREATE INDEX idx_ledger_entries_amount ON ledger... | SELECT t1.account_id, t2.balance FROM ledger_entries AS t1 INNER JOIN accounts AS t2 ON t1.account_id = t2.id::UUID WHERE t1.posting_date > '2022-01-01' AND t2.risk_tier = 'high' AND t1.currency = 'USD' AND t1.amount > 1000 |
ClickHouse 24.3 | WITH ranked_match_stats AS (SELECT match_id, player_id, SUM(kills) AS total_kills, SUM(score) AS total_score FROM player_stats WHERE match_id IN (SELECT match_id FROM match_sessions WHERE region = 'EU' AND started_at > '2022-01-01' AND ended_at < '2022-12-31') GROUP BY match_id, player_id) SELECT rms.match_id, rms.play... | {"stages":{"0":{"name":"Fetch","rows":"13451","bytes":"1078816"},"1":{"name":"Join","rows":"13451","bytes":"1078816","join_type":"Inner"},"2":{"name":"Aggregator","rows":"29843","bytes":"2391872"},"3":{"name":"Filter","rows":"29843","bytes":"2391872"},"4":{"name":"Project","rows":"29843","bytes":"2391872"},"5":{"name":... | The query planner chose this sub-optimal route because it was unable to push down the filters on the match_sessions table into the partitioned storage, resulting in a full table scan. The Materialized CTE (WITH clause) prevented the optimizer from effectively reordering the operations and applying the filters earlier i... | CREATE INDEX idx_match_sessions_match_id ON match_sessions (match_id); CREATE INDEX idx_player_stats_player_id ON player_stats (player_id); CREATE INDEX idx_leaderboards_player_id ON leaderboards (player_id); | SELECT rms.match_id, rms.player_id, rms.total_kills, rms.total_score, lb.mmr_rating, lb.rank_tier FROM (SELECT match_id, player_id, SUM(kills) AS total_kills, SUM(score) AS total_score FROM player_stats WHERE match_id IN (SELECT match_id FROM match_sessions WHERE region = 'EU' AND started_at > '2022-01-01' AND ended_at... |
PostgreSQL 16.2 | SELECT d.device_id, SUM(dt.val) AS total_val FROM device_telematics dt JOIN devices d ON dt.device_id = d.device_id WHERE dt.metric_type = 'temperature' AND dt.recorded_at > '2022-01-01' AND d.region = 'North America' AND d.status = 'active' GROUP BY d.device_id ORDER BY total_val DESC LIMIT 100; | EXPLAIN (ANALYZE, BUFFERS) SELECT d.device_id, SUM(dt.val) AS total_val FROM device_telematics dt JOIN devices d ON dt.device_id = d.device_id WHERE dt.metric_type = 'temperature' AND dt.recorded_at > '2022-01-01' AND d.region = 'North America' AND d.status = 'active' GROUP BY d.device_id ORDER BY total_val DESC LIMIT ... | The query planner chose this sub-optimal route because the existing index on device_telematics is too broad and doesn't account for the specific metric_type filter, leading to an excessive number of rows being scanned and joined, thus causing the Hash Join and Sort operations to become the bottlenecks. Additionally, th... | CREATE INDEX idx_device_telematics_metric_type ON device_telematics (metric_type, recorded_at); CREATE INDEX idx_devices_region_status ON devices (region, status); VACUUM ANALYZE device_telematics; VACUUM ANALYZE devices; | SELECT d.device_id, SUM(dt.val) AS total_val FROM device_telematics dt JOIN devices d ON dt.device_id = d.device_id WHERE dt.metric_type = 'temperature' AND dt.recorded_at > '2022-01-01' AND d.region = 'North America' AND d.status = 'active' GROUP BY d.device_id ORDER BY total_val DESC LIMIT 100; |
Enterprise Database Query Plan Optimization & Remediation Traces (Community Preview)
Overview
High-concurrency database workloads frequently degrade due to optimizer plan misestimations, unindexed correlated joins, merge-sort memory spills, and non-sargable expressions.
This repository hosts the Community Preview (250 verified traces) of an architectural fine-tuning dataset spanning PostgreSQL 16, MySQL 8.0, ClickHouse 24, CockroachDB, MariaDB, and SQLite. Each record pairs an authentic production query regression and raw execution plan (EXPLAIN ANALYZE) with chain-of-thought architectural diagnosis, actionable remediation DDL, and dialect-compliant optimized queries.
Dataset Splits & Commercial Licensing
| Split | Records | Format | Primary Use Case | License |
|---|---|---|---|---|
| Community Evaluation (This Repo) | 250 | Parquet | Benchmarking, zero-shot eval, research | CC BY-NC 4.0 |
| Enterprise Commercial Corpus | 2,500 | Parquet + JSONL | Production fine-tuning (SFT/DPO), commercial DBA agents | Commercial Enterprise License |
Complete Enterprise Corpus (2,500 Records)
For production model deployment, proprietary agent training, or private RAG pipelines without non-commercial restrictions:
👉 Acquire Enterprise Commercial License & Dataset via Dodo Payments
Data Structure & Schema
{
"database_engine": "string",
"slow_query": "string",
"execution_plan": "string",
"diagnostic_thought": "string",
"remediation_ddl": "string",
"optimized_query": "string"
}
Field Definitions
| Field | Data Type | Description |
|---|---|---|
database_engine |
string |
Target engine dialect & exact version (e.g., PostgreSQL 16.2, ClickHouse 24.3). |
slow_query |
string |
Realistic unoptimized SQL query triggering runtime performance degradation. |
execution_plan |
string |
Raw optimizer output (EXPLAIN (ANALYZE, BUFFERS) or JSON plan) exposing node bottlenecks. |
diagnostic_thought |
string |
Step-by-step reasoning diagnosing node cost, row estimate deltas, and I/O buffer behavior. |
remediation_ddl |
string |
Executable DDL statements (CREATE INDEX CONCURRENTLY, table parameter tuning). |
optimized_query |
string |
Performant rewritten query strictly adhering to engine dialect grammar. |
Quickstart (Hugging Face Datasets)
from datasets import load_dataset
# Load Community Preview
dataset = load_dataset("ayushjoshi2012/sql-optimization-diagnostic-traces", split="train")
# Inspect a single diagnostic trace
sample = dataset[0]
print(f"Engine: {sample['database_engine']}")
print(f"Slow Query:\n{sample['slow_query']}")
print(f"Diagnosis:\n{sample['diagnostic_thought']}")
print(f"Remediation DDL:\n{sample['remediation_ddl']}")
print(f"Rewritten Query:\n{sample['optimized_query']}")
Core Domain Taxonomy
Traces are generated across representative enterprise schema designs:
- Fintech & Ledger Core: Double-entry ledger scans, implicit UUID-to-integer type coercion, balance aggregations.
- IoT & Telematics: Granule skip-index mismatches, partition decompression stalls, time-bucket groupings.
- B2B SaaS & Security: High-offset tenant audit logs, soft-delete booleans, unindexed RBAC joins.
- Healthcare & EHR: Unindexed composite foreign keys, leading-wildcard text filters, outdated optimizer statistics.
- Freight & Global Logistics: Disk-spilling merge joins on departure timestamps, Cartesian row multiplication.
- AdTech & Gaming Telemetry: Unindexed streaming click joins, unbounded window function sorts, leaderboard lock contention.
Citation & Contact
For inquiries regarding custom synthetic domain generation, enterprise site licenses, or data additions, please commentinitiate a new discussion.
- Downloads last month
- 89