我想像这样插入我的数据库。但是表格中可能有 200k+ 条记录。我想将插入分成 10,000 个左右的组,我怎样才能只在 MySQL 中有效地做到这一点?
INSERT INTO `slugs` (`sku`, `tablename`)
select `SKU`, 'tableA'
from `tableA`
WHERE `SKU` NOT IN
(select `sku` from `slugs` where `tablename` = 'tableA');
我想像这样插入我的数据库。但是表格中可能有 200k+ 条记录。我想将插入分成 10,000 个左右的组,我怎样才能只在 MySQL 中有效地做到这一点?
INSERT INTO `slugs` (`sku`, `tablename`)
select `SKU`, 'tableA'
from `tableA`
WHERE `SKU` NOT IN
(select `sku` from `slugs` where `tablename` = 'tableA');
在末尾添加 aLIMIT 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