I’m running a query on a DB with 1GB RAM and 1 vCPU. it has 7 tables - 5 are small and 1 has 500K records and another has 2.5 Million records. I’m joining across 4 tables only on the IDs which arfe the PKs on each table and it can take a minute or longer to run this. It should not be this slow. Any suggestions? Oh yeah - I’m limiting the results to 1000 and I’m connecting directly via DBeaver
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
Hey there!
On a 1GB RAM / 1 vCPU Droplet, it’s not too surprising that a query joining large tables is taking a while, especially if one table has 2.5 million records. Even if you’re just joining on primary keys, MySQL still needs some breathing room to process everything efficiently.
I’d recommend starting with an
EXPLAIN
plan to see if the indexes are actually being used. Sometimes it looks fine on paper, but MySQL might be doing a full table scan behind the scenes.Also, keep in mind that with DBeaver or any external client, the query results have to travel across the network, so that adds a bit of latency too.
If this is more than just a dev/test setup, upgrading the Droplet to something like 2GB RAM and 1 or 2 vCPUs can help a lot. Even small bumps in resources can drastically improve query performance.
- Bobby
Heya, @775c69c916d345a7a367d867078ccb
You’re right — that query should not be taking a full minute if you’re joining on indexed primary keys and limiting the results to 1000 rows.
Run this before your query:
It’ll show if any table is doing a full table scan instead of using an index. If you see
"type: ALL"
in the plan for any table, that’s your bottleneck.Regards
Apart from what has been said, Ensure Indexes Are Present on Join Keys.
For all join conditions like:
Make sure:
a.id
is a PRIMARY KEYb.a_id
has an INDEXFor your large tables:
Also, If you’re using
LIMIT 1000
without anORDER BY
, the DB might still process all joins before applying the limit.