现在我有两个表,其中一个表(A)的数据大概有一千多万,而另一个表(B)的数据大概有三千万,现在需要在B表中删除与A表相关联的数据 ,SQL应该怎么写执行速度最快?普通的delete語句执行了三个小时只删除一小部分数据 我写的SQL如下:
delete B from B,A where B.accountid=A.accountid;
delete B from B,A where B.accountid=A.accountid;
where exists (select 1 from A where B.accountid=A.accountid);
这个写法已经无可挑剔了。 关键是看一下你的索引都建了没有。A,B表上针对列accountid的索引。
我想在执行插入语句,然后执行select last_insert_id()这两步之前将select last_insert_id()置空,请问各位大侠如何实现?
这里的n表示你每次删除的记录数
赶紧去试试这个,我把limit写中间了,报错。我是想过这个方法....
写个存储过程循环执行吧。明天试试给各位结果哦
create table newtt as
select b.* from b left join a on a.accountid=v.accountid where b.accountid is null
呵呵,帅哥,exists 是非常慢的哦。
都是MyISAM 引擎。。
还没解决办法,急啊
这么大, limit 100,0000 这样删除 否则这操作哦会锁表
select b.* from b left join a where A.accountid is null;
truncate table b;
insert into b
select * from xx