私の Web アプリケーションは、連絡先テーブルにインポートする必要がある電話のカンマ区切りのリストを受け取ります。リストには、100,000 から 1,000,000 のアイテムを含めることができます。ストアドプロシージャを実装しました。しかし、それでも私には遅すぎます。それを改善するために私を助けてもらえますか?
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `import_phones`
(IN ContactGroupId int, IN strPhones mediumtext, OUT total int, OUT inserted int)
main: BEGIN
    declare delim varchar(1) default(',');
    declare delimPtr int unsigned default(0); -- pointer to current spliter position
    declare startPtr int unsigned default(1);
    declare phone1 nvarchar(20);
    set total = 0; -- counter of total rows
    set inserted = 0; -- counter of inserted rows
    if strPhones is null or length(strPhones) < 1 then 
        leave main;
    end if;
    drop table if exists insertphones;
    create temporary table insertphones(phone nvarchar(20))
        engine = memory;
/***
-- split strPhones by delimiter
*/
    SET delimPtr = locate(delim, strPhones,startPtr);
    loop_label: while(delimPtr > 0) do
        insert into insertphones (phone) values (substring(strPhones,startPtr,delimPtr-startPtr));
        -- select delimPtr,startPtr, substring(strPhones,startPtr,delimPtr-startPtr); 
        set startPtr = delimPtr+1;
        set delimPtr = locate(delim, strPhones,startPtr);
    end while;
    if delimPtr = 0 then
        insert into insertphones (phone) values (substring(strPhones,startPtr,delimPtr-startPtr));
    end if;
    -- select delimPtr,startPtr; 
    select count(*) from insertphones into total;
/***
--  insert phones 
*/
    insert into contacts (Phone, ContactGroupId)
        select distinct(phone), ContactGroupId from insertphones where 
        phone not in (select Phone from contacts where ContactGroupId = ContactGroupId);
    SELECT ROW_COUNT() into inserted ;
    -- select total, inserted;
END