I
I
Ivan Ivanov2020-07-02 11:09:45
MySQL
Ivan Ivanov, 2020-07-02 11:09:45

How to limit search scope in mysql?

There is a table, in it it is a lot of records. How can I limit the area in which the search occurs (SELECT * from post where status = 1)? For example, let's search only in the first 1 million records. It is important for me that mysql does not go through all 13 million records. And analyzing the requests, I see that this is happening. Let the records be stored, but the selection is only from the first million records. Example:

MariaDB [yii2advanced]> DESCRIBE select * from scan limit 1;
+------+-------------+-------+------+---------------+------+---------+------+----------+-------+
| id   | select_type | table | type | possible_keys | key  | key_len | ref  | rows     | Extra |
+------+-------------+-------+------+---------------+------+---------+------+----------+-------+
|    1 | SIMPLE      | scan  | ALL  | NULL          | NULL | NULL    | NULL | 13460011 |       |
+------+-------------+-------+------+---------------+------+---------+------+----------+-------+

Another example:
MariaDB [yii2advanced]> DESCRIBE select * from scan where url like "%1%" limit 20;
+------+-------------+-------+------+---------------+------+---------+------+----------+-------------+
| id   | select_type | table | type | possible_keys | key  | key_len | ref  | rows     | Extra       |
+------+-------------+-------+------+---------------+------+---------+------+----------+-------------+
|    1 | SIMPLE      | scan  | ALL  | NULL          | NULL | NULL    | NULL | 13460011 | Using where |
+------+-------------+-------+------+---------------+------+---------+------+----------+-------------+

Once again, you need to search only in the first million records

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stalker_RED, 2020-07-02
@maksim_fix

If the id is autoincrement and there are no spaces in the numbering, then you can where id < 1000001
Well, or find out what id you have in the millionth place.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question