【问题标题】:MySQL IF exists INSERT else INSERT procedureMySQL IF 存在 INSERT else INSERT 过程
【发布时间】: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;

感谢您的帮助!

【问题讨论】:

标签: mysql sql stored-procedures insert


【解决方案1】:

似乎整个 RBAR 过程可以用两条 SQL 语句代替,性能得到显着提升:

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 IN (SELECT customer_id FROM customer_profiles_tmp);

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 NOT IN (SELECT customer_id FROM customer_profiles_tmp);

【讨论】:

  • 抱歉耽搁了...我让这个以另一种方式工作,但这个答案似乎也很棒!
猜你喜欢
  • 1970-01-01
  • 2012-08-08
  • 2011-11-21
  • 1970-01-01
  • 2021-08-10
  • 1970-01-01
  • 2019-02-19
  • 2020-06-07
  • 2014-08-17
相关资源
最近更新 更多