因为以前曾经作过一个对这个库进行无条件删除的脚本,就是要删除数据量较大的表中的所有数据,但是因为客户要求,不能使用truncate table,怕破坏已有的库结构。所以只能用delete删,当时也遇到了日志文件过大的问题,当时采用的方法是分批删除,在SQL2K中用set rowcount @chunk,在SQL2K5中用delete top @chunk。这样的操作不仅使删除时间大大减少,而且让日志量大大减少,只增长了1G左右。 但是这次清除数据的工作需要加上条件,就是delete A from A where ....后面有条件的。再次使用分批删除的方法,却已经没效果了。 不知您知不知道这是为什么。
mysql not in 和 left join 效率问题记录
首先说明该条sql的功能是查询集合a不在集合b的数据。 not in的写法 复制代码 代码如下: select add_tb.RUID from (select distinct RUID from UserMsg where SubjectID =12 and CreateTime>"2009-8-14 15:30:00" and CreateTime<="2009-8-17 16:00:00" ) add_tb where add_tb.RUID not in (select distinct RUID from UserMsg where SubjectID =12 and CreateTime<"2009-8-14 15:30:00" )
Using index; Using where | | 2 | DERIVED | UserMsg | range | SubjectID,CreateTime | CreateTime | 9 | NULL | 1857 |
Using where; Using temporary | +----+--------------------+------------+----------------+---------------------------+------------+---------+------+------+--
----------------------------+ 分析:该条查询速度快原因为id=2的sql查询出来的结果比较少,所以id=1sql所以运行速度比较快,id=2的使用了临时表,不知道这个时候是否使用索引? 其中一种left join 复制代码 代码如下: select a.ruid,b.ruid from(select distinct RUID from UserMsg where SubjectID =12 and CreateTime >= "2009-8-14 15:30:00" and CreateTime<="2009-8-17 16:00:00" ) a left join ( select distinct RUID from UserMsg where SubjectID =12 and CreateTime< "2009-8-14 15:30:00" ) b on a.ruid = b.ruid where b.ruid is null
-------+ 分析:使用了两个临时表,并且两个临时表做了笛卡尔积,导致不能使用索引并且数据量很大 另外一种left join 复制代码 代码如下: select distinct a.RUID from UserMsg a left join UserMsg b on a.ruid = b.ruid and b.subjectID =12 and b.createTime < "2009-8-14 15:30:00" where a.subjectID =12 and a.createTime >= "2009-8-14 15:30:00" and a.createtime <="2009-8-17 16:00:00" and b.ruid is null;
--------------------+ | 1 | SIMPLE | a | range | SubjectID,CreateTime | CreateTime | 9 | NULL | 1839 | Using where;
Using temporary | | 1 | SIMPLE | b | ref | RUID,SubjectID,CreateTime | RUID | 96 | dream.a.RUID | 2 | Using where;
Not exists; Distinct | +----+-------------+-------+-------+---------------------------+------------+---------+--------------+------+---------------
--------------------+ 分析:两次查询都是用上了索引,并且查询时同时进行的,所以查询效率应该很高 使用not exists的sql 复制代码 代码如下: select distinct a.ruid from UserMsg a where a.subjectID =12 and a.createTime >= "2009-8-14 15:30:00" and a.createTime <="2009-8-17 16:00:00" and not exists ( select distinct RUID from UserMsg where subjectID =12 and createTime < "2009-8-14 15:30:00" and ruid=a.ruid )
laserhe帮忙分析问题总结 复制代码 代码如下: select a.ruid,b.ruid from( select distinct RUID from UserMsg where CreateTime >= "2009-8-14 15:30:00" and CreateTime<="2009-8-17 16:00:00" ) a left join UserMsg b on a.ruid = b.ruid and b.createTime < "2009-8-14 15:30:00" where b.ruid is null;
面开销最小的,选取并执行之。那么: explain select a.ruid,b.ruid from(select distinct RUID from UserMsg where CreateTime >= "2009-8-14 15:30:00"
and CreateTime<="2009-8-17 16:00:00" ) a left join UserMsg b on a.ruid = b.ruid and b.createTime < "2009-8-14 15:30:00"
where b.ruid is null; 和 explain select add_tb.RUID -> from (select distinct RUID -> from UserMsg -> where CreateTime>"2009-8-14 15:30:00" -> and CreateTime<="2009-8-17 16:00:00" -> ) add_tb -> where add_tb.RUID -> not in (select distinct RUID -> from UserMsg -> where CreateTime<"2009-8-14 15:30:00" -> ); explain +----+--------------------+------------+----------------+-----------------+------------+---------+------+------+------------
------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra
Using where | | 2 | DERIVED | UserMsg | range | CreateTime | CreateTime | 9 | NULL | 3509 | Using where;
Using temporary | +----+--------------------+------------+----------------+-----------------+------------+---------+------+------+------------
------------------+ 开销是完全一样的,开销可以从 rows 那个字段得出(基本上是rows那个字段各个行的数值的乘积,也就是笛卡尔积) 但是呢:下面这个: explain select a.ruid,b.ruid from(select distinct RUID from UserMsg where CreateTime >= "2009-8-14 15:30:00"
and CreateTime<="2009-8-17 16:00:00" ) a left join ( select distinct RUID from UserMsg where createTime < "2009-8-14
15:30:00" ) b on a.ruid = b.ruid where b.ruid is null; 执行时间21.31s +----+-------------+------------+-------+---------------+------------+---------+------+-------+-----------------------------
-+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra