One developer reported there's a delete statement taking very long time to run on small tables.
The three tables involved this query, category_group, tmp_category_group category_group_info have 7400, 7600 and 25 rows respectively.
While checking slow query logs, shockingly I found the query has 3.8MM examined rows. Change the query to select statement will yield the same result.
# Query_time: 9 Lock_time: 0 Rows_sent: 0 Rows_examined: 3809728
delete from category_group where group_id = (select group_id from category_group_info x where x.group_name = 'Storylines') and category_id in ( select category_id from tmp_category_group cg where group_id = (select group_id from category_group_info x where x.group_name = 'KidsCharacter'));
# Query_time: 8 Lock_time: 0 Rows_sent: 0 Rows_examined: 3810182
select * from category_group where group_id = (select group_id from category_group_info x
where x.group_name = 'Storylines') and category_id in
( select category_id from tmp_category_group cg where group_id =
(select group_id from category_group_info x where x.group_name = 'KidsCharacter'));
The explain plan doesn't provide much help.
mysql> explain select * from category_group where group_id = (select group_id from category_group_info x where x.group_name = 'Storylines') and category_id in ( select category_id from tmp_category_group cg where group_id = (select group_id from category_group_info x where x.group_name = 'KidsCharacter'));
+----+--------------------+----------------+------+---------------+----------+---------+-------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+----------------+------+---------------+----------+---------+-------+------+--------------------------+
| 1 | PRIMARY | category_group | ref | group_id | group_id | 4 | const | 501 | Using where; Using index |
| 3 | DEPENDENT SUBQUERY | cg | ALL | NULL | NULL | NULL | NULL | 7396 | Using where |
| 4 | SUBQUERY | x | ALL | NULL | NULL | NULL | NULL | 25 | Using where |
| 2 | SUBQUERY | x | ALL | NULL | NULL | NULL | NULL | 25 | Using where |
+----+--------------------+----------------+------+---------------+----------+---------+-------+------+--------------------------+
4 rows in set (0.01 sec)
select category_id from tmp_category_group cg where group_id = ...
That end up leading me to add an index on group_id of tmp_category_group and the query running lightening fast now.
Could this be some sort of Bug?
mysql> select * from category_group where group_id = (select distinct group_id from category_group_info x where x.group_name = 'Storylines') and category_id in ( select distinct category_id from tmp_category_group cg where group_id = (select distinct group_id from category_group_info x where x.group_name = 'KidsCharacter'));
Empty set (0.31 sec)
mysql> delete from category_group
-> where group_id = (select group_id from category_group_info x where x.group_name = 'Storylines')
-> and category_id in (
-> select category_id
-> from tmp_category_group cg
-> where group_id = (select group_id from category_group_info x where x.group_name = 'KidsCharacter'));
Query OK, 0 rows affected (0.36 sec)
Empty set (0.31 sec)
mysql> delete from category_group
-> where group_id = (select group_id from category_group_info x where x.group_name = 'Storylines')
-> and category_id in (
-> select category_id
-> from tmp_category_group cg
-> where group_id = (select group_id from category_group_info x where x.group_name = 'KidsCharacter'));
Query OK, 0 rows affected (0.36 sec)
No comments:
Post a Comment