Answer the question
In order to leave comments, you need to log in
Long query execution in MySQL
Good afternoon.
Unexpectedly for me, one of the projects began to develop very quickly and now there was a problem with the database.
PHP + MySQL bundle is used.
There is a table, it already has ~ 20 million records%)
Table structure:
+---------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| task_id | int(9) | NO | PRI | NULL | |
| user_id | int(9) | NO | PRI | NULL | |
| checked | enum('0','1') | NO | MUL | 0 | |
| taken | enum('0','1') | NO | MUL | 0 | |
+---------+---------------+------+-----+---------+-------+
SELECT DISTINCT `task_id` FROM `tasks_pending` WHERE `checked`='0' AND `taken`='0' LIMIT 50;
Answer the question
In order to leave comments, you need to log in
Try the compound key checked, taken, task_id. In that order, it might help. What is an InnoDB table anyway?
You are using DISTINCT , which is bad in itself.
"If LIMIT # is specified with DISTINCT, MySQL will stop as soon as it finds # unique rows."
I advise you to read about how MySQL optimizes DISTINCT:
task_id_2 is redundant, the query that will use it in the same way will be able to use the first half of the primary index. so task_id_2 is a duplicate and you need to remove
the indexes separately for checked and separately for taken are not very useful.
index by (checked, taken, task_id) that the first commenter advised you - it really makes sense to try.
1. Show MySQL config,
2. One mysqltuner is not enough for you
3. I advise you to replace MySQL with Perocona Server.
Try to hang a composite index on checked,taken or even on task_id, checked,taken
and you can try changing the query to a query like
select task_id from tasks_pending where checked=0 and taken=0 group by task_id limit 50;
Materialize the query result. Create a separate table, insert when tasks arrive, delete when assigned.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question