| name | sql-optimization |
| plugin | coding |
| description | Universal SQL performance optimization assistant for comprehensive query tuning, indexing strategies, and database performance analysis across all SQL databases (MySQL, PostgreSQL, SQL Server, Oracle, Databricks). SQL style: sqlfmt lowercase, CTEs over subqueries, QUALIFY over row_number subqueries, GROUP BY ALL, meaningful aliases, alias-prefixed columns, no select *. |
SQL Performance Optimization Assistant
Expert SQL performance optimization for ${selection} (or entire project if no selection). Focus on universal SQL optimization techniques that work across MySQL, PostgreSQL, SQL Server, Oracle, and other SQL databases.
Before writing or rewriting any SQL or Python, check how formatting is configured in this project. Look for pyproject.toml, .sqlfmt.toml, ruff.toml, setup.cfg, or editor config files (.editorconfig). Use whatever line_length, formatter, and style settings are defined there — do not assume defaults.
🎯 Core Optimization Areas
Query Performance Analysis
select *
from orders
where year(created_at) = 2024
and customer_id in (select id from customers where status = 'active');
with active_customers as (
select cust.id
from customers as cust
where cust.status = 'active'
),
orders_2024 as (
select
ord.id,
ord.customer_id,
ord.total_amount,
ord.created_at,
from orders as ord
inner join active_customers as cust on ord.customer_id = cust.id
where ord.created_at >= '2024-01-01'
and ord.created_at < '2025-01-01'
)
select
ord.id,
ord.customer_id,
ord.total_amount,
ord.created_at,
from orders_2024 as ord;
Index Strategy Optimization
create index idx_user_data on users(email, first_name, last_name, created_at);
create index idx_users_email_created on users(email, created_at);
create index idx_users_name on users(last_name, first_name);
create index idx_users_active on users(status, created_at)
where status is not null;
CTE over Subquery / Correlated Subquery
select prod.product_name, prod.price
from products as prod
where prod.price > (
select avg(p2.price) from products as p2 where p2.category_id = prod.category_id
);
with products_with_avg as (
select
prod.product_name,
prod.price,
avg(prod.price) over (partition by prod.category_id) as avg_category_price,
from products as prod
)
select
pwa.product_name,
pwa.price,
from products_with_avg as pwa
where pwa.price > pwa.avg_category_price;
📊 Performance Tuning Techniques
JOIN Optimization
select o.*, c.name, p.product_name
from orders o
left join customers c on o.customer_id = c.id
left join order_items oi on o.id = oi.order_id
left join products p on oi.product_id = p.id
where o.created_at > '2024-01-01'
and c.status = 'active';
with recent_orders as (
select ord.id, ord.total_amount, ord.customer_id
from orders as ord
where ord.created_at > '2024-01-01'
)
select
ord.id,
ord.total_amount,
cust.name,
prod.product_name,
from recent_orders as ord
inner join customers as cust on ord.customer_id = cust.id and cust.status = 'active'
inner join order_items as items on ord.id = items.order_id
inner join products as prod on items.product_id = prod.id;
Pagination Optimization
select
prod.id,
prod.name,
prod.created_at,
from products as prod
order by prod.created_at desc
limit 20 offset 10000;
select
prod.id,
prod.name,
prod.created_at,
from products as prod
where prod.created_at < '2024-06-15 10:30:00'
order by prod.created_at desc
limit 20;
Aggregation — GROUP BY ALL
select
ord.customer_id,
cust.region,
cust.segment,
count(*) as order_count,
sum(ord.total_amount) as revenue,
from orders as ord
inner join customers as cust on ord.customer_id = cust.id
group by ord.customer_id, cust.region, cust.segment;
select
ord.customer_id,
cust.region,
cust.segment,
count(*) as order_count,
sum(ord.total_amount) as revenue,
from orders as ord
inner join customers as cust on ord.customer_id = cust.id
group by all;
select
ord.status,
count(*) as total_orders,
count(case when ord.priority = 'high' then 1 end) as high_priority_count,
sum(ord.total_amount) as total_revenue,
from orders as ord
group by all;
🔍 Query Anti-Patterns and Fixes
No SELECT * — Explicit Columns with Alias Prefix
select *
from orders as ord
join customers as cust on ord.customer_id = cust.id;
select
ord.id,
ord.created_at,
ord.total_amount,
cust.name,
cust.region,
from orders as ord
inner join customers as cust on ord.customer_id = cust.id;
QUALIFY over ROW_NUMBER Subquery
with ranked_orders as (
select
ord.id,
ord.customer_id,
ord.created_at,
ord.total_amount,
row_number() over (partition by ord.customer_id order by ord.created_at desc) as rn,
from orders as ord
)
select
ranked.id,
ranked.customer_id,
ranked.created_at,
ranked.total_amount,
from ranked_orders as ranked
where ranked.rn = 1;
select
ord.id,
ord.customer_id,
ord.created_at,
ord.total_amount,
from orders as ord
qualify row_number() over (partition by ord.customer_id order by ord.created_at desc) = 1;
select
prod.id,
prod.category_id,
prod.name,
prod.price,
from products as prod
qualify rank() over (partition by prod.category_id order by prod.price asc) <= 3;
WHERE Clause / Collation Optimization
select ord.id, ord.customer_email, ord.total_amount
from orders as ord
where upper(ord.customer_email) = 'JOHN@EXAMPLE.COM';
select ord.id, ord.customer_email, ord.total_amount
from orders as ord
where ord.customer_email = 'john@example.com';
alter table orders alter column customer_email type string collate utf8_lcase;
analyze table orders compute statistics for columns customer_email;
select ord.id, ord.customer_email, ord.total_amount
from orders as ord
where ord.customer_email = 'john@example.com';
OR vs UNION ALL
select prod.id, prod.name, prod.category, prod.price
from products as prod
where (prod.category = 'electronics' and prod.price < 1000)
or (prod.category = 'books' and prod.price < 50);
with electronics as (
select prod.id, prod.name, prod.category, prod.price
from products as prod
where prod.category = 'electronics'
and prod.price < 1000
),
books as (
select prod.id, prod.name, prod.category, prod.price
from products as prod
where prod.category = 'books'
and prod.price < 50
)
select electronics.id, electronics.name, electronics.category, electronics.price
from electronics
union all
select books.id, books.name, books.category, books.price
from books;
📈 Database-Agnostic Optimization
Batch Operations
insert into products (name, price) values ('Product 1', 10.00);
insert into products (name, price) values ('Product 2', 15.00);
insert into products (name, price) values ('Product 3', 20.00);
insert into products (name, price)
values
('Product 1', 10.00),
('Product 2', 15.00),
('Product 3', 20.00);
CTE over Temporary Tables
create temporary table temp_customer_totals as
select
ord.customer_id,
sum(ord.total_amount) as total_spent,
count(*) as order_count,
from orders as ord
where ord.created_at >= '2024-01-01'
group by ord.customer_id;
select cust.name, totals.total_spent, totals.order_count
from temp_customer_totals as totals
join customers as cust on totals.customer_id = cust.id
where totals.total_spent > 1000;
with customer_totals as (
select
ord.customer_id,
sum(ord.total_amount) as total_spent,
count(*) as order_count,
from orders as ord
where ord.created_at >= '2024-01-01'
group by all
),
high_value_customers as (
select ct.customer_id, ct.total_spent, ct.order_count
from customer_totals as ct
where ct.total_spent > 1000
)
select
cust.name,
hvc.total_spent,
hvc.order_count,
from high_value_customers as hvc
inner join customers as cust on hvc.customer_id = cust.id;
⚡ Databricks / Spark SQL Optimization
Collations for Case/Accent-Insensitive Comparisons
Up to 22× faster than lower() wrappers — enables Delta file-skipping and Photon optimization.
select cust.id, cust.name, cust.email
from customers as cust
where lower(cust.name) = 'john smith';
alter table customers alter column name type string collate utf8_lcase;
analyze table customers compute statistics for columns name;
select cust.id, cust.name, cust.email
from customers as cust
where cust.name = 'john smith';
Collation reference:
| Collation | Use case |
|---|
utf8_lcase | English case-insensitive (default choice) |
unicode | Unicode-aware, case-sensitive |
de, fr, el, ru, zh | Language-specific ordering |
<lang>_ai | Accent-insensitive (e.g., el_ai, fr_ai) |
select * from collations();
create table hero_names (
greek_name string collate el_ai,
english_name string collate utf8_lcase
);
QUALIFY for Deduplication / Top-N per Group
select
ord.id,
ord.customer_id,
ord.created_at,
ord.total_amount,
from orders as ord
qualify row_number() over (partition by ord.customer_id order by ord.created_at desc) = 1;
select
prod.id,
prod.category_id,
prod.name,
prod.price,
from products as prod
qualify rank() over (partition by prod.category_id order by prod.price asc) <= 3;
GROUP BY ALL
select
ord.status,
cust.region,
cust.segment,
count(*) as order_count,
sum(ord.total_amount) as revenue,
avg(ord.total_amount) as avg_order_value,
from orders as ord
inner join customers as cust on ord.customer_id = cust.id
group by all;
Z-ORDER and Liquid Clustering
optimize my_table zorder by (customer_id, event_date);
alter table my_table cluster by (customer_id, event_date);
🛠️ Index Management
Index Design Principles
create index idx_orders_covering
on orders(customer_id, created_at)
include (total_amount, status);
Partial Index Strategy
create index idx_orders_active
on orders(created_at)
where status in ('pending', 'processing');
📊 Performance Monitoring Queries
select sl.query_time, sl.lock_time, sl.rows_sent, sl.rows_examined, sl.sql_text
from mysql.slow_log as sl
order by sl.query_time desc;
select pss.query, pss.calls, pss.total_time, pss.mean_time
from pg_stat_statements as pss
order by pss.total_time desc;
with query_stats as (
select
qs.total_elapsed_time / qs.execution_count as avg_elapsed_time,
qs.execution_count,
qs.sql_handle,
qs.statement_start_offset,
qs.statement_end_offset,
from sys.dm_exec_query_stats as qs
)
select
qs.avg_elapsed_time,
qs.execution_count,
substring(
qt.text,
(qs.statement_start_offset / 2) + 1,
(
(case qs.statement_end_offset when -1 then datalength(qt.text) else qs.statement_end_offset end
- qs.statement_start_offset) / 2
) + 1
) as query_text,
from query_stats as qs
cross apply sys.dm_exec_sql_text(qs.sql_handle) as qt
order by qs.avg_elapsed_time desc;
explain cost
select ord.customer_id, count(*) as order_count from orders as ord group by all;
🧹 SQL Correctness and Clarity Patterns
FILTER (WHERE …) over CASE WHEN for Conditional Aggregation
select
count(case when ord.status = 'pending' then 1 end) as pending_count,
count(case when ord.status = 'shipped' then 1 end) as shipped_count,
sum(case when ord.priority = 'high' then ord.total_amount else 0 end) as high_priority_revenue,
from orders as ord;
select
count(*) filter (where ord.status = 'pending') as pending_count,
count(*) filter (where ord.status = 'shipped') as shipped_count,
sum(ord.total_amount) filter (where ord.priority = 'high') as high_priority_revenue,
from orders as ord;
NOT EXISTS over NOT IN (NULL Safety)
select ord.id, ord.customer_id, ord.total_amount
from orders as ord
where ord.customer_id not in (select cust.id from customers as cust where cust.status = 'inactive');
select ord.id, ord.customer_id, ord.total_amount
from orders as ord
where not exists (
select 1
from customers as cust
where cust.id = ord.customer_id
and cust.status = 'inactive'
);
select ord.id, ord.customer_id, ord.total_amount
from orders as ord
left join customers as cust
on ord.customer_id = cust.id
and cust.status = 'inactive'
where cust.id is null;
NULLS LAST / NULLS FIRST in ORDER BY
select prod.id, prod.name, prod.discontinued_at
from products as prod
order by prod.discontinued_at desc;
select prod.id, prod.name, prod.discontinued_at
from products as prod
order by prod.discontinued_at desc nulls last;
BETWEEN Gotcha with Timestamps — Use >= / <
select ord.id, ord.created_at, ord.total_amount
from orders as ord
where ord.created_at between '2024-01-01' and '2024-01-31';
select ord.id, ord.created_at, ord.total_amount
from orders as ord
where ord.created_at >= '2024-01-01'
and ord.created_at < '2024-02-01';
Boolean WHERE Predicate — No Redundant Comparison
select ord.id, ord.customer_id, ord.total_amount
from orders as ord
where ord.is_paid = true
and ord.is_cancelled = false;
select ord.id, ord.customer_id, ord.total_amount
from orders as ord
where ord.is_paid
and not ord.is_cancelled;
Boolean Expression as Alias — No CASE WHEN
select
ord.id,
ord.total_amount,
case when ord.total_amount > 1000 then true else false end as is_high_value,
case when ord.status = 'shipped' then true else false end as is_shipped,
from orders as ord;
select
ord.id,
ord.total_amount,
(ord.total_amount > 1000) as is_high_value,
(ord.status = 'shipped') as is_shipped,
from orders as ord;
EXISTS over COUNT(*) for Existence Checks
select cust.id, cust.name
from customers as cust
where (
select count(*) from orders as ord where ord.customer_id = cust.id
) > 0;
select cust.id, cust.name
from customers as cust
where exists (
select 1 from orders as ord where ord.customer_id = cust.id
);
Identifier Quoting — Backticks for Names, Single Quotes for Strings
select 'my name', 'order date'
from events;
select event.order, event.group
from events as event;
select
evt.`my name`,
evt.`order`,
evt.`group`,
from events as evt;
select
evt."my name",
evt."order",
evt."group",
from events as evt;
select cust.id, cust.name
from customers as cust
where cust.status = 'active'
and cust.region = 'EMEA';
Query Structure
Index Strategy
Data Types and Schema
Query Patterns
Performance Testing
📝 Optimization Methodology
- Identify: Use database-specific tools to find slow queries
- Analyze: Examine execution plans — look for full scans, high row counts, missing indexes
- Optimize: CTEs over subqueries ·
qualify over row_number wrappers · group by all · filter (where …) · collations · indexes · boolean clarity
- Test: Verify improvements with realistic data volumes
- Monitor: Continuously track query performance metrics
- Iterate: Regular review and optimization cycle
Focus on measurable improvements. Always test with realistic data volumes and query patterns.