2016-05-17 3 views
1

基本的に、私は3つのテーブルcustomer_profiles_lib、customer_profiles_tmp、customer_duplicates_tmpを持っています。MySQL IFが存在するINSERT else INSERTプロシージャ

customer_profiles_libの各レコードがcustomer_profiles_tmpにあるかどうかを確認します。そうでない場合は、customer_profiles_tmpにINSERTします。存在する場合は、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; 

ありがとうございました!

+0

テーブル、この質問のような:http://stackoverflow.com/questions/3884344/mysql-on-duplicate-key-insert-into-an-audit-or-log-table それは非常にきれいではないしかし、。 – pilsetnieks

答えて

1

全体RBAR手順は劇的な性能向上を持つ2つのSQL文に置き換えることができそうです:あなたは多分重複key`に `使う代わりに、`他にレコードを挿入するignore`を挿入することができ

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); 
+0

遅れて申し訳ありません...これは別の方法で動作するようになっていますが、この回答も素晴らしいようです! – brandoncluff

関連する問題