1

I'm trying to figure out what to look at to try to understand why I'm seeing much slower performance of COUNT WHERE queries on an AWS RDS MySql database compared to the same query on a MariaDB database running on a local CentOS server.

The queries look like:

SELECT COUNT(serial) FROM devices
    WHERE device_family="foo"
      AND serial > 1000
      AND serial < 10000000;

On the local instance queries like this return in a small number of seconds even when there are 20M records or so for the device family. On RDS it's taking many minutes.

My DB experience is limited, and I'm wondering how to understand what's happening here.

The RDS instance is db.m5.xlarge, 4 vCPU, 16 GB RAM, Provisioned IOPS (SSD) 1000 IOPS. I revved the IOPS up to 10K and only saw modest improvements.

The data in the relevant table was migrated from the local server to RDS and is essentially the same: 150M records with a handful of fields, no relationships or foreign keys (it's currently the only table in the DB).

The indexes (SHOW INDEXES FROM ) are consistent.

Not sure what else is relevant or where to go from here?

1
  • To check if everything is really consistent , run on both instances : EXPLAIN SELECT COUNT(serial) FROM devices WHERE device_family="foo" AND serial > 1000 AND serial < 10000000;
    – Arturo
    Jan 12, 2021 at 1:40

1 Answer 1

1

There are many reasons why there could be a discrepancy between your local and RDS instance. Besided running EXPLAIN on your query in both environments, you may consider adding the following index:

CREATE INDEX idx ON devices (device_family, serial);

This index, if used, would completely cover the WHERE clause and should speed up the query. You may also try swapping the order of the two columns in the index.

3
  • The order given is good. Swapping the order will be worse. Put the = things first, leave the "range" for last. See stackoverflow.com/questions/50239658/… for a lengthy "proof".
    – Rick James
    Jan 12, 2021 at 8:30
  • @RickJames I will definitely read your link, thanks for the information and learning. Jan 12, 2021 at 8:34
  • Thanks all! Once I looked at the EXPLAIN output the answer was staring me in the face - the composite primary key had changed from one that included serial and device_family so I just had to create the suggested index and all is copasetic.
    – Scott
    Jan 12, 2021 at 22:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.