mysql where in 索引,MySQL索引-In与相等索引问题

Following queries run quite fast and instantaneously on mysql server:

SELECT table_name.id

FROM table_name

WHERE table_name.id in (10000)

SELECT table_name.id

from table_name

where table_name.id = (SELECT table_name.id

FROM table_name

WHERE table_name.id in (10000)

);

But if I change the second query to as following, then it takes more than 20 seconds:

SELECT table_name.id

from table_name

where table_name.id in (SELECT table_name.id

FROM table_name

WHERE table_name.id in (10000)

);

On doing explain, I get the following output. It is clear that there are some issues regarding how MySQL indexes the data, and use in keyword.

For first query:

+----+-------------+---------------+-------+---------------+---------+---------+-------+------+-------------+

| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |

+----+-------------+---------------+-------+---------------+---------+---------+-------+------+-------------+

| 1 | SIMPLE | table_name | const | PRIMARY | PRIMARY | 4 | const | 1 | Using index |

+----+-------------+---------------+-------+---------------+---------+---------+-------+------+-------------+

For second query:

+----+-------------+---------------+-------+---------------+---------+---------+-------+------+-------------+

| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |

+----+-------------+---------------+-------+---------------+---------+---------+-------+------+-------------+

| 1 | PRIMARY | table_name | const | PRIMARY | PRIMARY | 4 | const | 1 | Using index |

| 2 | SUBQUERY | table_name | const | PRIMARY | PRIMARY | 4 | | 1 | Using index |

+----+-------------+---------------+-------+---------------+---------+---------+-------+------+-------------+

For third query:

+----+--------------------+------------+-------+---------------+---------+---------+-------+---------+--------------------------+

| id | select_type | table_name | type | possible_keys | key | key_len | ref | rows | Extra |

+----+--------------------+------------+-------+---------------+---------+---------+-------+---------+--------------------------+

| 1 | PRIMARY | table_name | index | NULL | sentTo | 5 | NULL | 6250751 | Using where; Using index |

| 2 | DEPENDENT SUBQUERY | table_name | const | PRIMARY | PRIMARY | 4 | const | 1 | Using index |

+----+--------------------+------------+-------+---------------+---------+---------+-------+---------+--------------------------+

I am using InnoDB and have tried changing the third query to forcibly use the index as indicated by the following category.

解决方案

In first case you have only first record from subquery (It runs once, because equals is only for first value)

In second query you got Cartesian multiplication (each per each) because IN runs subquery for each row. Which is not good for performance

Try to use joins for these cases.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值