I'm trying to improve performance of the following query which takes a 1+ minutes to execute:
SELECT *
FROM test
WHERE ( created_at < '2023-3-31 06:10:20.871' )
AND ( ( id > '2a95048f' )
OR ( id = '2a95048f'
AND created_at > '2022-12-27 23:53:24.958' ) )
ORDER BY id ASC,
created_at ASC
LIMIT 1000;
I altered the query by switching the order in the ORDER BY and the query returned results in 549ms:
SELECT *
FROM test
WHERE ( created_at < '2023-3-31 06:10:20.871' )
AND ( ( id > '2a95048f' )
OR ( id = '2a95048f'
AND created_at > '2022-12-27 23:53:24.958' ) )
ORDER BY created_at ASC,
id ASC
LIMIT 1000;
Here is some information about the indexes:
- ID - Cardinality 6.8 million, unique
- CREATED_AT - Cardinality 7.1 million, NOT unique
Why is one drastically performant than the other?
UPDATE - "SHOW INDEX FROM TEST"