SELECT MAX vs ORDER BY LIMIT 1 question has been answered here several times, but if I add a WHERE clause, things change dramatically
Here is my table:
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
id | b'int' | 'NO' | 'PRI' | None | 'auto_increment' |
'open_time' | b'bigint' | 'NO' | 'UNI' | None | '' |
Note, that both columns are indexed.
And here are the requests:
SELECT id from table
WHERE open_time > 0
ORDER BY id DESC LIMIT 1
SELECT MAX(id) from BTCUSDT1mHist
WHERE open_time > 0
EXPLAIN ANALYZE shows the following: ORDER BY:
-> Limit: 1 row(s) (cost=0.10 rows=1) (actual time=0.038..0.038 rows=1 loops=1)
-> Filter: (table.open_time > 0) (cost=0.10 rows=1) (actual time=0.037..0.037 rows=1 loops=1)
-> Index scan on table using PRIMARY (reverse) (cost=0.10 rows=2) (actual time=0.036..0.036 rows=1 loops=1)
MAX():
-> Aggregate: max(table.id) (cost=325890.06 rows=1081033) (actual time=1025.181..1025.181 rows=1 loops=1)
-> Filter: (table.open_time > 0) (cost=217786.76 rows=1081033) (actual time=0.032..866.890 rows=2180645 loops=1)
-> Index range scan on table using open_time (cost=217786.76 rows=1081033) (actual time=0.031..705.926 rows=2180645 loops=1)
ORDER BY finishes in 0.0012 seconds, while MAX() does in 1.026 seconds
I have read this question also, but it doesn't seem to cover my situation
The question is: why does MAX() takes so much longer than ORDER BY LIMIT?