mysql 避免使用in语句_mysql – 使用巨大的NOT IN语句优化查询

博客讨论了一个关于优化MySQL查询的问题,该查询旨在找出所有在2011年3月15日或之前存在的唯一源代码站点,但排除了之后出现的站点。尽管源站点和时间戳上有索引,查询仍然运行缓慢。依赖子查询的解决方案可能导致全表扫描,影响性能。寻求改善查询速度和索引使用的策略。
摘要由CSDN通过智能技术生成

我试图在某个时间戳之前找到仅存在的源代码.这个查询对于这项工作来说似乎很差.知道如何优化或可能改进的索引吗?

select distinct sourcesite

from contentmeta

where timestamp <= '2011-03-15'

and sourcesite not in (

select distinct sourcesite

from contentmeta

where timestamp>'2011-03-15'

);

源站点和时间戳上有一个索引,但查询仍然需要很长时间

mysql> EXPLAIN select distinct sourcesite from contentmeta where timestamp <= '2011-03-15' and sourcesite not in (select distinct sourcesite from contentmeta where timestamp>'2011-03-15');

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

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

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

| 1 | PRIMARY | contentmeta | index | NULL | sitetime | 14 | NULL | 725697 | Using where; Using index |

| 2 | DEPENDENT SUBQUERY | contentmeta | index_subquery | sitetime | sitetime | 5 | func | 48 | Using index; Using where; Full scan on NULL key |

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值