如何限制一次在INSERT INTO上插入的记录数



我想像这样插入数据库。但表中可能有20多万条记录。我想把插入分成10000个左右的组,我如何才能在MySQL中有效地做到这一点?

INSERT INTO `slugs` (`sku`, `tablename`)
select `SKU`, 'tableA'
from `tableA`
WHERE `SKU` NOT IN
(select `sku` from `slugs` where `tablename` = 'tableA');

示例代码:http://sqlfiddle.com/#!2/a75a1/8

LIMIT 10000添加到末尾并运行它,直到插入的行数=0。

如果它不是一次性的,你可以在一个有循环的过程中完成。。

试试这个:

SET @rank=0;
select rank
, IF(MOD(rank,10000)=0,CONCAT(IF(MOD(rank,10000)=1,CONCAT("insert ignore into table values (""",NAME,""",""",SKU,""")"),CONCAT(",(""",NAME,""",""",SKU,""")")),";"),IF(MOD(rank,10000)=1,CONCAT("insert ignore into table values (""",NAME,""",""",SKU,""")"),CONCAT(",(""",NAME,""",""",SKU,""")"))) as insert_statement
from (
SELECT @rank:=@rank+1 AS rank,NAME,SKU
from tablea 
) der

相关内容

  • 没有找到相关文章

最新更新