1

我想像这样插入我的数据库。但是表格中可能有 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');

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

4

2 回答 2

1

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

如果不是一次性的,您可以在一个循环的过程中执行此操作。

于 2012-04-03T22:11:05.663 回答
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
于 2015-07-13T22:23:08.353 回答