【发布时间】:2016-05-17 22:39:13
【问题描述】:
基本上,我有 3 个表:customer_profiles_lib、customer_profiles_tmp 和 customer_duplicates_tmp。
我想检查 customer_profiles_lib 中的每条记录是否在 customer_profiles_tmp 中...如果不是,则 INSERT into customer_profiles_tmp... 如果是,则 INSERT INTO customer_duplicates_tmp。
我在一个程序中尝试过,但我有 900 万条记录要处理,而且速度太慢了……这是我所拥有的:
CREATE DEFINER=`company`@`%` PROCEDURE `customerImport`()
BEGIN
DECLARE unique_id INT;
DECLARE fin INT;
DECLARE curs CURSOR FOR SELECT customer_id AS unique_id FROM customer_profiles_lib;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET fin = 1;
OPEN curs;
SET fin = 0;
REPEAT
FETCH curs INTO unique_id;
IF (SELECT EXISTS (SELECT customer_id FROM customer_profiles_tmp WHERE customer_id = unique_id)) THEN
SELECT unique_id AS 'ADDING';
INSERT IGNORE INTO customer_duplicates_tmp (first, last, address_1, address_2, city, state, zipcode, email, customer_id, phone, store_number)
SELECT first, last, address_1, address_2, city, state, zipcode, email, customer_id, phone, store_number FROM customer_profiles_lib WHERE customer_id = unique_id ORDER BY customer_profile_id DESC LIMIT 1;
ELSE
SELECT unique_id AS 'SKIPPING';
INSERT IGNORE INTO customer_profiles_tmp (first, last, address_1, address_2, city, state, zipcode, email, customer_id, phone, store_number)
SELECT first, last, address_1, address_2, city, state, zipcode, email, customer_id, phone, store_number FROM customer_profiles_lib WHERE customer_id = unique_id ORDER BY customer_profile_id DESC LIMIT 1;
END IF;
UNTIL fin END REPEAT;
CLOSE curs;
END
这种方式需要 1 小时,并且适用于插入,但不会在我的 customer_duplicates_tmp 表中放入任何内容。
INSERT IGNORE INTO customer_profiles_tmp (
first,
last,
address_1,
address_2,
city,
state,
zipcode,
email,
customer_id,
phone,
store_number
)
SELECT
tmp.first,
tmp.last,
tmp.address_1,
tmp.address_2,
tmp.city,
tmp.state,
tmp.zipcode,
tmp.email,
tmp.customer_id,
tmp.phone,
tmp.store_number
FROM customer_profiles_lib AS tmp;
感谢您的帮助!
【问题讨论】:
-
您可以使用
on duplicate key而不是insert ignore将记录插入到另一个表中,有点像这个问题:stackoverflow.com/questions/3884344/… 虽然它不是很干净和漂亮。
标签: mysql sql stored-procedures insert