Question

Query taking 1 minute to run

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


Submit an answer


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!

Sign In or Sign Up to Answer

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.

Bobby Iliev
Site Moderator
Site Moderator badge
May 27, 2025

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

alexdo
Site Moderator
Site Moderator badge
May 27, 2025

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

KFSys
Site Moderator
Site Moderator badge
May 28, 2025

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

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

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

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.