由于sql语句不支持子查询里 select * from a limit 0,1 特别是limit 0,1。
所以我不得不选择存储过程来实现2表(1个亿级别大表 1个4千记录的小表)关联删除数据LOg (id, userId,createTime,url) 这是过亿大表
a(userId,createTime, url,minId ) ------这个表是从a通过group by userId,createTime,url
having count(*)>100
order by userId,createTime,minId 得到的本来考虑用下面的语句删除
delete... where userId,createTime, url in (select userId,createTime, url from a limit 0,1)
and id >(select minid from a limit 0.1)
但是 mysql编译报错误 当前mysql版本不支持子查询里limit 0,1。这样不得已,选择存储过程。
现在mysql的存储过程不是很熟悉, 甚至
for 。等结构 , 还有list结构(存储来自select userId,createTime, url from a limit 0,1的值)--这样delete ..where 各个字段直接与致谢list集合变量的值比较哦就可以啦
都不熟悉,
请指点
所以我不得不选择存储过程来实现2表(1个亿级别大表 1个4千记录的小表)关联删除数据LOg (id, userId,createTime,url) 这是过亿大表
a(userId,createTime, url,minId ) ------这个表是从a通过group by userId,createTime,url
having count(*)>100
order by userId,createTime,minId 得到的本来考虑用下面的语句删除
delete... where userId,createTime, url in (select userId,createTime, url from a limit 0,1)
and id >(select minid from a limit 0.1)
但是 mysql编译报错误 当前mysql版本不支持子查询里limit 0,1。这样不得已,选择存储过程。
现在mysql的存储过程不是很熟悉, 甚至
for 。等结构 , 还有list结构(存储来自select userId,createTime, url from a limit 0,1的值)--这样delete ..where 各个字段直接与致谢list集合变量的值比较哦就可以啦
都不熟悉,
请指点
然后 delete from LOg where userId,createTime,url = cursor.userId,createTime,url and id <>minid 的。
如果多的话,建议不要用delete,因为会非常的慢
可以把先把要保留的数据select并insert进一个中间表,然后truncate源表,再把前面保留在中间表的数据insert回去
BEGIN
DECLARE business_date timestamp default current_timestamp;
DECLARE no_more_gbmes int;
DECLARE mon int;
DECLARE gid int;
DECLARE gbid int;
DECLARE accountid int;
DECLARE bsid int;
DECLARE gbmes_csr CURSOR FOR SELECT g_id,gb_id,account_id,bs_id FROM jz_gbmes;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_more_gbmes=1;
SELECT max(plandate) into business_date from jz_business_plan;
SET no_more_gbmes=0;
set mon = DATE_FORMAT(business_date,'%m');
WHILE (mon < DATE_FORMAT(Now(),'%m')) do
OPEN gbmes_csr;
REPEAT
FETCH gbmes_csr INTO gid,gbid,accountid,bsid;
insert into jz_business_plan(g_id,gb_id,account_id,bs_id) values(gid,gbid,accountid,bsid);
UNTIL no_more_gbmes
END REPEAT;
CLOSE gbmes_csr;
set mon = mon+1;
end WHILE;
END$$
DELIMITER ;
参考上面的例子
WHILE (mon < DATE_FORMAT(Now(),'%m')) do
OPEN gbmes_csr;
REPEAT
FETCH gbmes_csr INTO gid,gbid,accountid,bsid;
insert into jz_business_plan(g_id,gb_id,account_id,bs_id) values(gid,gbid,accountid,bsid);
UNTIL no_more_gbmes
END REPEAT;
CLOSE gbmes_csr;
set mon = mon+1;
end WHILE;
我的a表里咩有 mon这个日期增长字段 ,也没有自增长id,
问如何实现这个表的游标的向下移动