B
B
BusterX2019-04-13 14:59:23
MySQL
BusterX, 2019-04-13 14:59:23

How to optimally find the maximum value?

There is a docs table with 500,000 entries:
id | doc | base | date
Index: (base, date)
Task: find maximum value of date with specific base value
Tried queries:
1. SELECT `date` FROM `docs` WHERE `base` = 'b1' ORDER BY `date` DESC LIMIT 1;
Request time: 0.0009
EXPLAIN:
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | extra |
| 1 | SIMPLE | docs | NULL | ref | base_date | base_date | 78 | const | 7157 | 100.00 | Using where; using index |
1. SELECT MAX(`date`) FROM `docs` WHERE `base` = 'b1'
Query time: 0.0006
EXPLAIN:
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | extra |
| 1 | SIMPLE | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | Select tables optimized away |
Regardless of the query option used, the "Handler read rnd next" status variable is incremented by 9827 each time. Should
I pay attention to this? How can you optimize?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Bolshakov, 2019-04-13
@BusterX

In my opinion, the best solution for you would be:
SELECT MAX(поле) FROM `имя_таблицы` WHERE условие

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question