Dataset Viewer
Auto-converted to Parquet Duplicate
id
int64
1
300
db
stringclasses
4 values
base_sql
stringlengths
53
2.42k
optimized_sql
stringlengths
22
1.11k
base_time
float64
0.07
450k
fast_time
float64
0.03
212k
base_explain_analyze
unknown
optimized_explain_analyze
unknown
difficulty
stringclasses
3 values
1
tpch
WITH UnpivotedData AS (SELECT ctid AS row_id, l_extendedprice AS estimate_val FROM lineitem UNION ALL SELECT ctid AS row_id, l_discount AS estimate_val FROM lineitem UNION ALL SELECT ctid AS row_id, l_tax AS estimate_val FROM lineitem), AggregatedAverages AS (SELECT row_id, round(avg(estimate_val), 2) AS avg_estimate F...
SELECT t.*, round(avg_calc.avg_val, 2) AS avg_estimate FROM lineitem AS t LEFT JOIN LATERAL (SELECT avg(v.val) AS avg_val FROM (VALUES (t.l_extendedprice), (t.l_discount), (t.l_tax)) AS v(val)) AS avg_calc ON TRUE;
130,791.865
42,217.071
{ "Plan Width": 149, "Actual Rows": 17996609, "Plan Rows": 200, "Actual Startup Time": 25607.736, "Node Type": "Nested Loop", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 2092718.38, "Actual Total Time": 129459.461, "Startup Cost": 2091908.88, "Plans": [ { "Plan Width": 38, ...
{ "Plan Width": 149, "Actual Rows": 17996609, "Plan Rows": 17996248, "Actual Startup Time": 119.398, "Node Type": "Nested Loop", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 1957040.32, "Actual Total Time": 41550.132, "Startup Cost": 0.05, "Plans": [ { "Relation Name": "line...
medium
2
tpch
SELECT d.month, d.year, (SELECT COUNT(*) FROM orders t WHERE EXTRACT(MONTH FROM t.o_orderdate) = d.month AND EXTRACT(YEAR FROM t.o_orderdate) = d.year AND t.o_orderstatus = 'F') AS acc_completed, (SELECT COUNT(*) FROM orders t WHERE EXTRACT(MONTH FROM t.o_orderdate) = d.month AND EXTRACT(YEAR FROM t.o_ord...
SELECT EXTRACT(MONTH FROM o.o_orderdate) AS month, EXTRACT(YEAR FROM o.o_orderdate) AS year, COUNT(CASE WHEN o.o_orderstatus = 'F' THEN 1 END) AS acc_completed, COUNT(CASE WHEN o.o_orderstatus = 'O' THEN 1 END) AS acc_start FROM orders o GROUP BY EXTRACT(MONTH FROM o.o_orderdate), EXTRACT(YEAR FROM o.o_or...
364,865.627
7,351.592
{ "Plan Width": 32, "Actual Rows": 80, "Plan Rows": 2406, "Actual Startup Time": 10911.163, "Node Type": "Subquery Scan", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 3241205573.6, "Actual Total Time": 364845.39, "Startup Cost": 635932.55, "Alias": "d", "Plans": [ { "Plan ...
{ "Plan Width": 32, "Actual Rows": 80, "Plan Rows": 2406, "Actual Startup Time": 7332.59, "Node Type": "Aggregate", "Actual Loops": 1, "Parallel Aware": false, "Strategy": "Hashed", "Total Cost": 785988.89, "Actual Total Time": 7332.614, "Startup Cost": 785940.77, "Plans": [ { "Relatio...
medium
3
tpch
SELECT o.* FROM orders o WHERE o.o_clerk <> ' ' AND EXISTS ( SELECT 1 FROM (VALUES ('F'::char(1)), ('O'::char(1))) AS v(status) WHERE v.status = o.o_orderstatus );
SELECT t1.* FROM orders AS t1 WHERE t1.o_clerk <> ' ' AND t1.o_orderstatus IN ('F','O');
1,776.159
1,047.851
{ "Plan Width": 107, "Actual Rows": 4385171, "Plan Rows": 2998723, "Actual Startup Time": 6.974, "Node Type": "Hash Join", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 179691.05, "Actual Total Time": 1604.146, "Startup Cost": 0.05, "Plans": [ { "Relation Name": "orders", ...
{ "Relation Name": "orders", "Plan Width": 107, "Actual Rows": 4385171, "Plan Rows": 4375437, "Actual Startup Time": 3.22, "Node Type": "Seq Scan", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 145771.68, "Actual Total Time": 875.083, "Startup Cost": 0, "Alias": "t1" }
easy
4
tpch
SELECT l_orderkey, (array_agg(l_quantity ORDER BY l_quantity DESC) FILTER (WHERE l_returnflag = 'R'))[1] AS A, (array_agg(l_quantity ORDER BY l_quantity DESC) FILTER (WHERE l_returnflag = 'A'))[1] AS B, (array_agg(l_quantity ORDER BY l_quantity DESC) FILTER (WHERE l_returnflag = 'N'))[1] AS V FROM lineitem GROUP BY l_o...
WITH cte AS ( SELECT l_returnflag, l_quantity, l_orderkey FROM lineitem ) SELECT l_orderkey, MAX(CASE WHEN l_returnflag = 'R' THEN l_quantity END) AS A, MAX(CASE WHEN l_returnflag = 'A' THEN l_quantity END) AS B, MAX(CASE WHEN l_returnflag = 'N' THEN l_quantity END) AS V FROM cte GROUP BY l_orderkey
19,065.472
9,116.911
{ "Plan Width": 100, "Actual Rows": 4500000, "Plan Rows": 405253, "Actual Startup Time": 122.366, "Node Type": "Aggregate", "Actual Loops": 1, "Parallel Aware": false, "Strategy": "Sorted", "Total Cost": 1044001.65, "Actual Total Time": 18871.385, "Startup Cost": 0.44, "Plans": [ { "Re...
{ "Plan Width": 100, "Actual Rows": 4500000, "Plan Rows": 405253, "Actual Startup Time": 140.694, "Node Type": "Aggregate", "Actual Loops": 1, "Parallel Aware": false, "Strategy": "Sorted", "Total Cost": 1040962.25, "Actual Total Time": 8931.265, "Startup Cost": 0.44, "Plans": [ { "Rel...
medium
5
tpch
SELECT TableActual.price AS "Final Pricing" FROM ( SELECT o.o_orderkey AS ordernumber, o.o_custkey AS customerkey, o.o_orderstatus AS orderstatus, ( SELECT COALESCE(ROUND(SUM(l.l_extendedprice * (1 - l.l_discount)),2), 0) FROM ...
SELECT dt.price AS "Final Pricing" FROM ( SELECT o.o_orderkey AS ordernumber, o.o_custkey AS customerkey, o.o_orderstatus AS orderstatus, ( SELECT COALESCE(ROUND(SUM(l.l_extendedprice * (1 - l.l_discount)),2), 0) FROM lineitem l WHERE o.o_order...
28,045.002
19,491.649
{ "Plan Width": 32, "Actual Rows": 4500000, "Plan Rows": 4500380, "Actual Startup Time": 159.539, "Node Type": "Subquery Scan", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 64269307.38, "Actual Total Time": 27750.721, "Startup Cost": 0.43, "Alias": "tableactual", "Plans": [ { ...
{ "Plan Width": 32, "Actual Rows": 4500000, "Plan Rows": 4500380, "Actual Startup Time": 151.099, "Node Type": "Subquery Scan", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 43207528.98, "Actual Total Time": 19205.039, "Startup Cost": 0.43, "Alias": "dt", "Plans": [ { "Plan...
hard
6
tpch
select orders.*, (first_week + floor(extract(epoch from o_orderdate::timestamp - first_week) / (14 * 86400)) * 14 * interval '1 day')::date as biweek from (select orders.*, min(date_trunc('week', o_orderdate)) over () as first_week from orders ) orders;
WITH first_week_val AS ( SELECT min(date_trunc('week', o_orderdate)) as fw FROM orders ), orders_with_biweek AS ( SELECT orders.*, fw.fw as first_week FROM orders CROSS JOIN first_week_val fw ) SELECT orders.*, (first_week + floor(extract(epoch from o_orderdate::timestamp - first_week) / (14 *...
7,133.529
5,153.832
{ "Plan Width": 119, "Actual Rows": 4500000, "Plan Rows": 4499579, "Actual Startup Time": 3421.815, "Node Type": "Subquery Scan", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 359501.69, "Actual Total Time": 6948.276, "Startup Cost": 0, "Alias": "orders", "Plans": [ { "Plan...
{ "Plan Width": 119, "Actual Rows": 4500000, "Plan Rows": 4499579, "Actual Startup Time": 1934.694, "Node Type": "Nested Loop", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 437779.71, "Actual Total Time": 4971.459, "Startup Cost": 157020.63, "Plans": [ { "Plan Width": 8, ...
hard
7
tpch
SELECT d.year_value AS year, d.month_value AS month, COUNT(DISTINCT o.o_custkey) AS total_customers FROM ( SELECT DISTINCT EXTRACT(YEAR FROM o_orderdate) AS year_value, EXTRACT(MONTH FROM o_orderdate) AS month_value, DATE_TRUNC('month', o_orderdate)::DATE AS month_bucket FROM orders ) AS d JOIN orders...
SELECT EXTRACT(YEAR FROM o.o_orderdate) AS year, EXTRACT(MONTH FROM o.o_orderdate) AS month, COUNT(DISTINCT o.o_custkey) AS total_customers FROM orders AS o JOIN customer AS c ON o.o_custkey = c.c_custkey GROUP BY year, month ORDER BY year ASC, month ASC
12,729.231
6,939.517
{ "Plan Width": 24, "Actual Rows": 80, "Plan Rows": 241, "Actual Startup Time": 11049.74, "Node Type": "Aggregate", "Actual Loops": 1, "Parallel Aware": false, "Strategy": "Sorted", "Total Cost": 13103454.47, "Actual Total Time": 12664.04, "Startup Cost": 12562056.35, "Plans": [ { "Pla...
{ "Plan Width": 24, "Actual Rows": 80, "Plan Rows": 2406, "Actual Startup Time": 5216.116, "Node Type": "Aggregate", "Actual Loops": 1, "Parallel Aware": false, "Strategy": "Sorted", "Total Cost": 963064.89, "Actual Total Time": 6889.235, "Startup Cost": 918012.97, "Plans": [ { "Plan W...
easy
8
tpch
SELECT yt.* FROM orders yt JOIN (VALUES (DATE '1995-07-01', DATE '1996-06-30'), (DATE '1998-07-01', DATE '1998-12-31')) AS ranges(start_date, end_date) ON yt.o_orderdate BETWEEN ranges.start_date AND ranges.end_date
SELECT * FROM orders WHERE (o_orderdate >= DATE '1995-07-01' AND o_orderdate <= DATE '1996-06-30') OR (o_orderdate >= DATE '1998-07-01' AND o_orderdate <= DATE '1998-12-31')
2,323.148
652.711
{ "Plan Width": 107, "Actual Rows": 744880, "Plan Rows": 1000084, "Actual Startup Time": 5.287, "Node Type": "Nested Loop", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 280795.13, "Actual Total Time": 2276.161, "Startup Cost": 0, "Plans": [ { "Relation Name": "orders", ...
{ "Relation Name": "orders", "Plan Width": 107, "Actual Rows": 744880, "Plan Rows": 718201, "Actual Startup Time": 3.77, "Node Type": "Seq Scan", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 168285.6, "Actual Total Time": 604.229, "Startup Cost": 0, "Alias": "orders" }
medium
9
tpch
WITH grouped_orderkeys AS (SELECT DISTINCT l_orderkey FROM lineitem) SELECT l.l_orderkey, l.l_shipdate FROM lineitem l INNER JOIN orders o ON l.l_orderkey = o.o_orderkey INNER JOIN grouped_orderkeys g ON l.l_orderkey = g.l_orderkey
SELECT l.l_orderkey, l.l_shipdate FROM lineitem l WHERE EXISTS (SELECT 1 FROM orders o WHERE l.l_orderkey = o.o_orderkey)
14,566.972
10,253.646
{ "Plan Width": 8, "Actual Rows": 17996609, "Plan Rows": 17996248, "Actual Startup Time": 5215.184, "Node Type": "Hash Join", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 1315276.1, "Actual Total Time": 13919.978, "Startup Cost": 572787.04, "Plans": [ { "Relation Name": "lin...
{ "Plan Width": 8, "Actual Rows": 17996609, "Plan Rows": 17996248, "Actual Startup Time": 1220.699, "Node Type": "Hash Join", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 913457.56, "Actual Total Time": 9600.629, "Startup Cost": 190700.88, "Plans": [ { "Relation Name": "line...
medium
10
tpch
SELECT t.o_orderstatus FROM orders t INNER JOIN (VALUES ('O'), ('F'), ('P')) AS v(val) ON t.o_orderstatus = v.val
SELECT o_orderstatus FROM orders WHERE o_orderstatus = ANY(ARRAY['O', 'F', 'P'])
3,535.676
1,530.834
{ "Plan Width": 2, "Actual Rows": 4500000, "Plan Rows": 67494, "Actual Startup Time": 6.232, "Node Type": "Hash Join", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 146446.7, "Actual Total Time": 3332.54, "Startup Cost": 0.08, "Plans": [ { "Relation Name": "orders", "Pl...
{ "Relation Name": "orders", "Plan Width": 2, "Actual Rows": 4500000, "Plan Rows": 67494, "Actual Startup Time": 3.987, "Node Type": "Seq Scan", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 151396.16, "Actual Total Time": 1348.996, "Startup Cost": 0, "Alias": "orders" }
easy
11
tpch
select distinct l.l_suppkey from lineitem l where l.l_shipdate >= date '1995-01-01' and l.l_shipdate < date '1995-01-02' and not exists (select 1 from lineitem l2 where l2.l_suppkey = l.l_suppkey and l2.l_shipdate >= date '1995-01-01' and l2.l_shipdate < date '1995-01-02' and l2.l_returnflag = 'R');
select l.l_suppkey from lineitem l where l.l_shipdate >= date '1995-01-01' and l.l_shipdate < date '1995-01-02' group by l.l_suppkey having bool_and(l.l_returnflag <> 'R');
4,980.121
2,050.675
{ "Plan Width": 4, "Actual Rows": 3029, "Plan Rows": 6099, "Actual Startup Time": 4958.095, "Node Type": "Unique", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 1260220.71, "Actual Total Time": 4960.728, "Startup Cost": 1260158.3, "Plans": [ { "Plan Width": 4, "Actual R...
{ "Plan Width": 4, "Actual Rows": 3029, "Plan Rows": 3050, "Actual Startup Time": 2027.591, "Node Type": "Aggregate", "Actual Loops": 1, "Parallel Aware": false, "Strategy": "Sorted", "Total Cost": 607884.87, "Actual Total Time": 2030.153, "Startup Cost": 607755.72, "Plans": [ { "Plan ...
medium
12
tpch
WITH cutoff_date AS ( SELECT MIN(o_orderdate) AS min_date FROM orders ) SELECT r.o_orderkey, r.o_custkey, r.o_orderstatus, r.o_totalprice, r.o_orderdate, r.o_orderpriority, r.o_clerk, r.o_shippriority, r.o_comment FROM ( SELECT orders.*, ROW_NUMBER() OVER(ORDER BY...
WITH cutoff_date AS ( SELECT MIN(O_ORDERDATE) AS min_date FROM ORDERS ), latest_rows AS ( SELECT ORDERS.* FROM ORDERS WHERE O_ORDERDATE = (SELECT min_date FROM cutoff_date) ORDER BY O_ORDERDATE DESC, O_ORDERKEY DESC LIMIT 3 ) SELECT latest_rows.* FROM ...
1,834.488
828.508
{ "Plan Width": 107, "Actual Rows": 3, "Plan Rows": 623, "Actual Startup Time": 1811.547, "Node Type": "Sort", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 269821.63, "Actual Total Time": 1811.551, "Startup Cost": 269820.07, "Plans": [ { "Plan Width": 107, "Actual Rows...
{ "Plan Width": 107, "Actual Rows": 3, "Plan Rows": 3, "Actual Startup Time": 808.51, "Node Type": "Sort", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 134854.35, "Actual Total Time": 808.513, "Startup Cost": 134854.34, "Plans": [ { "Plan Width": 107, "Actual Rows": 3,...
medium
13
tpch
SELECT l_returnflag, l_linestatus FROM (SELECT l_returnflag, l_linestatus, ROW_NUMBER() OVER (ORDER BY l_orderkey ASC) as rn FROM lineitem) ranked WHERE rn = 1;
SELECT l.l_returnflag, l.l_linestatus FROM lineitem l ORDER BY l.l_orderkey ASC LIMIT 1;
12,780.307
0.082
{ "Plan Width": 4, "Actual Rows": 1, "Plan Rows": 89988, "Actual Startup Time": 102.452, "Node Type": "Subquery Scan", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 1216885.5, "Actual Total Time": 12758.87, "Startup Cost": 0.44, "Alias": "ranked", "Plans": [ { "Plan Width":...
{ "Plan Width": 8, "Actual Rows": 1, "Plan Rows": 1, "Actual Startup Time": 0.055, "Node Type": "Limit", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 0.48, "Actual Total Time": 0.056, "Startup Cost": 0.44, "Plans": [ { "Relation Name": "lineitem", "Plan Width": 8, ...
easy
14
tpch
SELECT t.l_returnflag, x_agg.c AS "F", y_agg.c AS "O", z_agg.c AS "P" FROM (SELECT DISTINCT l.l_returnflag FROM lineitem l) t LEFT JOIN LATERAL (SELECT COUNT(*) AS c FROM lineitem l2 WHERE l2.l_returnflag = t.l_returnflag AND l2.l_linestatus = 'F') x_agg ON TRUE LEFT JOIN LATERAL (SELECT COUNT(*) AS c FROM lineitem l2 ...
SELECT l.l_returnflag, SUM((l.l_linestatus = 'F')::int) AS "F", SUM((l.l_linestatus = 'O')::int) AS "O", SUM((l.l_linestatus = 'P')::int) AS "P" FROM lineitem l GROUP BY l.l_returnflag
26,161.88
6,592.034
{ "Plan Width": 26, "Actual Rows": 3, "Plan Rows": 3, "Actual Startup Time": 12021.421, "Node Type": "Nested Loop", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 6073217.54, "Actual Total Time": 26141.033, "Startup Cost": 2399293.14, "Plans": [ { "Plan Width": 18, "Actu...
{ "Plan Width": 26, "Actual Rows": 3, "Plan Rows": 3, "Actual Startup Time": 6571.326, "Node Type": "Aggregate", "Actual Loops": 1, "Parallel Aware": false, "Strategy": "Hashed", "Total Cost": 967246.71, "Actual Total Time": 6571.329, "Startup Cost": 967246.68, "Plans": [ { "Relation N...
medium
15
tpch
with sub as materialized (select l_extendedprice, l_shipdate from lineitem where l_shipdate <= current_timestamp) select sum(l_extendedprice * least(extract(epoch from (current_timestamp - l_shipdate)) / 60.0, 30) / 30) as t from sub;
select sum(l_extendedprice * least(extract(epoch from (current_timestamp - l_shipdate)) / 60.0, 30.0) / 30.0) as t from lineitem where l_shipdate <= current_timestamp;
17,971.034
11,328.785
{ "Plan Width": 8, "Actual Rows": 1, "Plan Rows": 1, "Actual Startup Time": 17878.47, "Node Type": "Aggregate", "Actual Loops": 1, "Parallel Aware": false, "Strategy": "Plain", "Total Cost": 1417151.95, "Actual Total Time": 17878.471, "Startup Cost": 1417151.94, "Plans": [ { "Relation ...
{ "Plan Width": 8, "Actual Rows": 1, "Plan Rows": 1, "Actual Startup Time": 11309.277, "Node Type": "Aggregate", "Actual Loops": 1, "Parallel Aware": false, "Strategy": "Plain", "Total Cost": 1057235.83, "Actual Total Time": 11309.278, "Startup Cost": 1057235.82, "Plans": [ { "Relation...
medium
16
tpch
SELECT li.l_orderkey, v.mindate AS minimum_date FROM lineitem li LEFT JOIN LATERAL ( SELECT MIN(mindate) AS mindate FROM (VALUES (li.l_shipdate), (li.l_commitdate), (li.l_receiptdate)) AS x(mindate) ) v ON true;
SELECT li.l_orderkey, CASE WHEN li.l_shipdate IS NULL AND li.l_commitdate IS NULL THEN li.l_receiptdate WHEN li.l_shipdate IS NULL AND li.l_receiptdate IS NULL THEN li.l_commitdate WHEN li.l_commitdate IS NULL AND li.l_receiptdate IS NULL THEN li.l_shipdate WHEN li.l_shipdate ...
26,725.222
3,620.935
{ "Plan Width": 8, "Actual Rows": 17996609, "Plan Rows": 17997578, "Actual Startup Time": 71.654, "Node Type": "Nested Loop", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 1867171.13, "Actual Total Time": 26069.581, "Startup Cost": 0.05, "Plans": [ { "Relation Name": "lineite...
{ "Relation Name": "lineitem", "Plan Width": 8, "Actual Rows": 17996609, "Plan Rows": 17997578, "Actual Startup Time": 80.827, "Node Type": "Seq Scan", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 697328.56, "Actual Total Time": 2977.373, "Startup Cost": 0, "Alias": "li" }
medium
17
tpch
WITH numbered_data AS (SELECT o_orderdate, COUNT(o_orderkey) AS new_customers, ROW_NUMBER() OVER (ORDER BY o_orderdate) AS rn FROM orders GROUP BY o_orderdate), window_boundaries AS (SELECT nd_curr.o_orderdate AS current_dt, nd_curr.rn AS current_rn, nd_start.o_orderdate AS start_dt FROM numbered_data nd_curr LEFT JOIN...
WITH windowed_data AS (SELECT o_orderdate, COUNT(o_orderkey) AS new_customers, SUM(COUNT(o_orderkey)) OVER(ORDER BY o_orderdate ROWS BETWEEN 11 PRECEDING AND CURRENT ROW) AS running_cust_count, COUNT(*) OVER(ORDER BY o_orderdate ROWS UNBOUNDED PRECEDING) AS current_row_count FROM orders GROUP BY o_orderdate) SELECT o_o...
4,077.521
1,294.079
{ "Plan Width": 44, "Actual Rows": 2395, "Plan Rows": 236, "Actual Startup Time": 4031.078, "Node Type": "Sort", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 440645.06, "Actual Total Time": 4031.185, "Startup Cost": 440644.47, "Plans": [ { "Plan Width": 20, "Actual Row...
{ "Plan Width": 36, "Actual Rows": 2395, "Plan Rows": 802, "Actual Startup Time": 1272.111, "Node Type": "Subquery Scan", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 146051.16, "Actual Total Time": 1274.576, "Startup Cost": 145942.89, "Alias": "windowed_data", "Plans": [ { ...
hard
18
tpch
SELECT DISTINCT s_suppkey, TRUE AS has_nike_partner FROM (SELECT s.s_suppkey, s.s_name, COUNT(*) FILTER (WHERE s.s_name = 'Supplier#000000001') OVER (PARTITION BY s.s_suppkey) AS nike_per_supplier, COUNT(*) FILTER (WHERE s.s_name = 'Supplier#000000002') OVER (PARTITION BY s.s_suppkey) AS reebok_per_supplier FROM suppli...
SELECT s.s_suppkey, bool_or(s.s_name = 'Supplier#000000001') AS has_nike_partner FROM supplier s JOIN partsupp ps ON s.s_suppkey = ps.ps_suppkey GROUP BY s.s_suppkey HAVING COUNT(CASE WHEN s.s_name = 'Supplier#000000001' THEN 1 END) > 0 AND COUNT(CASE WHEN s.s_name = 'Supplier#000000002' THEN 1 END) = 0;
1,602.248
833.775
{ "Plan Width": 5, "Actual Rows": 1, "Plan Rows": 4000, "Actual Startup Time": 6.7940000000000005, "Node Type": "Unique", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 166954.39, "Actual Total Time": 1582.958, "Startup Cost": 0.72, "Plans": [ { "Plan Width": 5, "Actual ...
{ "Plan Width": 5, "Actual Rows": 1, "Plan Rows": 50, "Actual Startup Time": 5.482, "Node Type": "Aggregate", "Actual Loops": 1, "Parallel Aware": false, "Strategy": "Sorted", "Total Cost": 119390.07, "Actual Total Time": 814.062, "Startup Cost": 0.72, "Plans": [ { "Plan Width": 30, ...
easy
19
tpch
SELECT t.* FROM lineitem t JOIN LATERAL ( SELECT count(*) AS cnt, sum(CASE WHEN t2.l_linenumber = 4 THEN 1 ELSE 0 END) AS s FROM lineitem t2 WHERE t2.l_orderkey = t.l_orderkey AND t2.l_partkey = t.l_partkey ) AS grp ON grp.cnt > 1 AND grp.s > 0;
SELECT t.* FROM lineitem AS t INNER JOIN ( SELECT l_orderkey, l_partkey FROM lineitem GROUP BY l_orderkey, l_partkey HAVING count(*) > 1 AND sum(CASE WHEN l_linenumber = 4 THEN 1 ELSE 0 END) > 0 ) AS tt ON tt.l_orderkey = t.l_orderkey AND tt.l_partkey = t.l_partkey;
46,498.001
16,782.248
{ "Plan Width": 117, "Actual Rows": 44, "Plan Rows": 17997578, "Actual Startup Time": 5520.438, "Node Type": "Nested Loop", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 168974682.86, "Actual Total Time": 46478.481, "Startup Cost": 9.33, "Plans": [ { "Relation Name": "lineite...
{ "Plan Width": 117, "Actual Rows": 44, "Plan Rows": 15, "Actual Startup Time": 2277.36, "Node Type": "Merge Join", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 2927640.26, "Actual Total Time": 16762.815, "Startup Cost": 4.67, "Plans": [ { "Plan Width": 8, "Actual Rows...
easy
20
tpch
SELECT tick_lower, tick_upper, ltv_usdc, ltv_eth FROM (SELECT l.l_shipdate AS tick_lower, l.l_commitdate AS tick_upper, l.l_extendedprice AS ltv_usdc, l.l_quantity AS ltv_eth, ROW_NUMBER() OVER () AS rn FROM lineitem l) AS numbered WHERE rn BETWEEN 1 AND (SELECT COUNT(*) FROM lineitem);
SELECT l.l_shipdate AS tick_lower, l.l_commitdate AS tick_upper, l.l_extendedprice AS ltv_usdc, l.l_quantity AS ltv_eth FROM lineitem l;
14,207.421
3,772.424
{ "Plan Width": 21, "Actual Rows": 17996609, "Plan Rows": 89981, "Actual Startup Time": 3695.045, "Node Type": "Subquery Scan", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 1399284.09, "Actual Total Time": 13559.215, "Startup Cost": 387046.79, "Alias": "numbered", "Plans": [ { ...
{ "Relation Name": "lineitem", "Plan Width": 21, "Actual Rows": 17996609, "Plan Rows": 17996248, "Actual Startup Time": 72.684, "Node Type": "Seq Scan", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 517340.48, "Actual Total Time": 3121.206, "Startup Cost": 0, "Alias": "l" }
medium
21
tpch
WITH ranked_plans AS (SELECT o.o_orderkey AS id, o.o_orderdate AS planeffectivedate, l.l_shipdate AS planeterminationdate, l.l_receiptdate AS enddate, ROW_NUMBER() OVER (PARTITION BY o.o_orderkey ORDER BY o.o_orderdate) AS rn FROM orders o JOIN lineitem l ON o.o_orderkey = l.l_orderkey), joined_plans AS (SELECT r1.id, ...
WITH plan_data AS (SELECT o.o_orderkey AS id, o.o_orderdate AS planeffectivedate, l.l_shipdate AS planeterminationdate, LEAD(o.o_orderdate, 1) OVER (PARTITION BY o.o_orderkey ORDER BY o.o_orderdate) AS next_plan_eff_date, l.l_receiptdate AS enddate FROM orders o JOIN lineitem l ON o.o_orderkey = l.l_orderkey) SELECT id...
61,910.071
31,033.217
{ "Plan Width": 52, "Actual Rows": 17996609, "Plan Rows": 8096623552, "Actual Startup Time": 49136.772, "Node Type": "Merge Join", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 213149656.65, "Actual Total Time": 60840.4, "Startup Cost": 10509114.75, "Plans": [ { "Plan Width":...
{ "Plan Width": 52, "Actual Rows": 17996609, "Plan Rows": 17996248, "Actual Startup Time": 18926.015, "Node Type": "Subquery Scan", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 4368658.12, "Actual Total Time": 30185.854, "Startup Cost": 3738789.44, "Alias": "plan_data", "Plans": [ ...
hard
22
tpch
SELECT d.day, COUNT(DISTINCT t.o_custkey) FROM (SELECT DISTINCT date_trunc('day', o_orderdate) AS day FROM orders) AS d JOIN orders t ON date_trunc('day', t.o_orderdate) = d.day GROUP BY d.day
SELECT date_trunc('day', o_orderdate) AS day, COUNT(DISTINCT o_custkey) FROM orders GROUP BY date_trunc('day', o_orderdate)
8,267.078
4,683.618
{ "Plan Width": 16, "Actual Rows": 2406, "Plan Rows": 2406, "Actual Startup Time": 6873.775, "Node Type": "Aggregate", "Actual Loops": 1, "Parallel Aware": false, "Strategy": "Sorted", "Total Cost": 978147.9, "Actual Total Time": 8231.854, "Startup Cost": 944376.99, "Plans": [ { "Plan ...
{ "Plan Width": 16, "Actual Rows": 2406, "Plan Rows": 2406, "Actual Startup Time": 3283.975, "Node Type": "Aggregate", "Actual Loops": 1, "Parallel Aware": false, "Strategy": "Sorted", "Total Cost": 830585.66, "Actual Total Time": 4644.925, "Startup Cost": 796802.73, "Plans": [ { "Plan...
easy
23
tpch
SELECT p.p_name, (SELECT SUM(ps.ps_supplycost) FROM partsupp ps WHERE ps.ps_partkey = p.p_partkey) AS total FROM part p WHERE EXISTS (SELECT 1 FROM partsupp ps2 WHERE ps2.ps_partkey = p.p_partkey);
SELECT p.p_name, n.total FROM (SELECT SUM(ps.ps_supplycost) as total, ps.ps_partkey FROM partsupp ps GROUP BY ps.ps_partkey) n JOIN part p ON p.p_partkey = n.ps_partkey
2,897.451
1,494.049
{ "Plan Width": 65, "Actual Rows": 600000, "Plan Rows": 401125, "Actual Startup Time": 116.258, "Node Type": "Merge Join", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 3544575, "Actual Total Time": 2844.028, "Startup Cost": 1.4, "Plans": [ { "Relation Name": "part", "P...
{ "Plan Width": 65, "Actual Rows": 600000, "Plan Rows": 401125, "Actual Startup Time": 5.8469999999999995, "Node Type": "Merge Join", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 158278.19, "Actual Total Time": 1450.018, "Startup Cost": 0.85, "Plans": [ { "Plan Width": 36, ...
easy
24
tpch
SELECT * FROM lineitem l WHERE (SELECT COUNT(*) FROM lineitem l2 WHERE l2.l_orderkey = l.l_orderkey AND l2.l_returnflag = 'R') = 0;
WITH r_orders AS (SELECT DISTINCT l_orderkey FROM lineitem WHERE l_returnflag = 'R') SELECT l.* FROM lineitem l LEFT JOIN r_orders ro ON l.l_orderkey = ro.l_orderkey WHERE ro.l_orderkey IS NULL;
189,480.463
34,291.46
{ "Relation Name": "lineitem", "Plan Width": 117, "Actual Rows": 31858169, "Plan Rows": 299959, "Actual Startup Time": 88.978, "Node Type": "Seq Scan", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 813112310.78, "Actual Total Time": 187709.608, "Startup Cost": 0, "Alias": "l", "Pla...
{ "Plan Width": 117, "Actual Rows": 31858169, "Plan Rows": 1, "Actual Startup Time": 140.188, "Node Type": "Merge Join", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 5755112.54, "Actual Total Time": 33048.663, "Startup Cost": 0.88, "Plans": [ { "Relation Name": "lineitem", ...
easy
25
tpch
SELECT * FROM supplier WHERE s_suppkey IN (SELECT l_suppkey FROM lineitem GROUP BY l_suppkey HAVING COUNT(*) = (SELECT MAX(order_count) FROM (SELECT COUNT(*) AS order_count FROM lineitem GROUP BY l_suppkey) AS counts))
SELECT s.* FROM supplier s JOIN (SELECT l_suppkey FROM (SELECT l_suppkey, RANK() OVER (ORDER BY COUNT(*) DESC) as rnk FROM lineitem GROUP BY l_suppkey) ranked WHERE rnk = 1) top_supplier ON s.s_suppkey = top_supplier.l_suppkey;
5,059.529
2,694.391
{ "Plan Width": 144, "Actual Rows": 1, "Plan Rows": 150, "Actual Startup Time": 3861.712, "Node Type": "Nested Loop", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 866032.76, "Actual Total Time": 5038.443, "Startup Cost": 432713.58, "Plans": [ { "Plan Width": 4, "Actual...
{ "Plan Width": 144, "Actual Rows": 1, "Plan Rows": 150, "Actual Startup Time": 2657.146, "Node Type": "Nested Loop", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 436376.48, "Actual Total Time": 2674.885, "Startup Cost": 434570.42, "Plans": [ { "Plan Width": 4, "Actual...
medium
26
tpch
SELECT l_returnflag, COUNT(*) AS count FROM lineitem GROUP BY l_returnflag ORDER BY l_returnflag LIMIT (SELECT COUNT(DISTINCT l_returnflag) FROM lineitem);
select l_returnflag, count(*) from lineitem group by l_returnflag
15,320.66
5,128.996
{ "Plan Width": 10, "Actual Rows": 3, "Plan Rows": 1, "Actual Startup Time": 15301.639, "Node Type": "Limit", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 1169652.89, "Actual Total Time": 15301.642, "Startup Cost": 1169652.88, "Plans": [ { "Plan Width": 8, "Actual Rows...
{ "Plan Width": 10, "Actual Rows": 3, "Plan Rows": 3, "Actual Startup Time": 5110.161, "Node Type": "Aggregate", "Actual Loops": 1, "Parallel Aware": false, "Strategy": "Hashed", "Total Cost": 607321.75, "Actual Total Time": 5110.163, "Startup Cost": 607321.72, "Plans": [ { "Relation N...
easy
27
tpch
SELECT v.visitor, c.unique_shops FROM (SELECT DISTINCT l_orderkey AS visitor FROM lineitem) v CROSS JOIN LATERAL (SELECT COUNT(*) AS unique_shops FROM (SELECT 1 FROM lineitem t2 WHERE t2.l_orderkey = v.visitor GROUP BY t2.l_suppkey) s) c ORDER BY c.unique_shops DESC;
SELECT l_orderkey AS visitor, cardinality(array_agg(DISTINCT l_suppkey)) AS unique_shops FROM lineitem GROUP BY l_orderkey ORDER BY unique_shops DESC;
26,547.724
12,270.745
{ "Plan Width": 12, "Actual Rows": 4500000, "Plan Rows": 405253, "Actual Startup Time": 25906.447, "Node Type": "Sort", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 4668607.31, "Actual Total Time": 26314.993, "Startup Cost": 4667594.17, "Plans": [ { "Plan Width": 12, "...
{ "Plan Width": 8, "Actual Rows": 4500000, "Plan Rows": 405253, "Actual Startup Time": 11665.573, "Node Type": "Sort", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 862322.13, "Actual Total Time": 12076.974, "Startup Cost": 861309, "Plans": [ { "Plan Width": 8, "Actual ...
medium
28
tpch
SELECT t.id, t.short_title, t.arr[1] AS classictax1, t.arr[2] AS classictax2, t.arr[3] AS classictax3, t.arr[4] AS classictax4 FROM ( SELECT p.p_partkey AS id, p.p_name AS short_title, ARRAY( SELECT ps.ps_supplycost FROM partsupp ps ...
SELECT p.p_partkey AS id, p.p_name AS short_title, MAX(CASE WHEN c.rnk = 1 THEN c.classictax END) AS classictax1, MAX(CASE WHEN c.rnk = 2 THEN c.classictax END) AS classictax2, MAX(CASE WHEN c.rnk = 3 THEN c.classictax END) AS classictax3, MAX(CASE WHEN c.rnk = 4 THEN c.classictax END) AS classi...
12,078.556
3,973.407
{ "Relation Name": "part", "Plan Width": 109, "Actual Rows": 600000, "Plan Rows": 600000, "Actual Startup Time": 130.623, "Node Type": "Index Scan", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 20722196.27, "Actual Total Time": 12022.769, "Startup Cost": 0.42, "Alias": "p", "Plans...
{ "Plan Width": 165, "Actual Rows": 600000, "Plan Rows": 600000, "Actual Startup Time": 1366.583, "Node Type": "Aggregate", "Actual Loops": 1, "Parallel Aware": false, "Strategy": "Sorted", "Total Cost": 554113.81, "Actual Total Time": 3909.191, "Startup Cost": 412736.27, "Plans": [ { ...
medium
29
tpch
SELECT MIN(l.l_shipdate) AS min_date, MAX(l.l_shipdate) AS max_date FROM customer AS c INNER JOIN orders AS o ON c.c_custkey = o.o_custkey INNER JOIN lineitem AS l ON o.o_orderkey = l.l_orderkey WHERE l.l_quantity IS NOT NULL;
SELECT MIN(l.l_shipdate) AS min_date, MAX(l.l_shipdate) AS max_date FROM lineitem l WHERE l.l_quantity IS NOT NULL AND EXISTS (SELECT 1 FROM orders o JOIN customer c ON o.o_custkey = c.c_custkey WHERE o.o_orderkey = l.l_orderkey);
17,094.238
12,861.882
{ "Plan Width": 8, "Actual Rows": 1, "Plan Rows": 1, "Actual Startup Time": 17073.783, "Node Type": "Aggregate", "Actual Loops": 1, "Parallel Aware": false, "Strategy": "Plain", "Total Cost": 1218526.49, "Actual Total Time": 17073.788, "Startup Cost": 1218526.48, "Plans": [ { "Plan Wid...
{ "Plan Width": 8, "Actual Rows": 1, "Plan Rows": 1, "Actual Startup Time": 12842.788, "Node Type": "Aggregate", "Actual Loops": 1, "Parallel Aware": false, "Strategy": "Plain", "Total Cost": 1367852.96, "Actual Total Time": 12842.793, "Startup Cost": 1367852.95, "Plans": [ { "Plan Wid...
easy
30
tpch
SELECT * FROM lineitem WHERE l_comment !~ '^image.*\.jpg$';
SELECT * FROM lineitem WHERE l_comment NOT LIKE 'image%.jpg'
12,728.496
3,605.56
{ "Relation Name": "lineitem", "Plan Width": 117, "Actual Rows": 17996609, "Plan Rows": 17994449, "Actual Startup Time": 80.879, "Node Type": "Seq Scan", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 562331.1, "Actual Total Time": 12040, "Startup Cost": 0, "Alias": "lineitem" }
{ "Relation Name": "lineitem", "Plan Width": 117, "Actual Rows": 17996609, "Plan Rows": 17994449, "Actual Startup Time": 78.897, "Node Type": "Seq Scan", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 562331.1, "Actual Total Time": 2962.221, "Startup Cost": 0, "Alias": "lineitem" }
medium
31
tpch
SELECT n.n_name, 'price' category, ROUND(avg(b.price), 1) price FROM nation n LEFT OUTER JOIN (SELECT l.l_extendedprice - 100.00 AS price, c.c_nationkey FROM lineitem l JOIN orders o ON l.l_orderkey = o.o_orderkey JOIN customer c ON o.o_custkey = c.c_custkey WHERE l.l_shipdate BETWEEN '1998-12-01' AND '1998-12-31') b O...
WITH filtered_lineitems AS (SELECT l.l_extendedprice - 100.00 AS price, c.c_nationkey FROM lineitem l JOIN orders o ON l.l_orderkey = o.o_orderkey JOIN customer c ON o.o_custkey = c.c_custkey WHERE l.l_shipdate BETWEEN '1998-12-01' AND '1998-12-31'), join_nation_lineitem AS (SELECT n.n_name, fl.price FROM nation n LEFT...
3,531.099
2,072.004
{ "Plan Width": 90, "Actual Rows": 25, "Plan Rows": 25, "Actual Startup Time": 3512.096, "Node Type": "Aggregate", "Actual Loops": 1, "Parallel Aware": false, "Strategy": "Sorted", "Total Cost": 652675.13, "Actual Total Time": 3512.14, "Startup Cost": 652621.45, "Plans": [ { "Plan Widt...
{ "Plan Width": 90, "Actual Rows": 25, "Plan Rows": 25, "Actual Startup Time": 2050.875, "Node Type": "Aggregate", "Actual Loops": 1, "Parallel Aware": false, "Strategy": "Sorted", "Total Cost": 652675.13, "Actual Total Time": 2050.919, "Startup Cost": 652621.45, "Plans": [ { "Plan Wid...
hard
32
tpch
SELECT i.* FROM lineitem i JOIN LATERAL (SELECT 1) l ON true;
select * from lineitem;
4,453.351
3,190.403
{ "Relation Name": "lineitem", "Plan Width": 117, "Actual Rows": 17996609, "Plan Rows": 17997578, "Actual Startup Time": 0.36, "Node Type": "Seq Scan", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 517352.78, "Actual Total Time": 3803.507, "Startup Cost": 0, "Alias": "i" }
{ "Relation Name": "lineitem", "Plan Width": 117, "Actual Rows": 17996609, "Plan Rows": 17997578, "Actual Startup Time": 0.015, "Node Type": "Seq Scan", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 517352.78, "Actual Total Time": 2489.967, "Startup Cost": 0, "Alias": "lineitem" }
easy
33
tpch
select * from lineitem where l_suppkey in (select l_suppkey from lineitem group by l_suppkey having count(distinct l_partkey) > 1) and l_partkey = 123;
with member_counts as (select l_suppkey, count(distinct l_partkey) as ext_id_count from lineitem group by l_suppkey) select t.* from lineitem t join member_counts mc on t.l_suppkey = mc.l_suppkey where mc.ext_id_count > 1 and t.l_partkey = 123;
63,256.894
43,293.651
{ "Plan Width": 117, "Actual Rows": 38, "Plan Rows": 10, "Actual Startup Time": 2648.081, "Node Type": "Merge Join", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 1782127.09, "Actual Total Time": 63237.717, "Startup Cost": 0.88, "Plans": [ { "Relation Name": "lineitem", ...
{ "Plan Width": 117, "Actual Rows": 38, "Plan Rows": 10, "Actual Startup Time": 442.674, "Node Type": "Merge Join", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 1782127.09, "Actual Total Time": 43274.251, "Startup Cost": 0.88, "Plans": [ { "Relation Name": "lineitem", ...
medium
34
tpch
select m.att as highest_attendance, m.att - (m.att * 0.15) as lowest_attendance, d.month from (select distinct extract(month from o_orderdate) as month from orders) d cross join lateral (select o_totalprice as att from orders o where extract(month from o_orderdate) = d.month order by o_totalprice desc limit 1) m;
select t.max_att as highest_attendance, t.max_att - (t.max_att * 0.15) as lowest_attendance, t.month from (select extract(month from o_orderdate) as month, max(o_totalprice) as max_att from orders group by extract(month from o_orderdate)) t;
10,964.996
1,989.231
{ "Plan Width": 48, "Actual Rows": 12, "Plan Rows": 2406, "Actual Startup Time": 2262.476, "Node Type": "Nested Loop", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 378219439.68, "Actual Total Time": 10945.667, "Startup Cost": 314153.76, "Plans": [ { "Plan Width": 8, "A...
{ "Plan Width": 72, "Actual Rows": 12, "Plan Rows": 2406, "Actual Startup Time": 1969.628, "Node Type": "Subquery Scan", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 168341.76, "Actual Total Time": 1969.651, "Startup Cost": 168269.58, "Alias": "t", "Plans": [ { "Plan Width...
medium
35
tpch
WITH filter_list AS (SELECT unnest(ARRAY['RAIL','TRUCK','SHIP','AIR','MAIL','FOB','REG AIR','AIR FREIGHT']) AS shipmode) SELECT l.* FROM lineitem l JOIN filter_list f ON l.l_shipmode = f.shipmode
SELECT * FROM lineitem l WHERE l.l_shipmode IN ('RAIL','TRUCK','SHIP','AIR','MAIL','FOB','REG AIR','AIR FREIGHT')
12,472.449
5,084.57
{ "Plan Width": 117, "Actual Rows": 17996609, "Plan Rows": 719850, "Actual Startup Time": 213.588, "Node Type": "Hash Join", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 614520.46, "Actual Total Time": 11805.983, "Startup Cost": 0.24, "Plans": [ { "Relation Name": "lineitem"...
{ "Relation Name": "lineitem", "Plan Width": 117, "Actual Rows": 17996609, "Plan Rows": 11879130, "Actual Startup Time": 72.28, "Node Type": "Seq Scan", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 697302.96, "Actual Total Time": 4428.263, "Startup Cost": 0, "Alias": "l" }
easy
36
tpch
SELECT Result, StartDate, EndDate, Games FROM ( SELECT o_orderstatus AS Result, MIN(o_orderdate) AS StartDate, MAX(o_orderdate) AS EndDate, COUNT(*) AS Games FROM ( SELECT GR.o_orderstatus, GR.o_orderdate, (SELECT COUNT...
WITH FilteredOrders AS ( SELECT o_orderdate, o_orderstatus FROM orders WHERE o_custkey = 4 ), GroupMarkers AS ( SELECT o_orderdate, o_orderstatus, CASE WHEN o_orderstatus <> LAG(o_orderstatus, 1, o_orderstatus) OVER (ORDER BY o_orderdate) THEN 1 ELSE 0 END AS is_n...
3,367.423
0.342
{ "Plan Width": 18, "Actual Rows": 1, "Plan Rows": 9, "Actual Startup Time": 3346.406, "Node Type": "Sort", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 2668962.92, "Actual Total Time": 3346.408, "Startup Cost": 2668962.9, "Plans": [ { "Plan Width": 18, "Actual Rows": ...
{ "Plan Width": 26, "Actual Rows": 1, "Plan Rows": 9, "Actual Startup Time": 0.18, "Node Type": "Sort", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 77.92, "Actual Total Time": 0.181, "Startup Cost": 77.9, "Plans": [ { "Plan Width": 26, "Actual Rows": 1, "Plan Ro...
hard
37
tpch
WITH pre_agg AS MATERIALIZED (SELECT p.p_type, l.l_quantity FROM part p JOIN lineitem l ON p.p_partkey = l.l_partkey) SELECT p_type, SUM(l_quantity) FROM pre_agg GROUP BY p_type;
SELECT p.p_type, SUM(l.l_quantity) AS sum FROM part p JOIN lineitem l ON p.p_partkey = l.l_partkey GROUP BY p.p_type;
19,115.519
13,291.28
{ "Plan Width": 100, "Actual Rows": 150, "Plan Rows": 200, "Actual Startup Time": 18968.333, "Node Type": "Aggregate", "Actual Loops": 1, "Parallel Aware": false, "Strategy": "Hashed", "Total Cost": 1224228.66, "Actual Total Time": 18968.375, "Startup Cost": 1224226.16, "Plans": [ { "P...
{ "Plan Width": 53, "Actual Rows": 150, "Plan Rows": 150, "Actual Startup Time": 13272.417, "Node Type": "Aggregate", "Actual Loops": 1, "Parallel Aware": false, "Strategy": "Hashed", "Total Cost": 864303.08, "Actual Total Time": 13272.456, "Startup Cost": 864301.2, "Plans": [ { "Plan ...
easy
38
tpch
SELECT DISTINCT l.l_orderkey FROM lineitem l JOIN lineitem l_prelim ON l.l_orderkey = l_prelim.l_orderkey AND l_prelim.l_returnflag = 'R' LEFT JOIN lineitem l_final ON l.l_orderkey = l_final.l_orderkey AND l_final.l_returnflag = 'N' WHERE l_final.l_orderkey IS NULL
SELECT l1.l_orderkey FROM lineitem l1 LEFT JOIN lineitem l2 ON l1.l_orderkey = l2.l_orderkey AND l2.l_returnflag = 'N' WHERE l1.l_returnflag = 'R' AND l2.l_orderkey IS NULL GROUP BY l1.l_orderkey
54,667.467
10,098.373
{ "Plan Width": 4, "Actual Rows": 1850743, "Plan Rows": 1, "Actual Startup Time": 51042.382, "Node Type": "Unique", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 1406024.54, "Actual Total Time": 54458.987, "Startup Cost": 1406024.53, "Plans": [ { "Plan Width": 4, "Actua...
{ "Plan Width": 4, "Actual Rows": 1850743, "Plan Rows": 1, "Actual Startup Time": 9156.453, "Node Type": "Group", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 1376315.21, "Actual Total Time": 9996.509, "Startup Cost": 1376315.21, "Plans": [ { "Plan Width": 4, "Actual R...
medium
39
tpch
WITH t AS ( SELECT l_shipdate, l_quantity, p_container FROM lineitem JOIN part ON lineitem.l_partkey = part.p_partkey WHERE p_name LIKE 'sienna%' AND l_shipdate >= '1995-02-15' AND l_shipdate <= '1995-03-01' AND l_shipmode = 'SHIP' AND l_linestatus = 'F' ), min_max_...
WITH t AS ( SELECT l_shipdate, l_quantity, p_container FROM lineitem JOIN part ON lineitem.l_partkey = part.p_partkey WHERE p_name LIKE 'sienna%' AND l_shipdate >= '1995-02-15' AND l_shipdate <= '1995-03-01' AND l_shipmode = 'SHIP' AND l_linestatus = 'F' ), base AS ...
8,540.853
1,079.412
{ "Plan Width": 176, "Actual Rows": 1, "Plan Rows": 1, "Actual Startup Time": 8516.637, "Node Type": "Nested Loop", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 621587.53, "Actual Total Time": 8516.645, "Startup Cost": 621587.37, "Plans": [ { "Plan Width": 20, "Actual ...
{ "Plan Width": 176, "Actual Rows": 1, "Plan Rows": 1, "Actual Startup Time": 1059.633, "Node Type": "Nested Loop", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 621580.31, "Actual Total Time": 1059.637, "Startup Cost": 621580.21, "Plans": [ { "Plan Width": 20, "Actual ...
hard
40
tpch
SELECT * FROM lineitem t1 WHERE t1.l_shipdate = (SELECT MAX(t2.l_shipdate) FROM lineitem t2 WHERE t2.l_orderkey = t1.l_orderkey);
SELECT t1.* FROM lineitem t1 INNER JOIN (SELECT l_orderkey, MAX(l_shipdate) AS max_shipdate FROM lineitem GROUP BY l_orderkey) t2 ON t1.l_orderkey = t2.l_orderkey AND t1.l_shipdate = t2.max_shipdate;
207,690.691
44,672.807
{ "Relation Name": "lineitem", "Plan Width": 117, "Actual Rows": 15240492, "Plan Rows": 299959, "Actual Startup Time": 138.849, "Node Type": "Seq Scan", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 807563077.79, "Actual Total Time": 206809.081, "Startup Cost": 0, "Alias": "t1", "P...
{ "Plan Width": 117, "Actual Rows": 15240492, "Plan Rows": 23911, "Actual Startup Time": 213.899, "Node Type": "Merge Join", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 6021732.41, "Actual Total Time": 44050.475, "Startup Cost": 0.88, "Plans": [ { "Plan Width": 8, "Ac...
medium
41
tpch
WITH DATA AS MATERIALIZED ( SELECT o_orderpriority, o_clerk, o_comment FROM orders WHERE o_orderkey > 0 AND o_orderstatus = 'O' AND NOT o_comment = 'string' ) SELECT o_orderpriority, o_clerk, CASE WHEN o_comment IS NULL THEN 'other_string' WHEN o_comment = 'another_value' THEN 's...
SELECT t.o_orderpriority, t.o_clerk, CASE WHEN t.o_comment IS NULL THEN 'other_string' WHEN t.o_comment = 'another_value' THEN 'some_string' ELSE 'default' END FROM orders t WHERE t.o_orderkey > 0 AND t.o_orderstatus = 'O' AND t.o_comment <> 'string'
2,042.329
950.246
{ "Plan Width": 160, "Actual Rows": 2192938, "Plan Rows": 2174646, "Actual Startup Time": 5.909, "Node Type": "CTE Scan", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 205950.17, "Actual Total Time": 1911.892, "Startup Cost": 157020.63, "Alias": "data", "Plans": [ { "Relati...
{ "Relation Name": "orders", "Plan Width": 64, "Actual Rows": 2192938, "Plan Rows": 2174646, "Actual Startup Time": 5.321, "Node Type": "Seq Scan", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 162457.25, "Actual Total Time": 850.912, "Startup Cost": 0, "Alias": "t" }
medium
42
tpch
WITH t_data AS MATERIALIZED (SELECT * FROM lineitem) SELECT * FROM t_data;
SELECT * FROM lineitem
8,247.222
2,633.83
{ "Plan Width": 370, "Actual Rows": 17996609, "Plan Rows": 17996248, "Actual Startup Time": 0.195, "Node Type": "CTE Scan", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 877265.44, "Actual Total Time": 7240.791, "Startup Cost": 517340.48, "Alias": "t_data", "Plans": [ { "Re...
{ "Relation Name": "lineitem", "Plan Width": 117, "Actual Rows": 17996609, "Plan Rows": 17996248, "Actual Startup Time": 0.018000000000000002, "Node Type": "Seq Scan", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 517340.48, "Actual Total Time": 1986.045, "Startup Cost": 0, "Alias": ...
easy
43
tpch
SELECT * FROM customer ORDER BY (c_mktsegment = 'AUTOMOBILE' OR c_mktsegment = 'BUILDING') DESC, (c_mktsegment = 'FURNITURE') DESC, (c_mktsegment = 'MACHINERY') DESC, (c_mktsegment = 'HOUSEHOLD') DESC;
SELECT * FROM customer ORDER BY (CASE WHEN c_mktsegment = 'AUTOMOBILE' OR c_mktsegment = 'BUILDING' THEN 1 WHEN c_mktsegment = 'FURNITURE' THEN 2 WHEN c_mktsegment = 'MACHINERY' THEN 3 WHEN c_mktsegment = 'HOUSEHOLD' THEN 4 ELSE 5 END);
1,044.716
550.619
{ "Plan Width": 163, "Actual Rows": 450000, "Plan Rows": 450000, "Actual Startup Time": 695.622, "Node Type": "Sort", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 138083.02, "Actual Total Time": 961.976, "Startup Cost": 136958.02, "Plans": [ { "Relation Name": "customer", ...
{ "Plan Width": 163, "Actual Rows": 450000, "Plan Rows": 450000, "Actual Startup Time": 400.047, "Node Type": "Sort", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 138083.02, "Actual Total Time": 475.254, "Startup Cost": 136958.02, "Plans": [ { "Relation Name": "customer", ...
easy
44
tpch
SELECT o.array1, o.array2, (SELECT COALESCE(SUM((SELECT count(*) FROM jsonb_array_elements_text(to_jsonb(o.array2)) b(el2) WHERE b.el2 = a.el1)), 0) > 1 FROM jsonb_array_elements_text(to_jsonb(o.array1)) a(el1) ) FROM ( SELECT orders.o_orderkey, array_agg(lineitem.l_partkey) AS array1, ...
SELECT o.array1, o.array2, (SELECT COALESCE(SUM(cardinality(array_positions(o.array2, el1))), 0) > 1 FROM unnest(o.array1) a1(el1) ) FROM ( SELECT orders.o_orderkey, array_agg(lineitem.l_partkey) AS array1, array_agg(lineitem.l_suppkey) AS array2 FROM orders JOIN lineitem O...
56,985.317
25,746.186
{ "Plan Width": 65, "Actual Rows": 4500000, "Plan Rows": 4499579, "Actual Startup Time": 155.414, "Node Type": "Subquery Scan", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 577358593.13, "Actual Total Time": 56660.209, "Startup Cost": 18.98, "Alias": "o", "Plans": [ { "Pla...
{ "Plan Width": 65, "Actual Rows": 4500000, "Plan Rows": 4499579, "Actual Startup Time": 128.444, "Node Type": "Subquery Scan", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 2177409.56, "Actual Total Time": 25543.41, "Startup Cost": 18.98, "Alias": "o", "Plans": [ { "Plan W...
hard
45
tpch
WITH DistinctRawTagStrings AS (SELECT DISTINCT trim(both '[]' from (l_comment)) AS cleaned_tags_string FROM lineitem WHERE l_comment IS NOT NULL AND trim(both '[]' from (l_comment)) != ''), UnnestedDistinctTagValues AS (SELECT cleaned_tags_string, unnest(string_to_array(cleaned_tags_string, ',')) AS tag_value FROM Dist...
SELECT d.*, unnested_tag.tag_value AS tag FROM lineitem d CROSS JOIN LATERAL (SELECT unnest(string_to_array(trim(both '[]' from (d.l_comment)), ',')) AS tag_value) AS unnested_tag;
68,993.316
26,056.143
{ "Plan Width": 149, "Actual Rows": 19719920, "Plan Rows": 17996248, "Actual Startup Time": 38656.155, "Node Type": "Hash Join", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 4650576.75, "Actual Total Time": 68096.015, "Startup Cost": 2713272.97, "Plans": [ { "Relation Name":...
{ "Plan Width": 149, "Actual Rows": 19719920, "Plan Rows": 179962480, "Actual Startup Time": 119.487, "Node Type": "Nested Loop", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 5421318.06, "Actual Total Time": 25322.529, "Startup Cost": 0, "Plans": [ { "Relation Name": "lineit...
hard
46
tpch
WITH grouped_exams AS MATERIALIZED ( SELECT l.l_orderkey, p.p_name, COUNT(*) AS test_count FROM lineitem l JOIN part p ON l.l_partkey = p.p_partkey GROUP BY l.l_orderkey, p.p_name ) SELECT l_orderkey, p_name, test_count FROM grouped_exams ORDER BY l_orderkey, p_name;
SELECT l.l_orderkey, p.p_name, COUNT(*) AS test_count FROM lineitem l JOIN part p ON l.l_partkey = p.p_partkey GROUP BY l.l_orderkey, p.p_name ORDER BY l.l_orderkey, p.p_name;
49,871.157
30,819.739
{ "Plan Width": 140, "Actual Rows": 17996548, "Plan Rows": 17996248, "Actual Startup Time": 44772.07, "Node Type": "Sort", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 11035782.77, "Actual Total Time": 48667.707, "Startup Cost": 10990792.15, "Plans": [ { "Plan Width": 45, ...
{ "Plan Width": 45, "Actual Rows": 17996548, "Plan Rows": 17996248, "Actual Startup Time": 20208.001, "Node Type": "Aggregate", "Actual Loops": 1, "Parallel Aware": false, "Strategy": "Sorted", "Total Cost": 4745180.14, "Actual Total Time": 30017.332, "Startup Cost": 4385255.18, "Plans": [ {...
medium
47
tpch
WITH ActiveCounts AS (SELECT o.o_orderdate, SUM(CASE WHEN o.o_orderstatus = 'O' THEN 1 ELSE 0 END) AS active_ids_count FROM orders o GROUP BY o.o_orderdate), ActiveLists AS (SELECT o.o_orderdate, STRING_AGG(CASE WHEN o.o_orderstatus = 'O' THEN o.o_orderkey::TEXT END, ',' ORDER BY o.o_orderkey) AS listagg_active_ids, ST...
SELECT o.o_orderdate AS "Some Date", SUM(CASE WHEN o.o_orderstatus = 'O' THEN 1 ELSE 0 END) AS "active id numbers", STRING_AGG(CASE WHEN o.o_orderstatus = 'O' THEN o.o_orderkey::TEXT END, ',' ORDER BY o.o_orderkey) AS "Listagg List of active id numbers", STRING_AGG(CASE WHEN o.o_orderstatus = 'O' THEN o.o_orderkey::TEX...
5,914.558
4,637.903
{ "Plan Width": 76, "Actual Rows": 2406, "Plan Rows": 28944, "Actual Startup Time": 3583.797, "Node Type": "Hash Join", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 1044124.08, "Actual Total Time": 5873.682, "Startup Cost": 931547.98, "Plans": [ { "Plan Width": 68, "Ac...
{ "Plan Width": 76, "Actual Rows": 2406, "Plan Rows": 2406, "Actual Startup Time": 2182.823, "Node Type": "Aggregate", "Actual Loops": 1, "Parallel Aware": false, "Strategy": "Sorted", "Total Cost": 909482.63, "Actual Total Time": 4602.551, "Startup Cost": 774435.14, "Plans": [ { "Plan...
medium
48
tpch
SELECT * FROM lineitem ORDER BY (10 - l_quantity) ASC
SELECT * FROM lineitem ORDER BY l_quantity DESC
52,921.413
27,830.758
{ "Plan Width": 149, "Actual Rows": 17996609, "Plan Rows": 17996248, "Actual Startup Time": 44738.761, "Node Type": "Sort", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 6835686.44, "Actual Total Time": 49848.281, "Startup Cost": 6790695.82, "Plans": [ { "Relation Name": "lin...
{ "Plan Width": 117, "Actual Rows": 17996609, "Plan Rows": 17996248, "Actual Startup Time": 23077.952, "Node Type": "Sort", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 6052566.82, "Actual Total Time": 26746.366, "Startup Cost": 6007576.2, "Plans": [ { "Relation Name": "line...
medium
49
tpch
SELECT n_name, nation_count AS max_dep FROM (SELECT n.n_name, COUNT(*) AS nation_count FROM customer c JOIN nation n ON c.c_nationkey = n.n_nationkey GROUP BY n.n_name) main WHERE NOT EXISTS (SELECT 1 FROM customer c2 JOIN nation n2 ON c2.c_nationkey = n2.n_nationkey WHERE n2.n_name <> main.n_name GROUP BY n2.n_name HA...
WITH nation_counts AS (SELECT n.n_name, COUNT(*) AS nation_count FROM customer c JOIN nation n ON c.c_nationkey = n.n_nationkey GROUP BY n.n_name), ranked_nations AS (SELECT n_name, nation_count, FIRST_VALUE(nation_count) OVER (ORDER BY nation_count DESC) AS max_count FROM nation_counts) SELECT n_name, nation_count AS ...
5,087.704
199.271
{ "Plan Width": 34, "Actual Rows": 1, "Plan Rows": 12, "Actual Startup Time": 1382.233, "Node Type": "Subquery Scan", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 306515.3, "Actual Total Time": 5067.544, "Startup Cost": 11875.49, "Alias": "main", "Plans": [ { "Plan Width":...
{ "Plan Width": 34, "Actual Rows": 1, "Plan Rows": 1, "Actual Startup Time": 199.117, "Node Type": "Subquery Scan", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 11877.32, "Actual Total Time": 199.14, "Startup Cost": 11876.57, "Alias": "ranked_nations", "Plans": [ { "Plan W...
medium
50
tpch
select c.c_custkey, c.c_name from customer c except select c2.c_custkey, c2.c_name from customer c2 join orders o on c2.c_custkey = o.o_custkey
select c.c_custkey, c.c_name from customer c where not exists(select 1 from orders o where o.o_custkey = c.c_custkey)
5,483.612
940.032
{ "Plan Width": 76, "Actual Rows": 150006, "Plan Rows": 450000, "Actual Startup Time": 4373.773, "Node Type": "SetOp", "Actual Loops": 1, "Parallel Aware": false, "Strategy": "Sorted", "Total Cost": 1280295.46, "Actual Total Time": 5422.427, "Startup Cost": 1243167.61, "Plans": [ { "Pl...
{ "Plan Width": 23, "Actual Rows": 150006, "Plan Rows": 191471, "Actual Startup Time": 3.658, "Node Type": "Merge Join", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 169974, "Actual Total Time": 914.399, "Startup Cost": 0.85, "Plans": [ { "Relation Name": "customer", "...
easy
51
tpch
WITH aggregated_dates AS ( SELECT o.o_custkey, c.c_name, c.c_nationkey, ARRAY_AGG(o.o_orderdate ORDER BY o.o_orderdate) AS date_array FROM orders o JOIN customer c ON o.o_custkey = c.c_custkey GROUP BY o.o_custkey, c.c_name, c.c_nationkey ), expanded_dates AS ( SELECT o_custkey, c_name, c_nationkey, date_value, ordinal...
WITH numbered_data AS ( SELECT o.o_orderkey, o.o_custkey, o.o_orderdate, c.c_name, c.c_nationkey, ROW_NUMBER() OVER (PARTITION BY o.o_custkey, c.c_name, c.c_nationkey ORDER BY o.o_orderdate) as seqnum FROM orders o JOIN customer c on o.o_custkey = c.c_custkey ) SELECT o_custkey, c_name, c_nationkey, MIN(o_orderdate), M...
21,153.584
13,271.84
{ "Plan Width": 43, "Actual Rows": 4470952, "Plan Rows": 4499580, "Actual Startup Time": 257.054, "Node Type": "Aggregate", "Actual Loops": 1, "Parallel Aware": false, "Strategy": "Sorted", "Total Cost": 5914665.93, "Actual Total Time": 20901.871, "Startup Cost": 35.51, "Plans": [ { "P...
{ "Plan Width": 43, "Actual Rows": 4470952, "Plan Rows": 4499579, "Actual Startup Time": 5924.352, "Node Type": "Aggregate", "Actual Loops": 1, "Parallel Aware": false, "Strategy": "Sorted", "Total Cost": 1449764.82, "Actual Total Time": 12999.103, "Startup Cost": 909815.39, "Plans": [ { ...
hard
52
tpch
SELECT c.c_name, o.o_totalprice FROM orders o JOIN customer c ON o.o_custkey = c.c_custkey WHERE o.o_orderpriority = '1-URGENT' AND o.o_orderstatus IN ('F', 'P') AND NOT EXISTS ( SELECT 1 FROM orders o2 WHERE o2.o_orderpriority = '1-URGENT' AND o2.o_orderstatus IN ('F', 'P') AND o2.o_totalprice > o....
SELECT c.c_name, o.o_totalprice FROM orders o JOIN customer c ON o.o_custkey = c.c_custkey JOIN ( SELECT MAX(o2.o_totalprice) AS max_price FROM orders o2 WHERE o2.o_orderpriority = '1-URGENT' AND o2.o_orderstatus IN ('F', 'P') ) mx ON o.o_totalprice = mx.max_price WHERE o.o_orderpriority = '1-URGENT' AND o....
4,336.273
1,715.432
{ "Plan Width": 27, "Actual Rows": 1, "Plan Rows": 307223, "Actual Startup Time": 1911.932, "Node Type": "Nested Loop", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 2677456351.9, "Actual Total Time": 4314.499, "Startup Cost": 0.42, "Plans": [ { "Plan Width": 12, "Actua...
{ "Plan Width": 27, "Actual Rows": 1, "Plan Rows": 1, "Actual Startup Time": 1076.962, "Node Type": "Nested Loop", "Actual Loops": 1, "Parallel Aware": false, "Total Cost": 293929.75, "Actual Total Time": 1697.558, "Startup Cost": 146936.24, "Plans": [ { "Plan Width": 12, "Actual R...
medium
End of preview. Expand in Data Studio

Effi-SQL

Update 2026-06-12

We release Effi-SQL, a dataset suite for SQL efficiency optimization.

This collection includes:

  • Effi-SQL Benchmark: a benchmark for evaluating SQL efficiency optimization methods.
  • Diff-SQL Training Dataset: training data used by Diff-SQL, including data for the Patch Generator and Constraint Aligner.

Dataset Fields

Effi-SQL Benchmark

  • id: A unique identifier for each benchmark instance.
  • db: The database name.
  • base_sql: The original slow SQL query.
  • optimized_sql: The human-verified optimized SQL query.
  • base_time: Execution time of the original SQL query.
  • fast_time: Execution time of the optimized SQL query.
  • base_explain_analyze: Query execution plan (QEP) output for the original SQL query.
  • optimized_explain_analyze: Query execution plan (QEP) output for the optimized SQL query.
  • difficulty: The difficulty label of the benchmark instance.

Diff-SQL Training Dataset

  • instance_id: A unique identifier for each training instance.
  • prompt: The model input prompt, including the slow SQL query, relevant database information, and the query execution plan of the slow SQL query.
  • response: The target response, including optimization or refinement reasoning and the target patch.
  • db: The database name of the slow SQL query.
  • base_sql: The original slow SQL query.
  • optimized_sql: The optimized SQL query used as the reference target.

Related Resources

Downloads last month
3