r/SQL • u/Effective_Ocelot_445 • May 07 '26
MySQL How do you optimize SQL queries when working with millions of rows in production databases?
I’m curious about the practical techniques people use in real-world systems to improve query performance and reduce execution time on large datasets.
37
Upvotes
54
u/Astrodynamics_1701 May 07 '26
For me it's:
6
u/OO_Ben Postgres - Retail Analytics May 07 '26
I don't think I've seen this specifically, but one that that helps me significantly are temp tables to prefilter data. And indexing those can help too if you're dealing with ton of rows each time.
As an example, I inherited a daily query that aggregates a sales summary. The previous person built it so it was doing a series of summed nested case statements with multiple subqueries. It was hard to read and very inefficient. It was like a 2-4 minute run for just the previous day, and we're talking only like maybe 20-30k sales rows it needed to aggregate.
I split it all out into a prefiltered temp table that isolated the appropriate lines into the buckets, and then ran a single aggregate step at the end. It went from being a 2 minute pull for a previous day update to a 45 second pull for all like 40M sales lines in our warehouse. Daily updates became less than a second. That was meaningful improvement right there.
5
u/j0holo May 07 '26
Slow query log, explain analyze mostly does the trick.
6
u/gorkemguzel32 May 07 '26
It heavily depends on your purpose, if you’re doing some analytical analysis you gotta use a columnar database like ClickHouse, performance difference is like day and night with transactional databases, if you’re looking for some anomalities within a transactional list it’s whole another thing you will benefit from indexes partitions and such.
3
u/lmarcantonio May 07 '26
EXPLAIN, EXPLAIN, EXPLAIN. After that usually it's better indices, view materialization or partitioning. It depends a lot with the kind of data and the access patterns; and if you are running online or in warehousing.
3
u/yankinwaoz May 07 '26 edited May 07 '26
Use partitions to allow parallal processing.
Use explain to see the plan. Review of predicates and the indexes. Look for full-table-scans.
Review the order of predicates because that can make large a difference depending on what type of DB you have.
If updating data, writing the SQL as a stored procedure that performs a commit at least every 2000 to 20k rows to prevent lock escalation, lock exhaustion, or time-outs.
Use materialized queries/views.
Make sure the query tells the database what type of locking is needed. Or more important, not needed. Are dirty reads okay? Great! Then your query can just blow past other people's locks.
Run statistics on the data from time to time. This gives the database the metadata it needs to optimize access paths.
And last thing. LOCK THE PLAN once you have it working well. You don't want some idiot making a small tweak to the schema and allowing the DB to generate a new plan on it's own without review in the middle of the night. Because it might generate a plan that doesn't work very well your performance goes down the toilet and no one knows why. Treat your plan like any other code change. It needs testing & review before being slammed into production.
This also benefits you because it saves the time of the DB having to sit there and generate a plan for the SQL before executing it. Especially if the SQL is dynamic. The DB will view the schema, the SQL, and figure out what it thinks if the best way to attack the problem. If you have some SQL that is being presented to the server thousands of times a day, that adds up.
Most DB's will recognize the SQL from before, then dig up the plan that it generated before. But it depends on how long it caches the the plans it generates. With a saved plan, it has already done the hard work, and has a pre-approved reliable plan that it can bring up to execute when the SQL shows up at the door.
I realized that I'm getting out of writing SQL queries and into DBA operations here. But the two go hand-in-hand if you want a healthy, happy, snappy database for the apps for use.
2
u/afinethingindeedlisa May 07 '26
Upgrade the warehouse to XL
2
u/asterix_rv May 07 '26
You can try out the AI assisted query optimization workflows in https://pgstudio.astrx.dev/ Its a useful guided tool that will help u identify hot paths in Explain analyze and suggest indexes and redesigns for the table.
2
u/mduell May 07 '26
https://use-the-index-luke.com/
1
u/Ginger-Dumpling May 07 '26
What are you trying to optimize? OLTP workloads would probably mean indexing and making sure the schema isn't weirdly modeled. OLAP stuff your probably looking at ordering, partitioning, ETL processes to denormalized structures, and ideally a columnar db.
Identify what you're trying to optimize. Use query plans to see if you're generally getting efficient plans. If not, take steps to improve. When all else fails don't ignore that you could be hardware bound.
1
u/trentsiggy May 07 '26
1
u/Creepy_Delay_6077 May 07 '26
While working in the production databases , optimization of sql queries is combination of query design,indexing strategy,partition plan
our main goal is to
reduce execution time
Disk I/O
Network shuffling
1
u/Acceptable-Cold-3830 May 07 '26
I used to teach a class in SQL Server performance and tuning. It gives you a good background on a lot of how sql server works, and how to approach specific problems. There are good online versions of that class. I would recommend it if you have the time.
1
u/_Toodaloo May 07 '26
Filter early on partition keys, Aggregate later
1
u/Aggressive_Ad_5454 May 07 '26
This is a vast topic that’s hard to answer in general.
My advice:
1
u/squadette23 May 07 '26
Systematic design of multi-join GROUP BY queries: https://kb.databasedesignbook.com/posts/systematic-design-of-join-queries/ is my take on one part of that question.
1
u/stlcdr May 08 '26
Real-world usage doesn’t often demand access to a whole dataset, but a subset. For example, time based records are often only used for the past few months. Not that this is your use case, but understanding that case can go a long way towards optimizing the structure and letting the database engine do its work.
1
u/Rough_Dinner6675 May 08 '26
Try hireall.in for best sql compiler
1
u/Front_Intention_5911 May 16 '26
A few things that made the biggest difference in my experience with large datasets:
The single biggest mistake I see is people optimizing the query syntax when the real problem is missing indexes or pulling more data than needed.
1
u/PatientlyAnxiously May 08 '26
You should definitely be using query optimization techniques, as others here have suggested.
I'll also add this: have you tried building your query into a flat table and keeping it updated via incremental processing? Out of those millions of rows in the underlying tables, how much of that data actually changes each day? If the answer is a small amount, you should consider incremental processing.
In a transactional DB like MySQL, you can use triggers to keep the downstream table updated incrementally & in realtime as the source data gets updated. Or if you have a downstream data warehouse then you can use other methods like dynamic tables in Snowflake.
1
u/Deep_Ad1959 May 27 '26
most of this thread is read-path tuning, which is right, but if 'millions of rows' also means heavy write volume, the ingest side is usually where the quieter wins hide. bulk loads through postgres' binary COPY protocol skip the per-statement parse/plan/bind that makes batched INSERTs fall over at scale, so it streams rows instead of re-planning each one. a laravel telemetry ingester i was looking at got to ~13.4k payloads/sec doing exactly that, where row-by-row inserts choked way earlier. for the read side everyone's right about EXPLAIN plus covering indexes, just don't let a slow write path disguise itself as a query problem. written with s4lai
1
u/1vim May 07 '26
A few techniques that make a real difference at scale:
Indexing strategy is the most impactful — covering indexes for your most frequent query patterns, partial indexes for filtered queries on large tables. Most people underindex.
Partitioning on date columns if you're dealing with time-series data. Query planner can skip entire partitions instead of scanning everything.
Materialized views for aggregations that get queried frequently but don't need to be real-time. Refresh on a schedule rather than computing on every query.
On the AI side — one thing worth exploring is moving the natural language query layer above your SQL layer entirely. Tools like Skopx let non-technical users ask questions in plain English and generate optimized queries automatically, which also means your data team stops fielding ad-hoc SQL requests and can focus on actual optimization work.
8
u/Grovbolle May 07 '26
Check the execution plans, build proper indexes, ensure statistics are decently up to date.
The answer is always check the execution plan in the cases I deal with.