DEV Community

DanielNnadi
DanielNnadi

Posted on

How I Cut SQL Query Time from 45 Seconds to 8 Seconds on 2.3 Million Rows

I inherited a SQL Server database with 2.3 million rows. Queries took 45 seconds. Users were frustrated. Dashboards timed out.

Here is exactly what I did.

Step 1: Find the slowest queries

I used SQL Server's query store to identify the top 10 worst performing queries.

Step 2: Check the execution plan

Missing index warnings everywhere. Also saw table scans on a 2 million row table.

Step 3: Add targeted indexes

Created two non-clustered indexes on the most filtered columns. No over-indexing. Just what the queries actually needed.

Step 4: Rewrite the worst join

One query was joining 6 tables with a cross apply that made no sense. Restructured to inner joins with proper filter ordering.

The result

45 seconds down to 8 seconds. An 82% improvement. Real-time dashboards started working again.

Key lesson

Check what is actually slow before changing anything. Most people skip this and waste time optimizing the wrong thing.

What is your fastest query optimization win?

Top comments (0)