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!
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:
EXPLAIN SELECT ...;
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:
ON a.id = b.a_id
Make sure:
a.id
is a PRIMARY KEY
b.a_id
has an INDEX
For your large tables:
-- For 2.5M record table
CREATE INDEX IF NOT EXISTS idx_table1_fk ON table1(foreign_key_column);
-- For 500K record table
CREATE INDEX IF NOT EXISTS idx_table2_fk ON table2(foreign_key_column);
Also, If you’re using LIMIT 1000
without an ORDER BY
, the DB might still process all joins before applying the limit.
ORDER BY some_indexed_column
LIMIT 1000
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.