优化CSV到MYSQL存储过程

时间:2017-04-09 22:00:02

标签: mysql

我已经在PHP和现在的MYSQL中尝试了这一点,并且这两个结果似乎在大约15秒内执行100个不同的项目。此过程采用CSV变量,仅插入当前不在数据库中的记录。在此之后,每个项目都插入到order_items_part_numbers表中,并带有相应的订单商品ID。这是我目前的代码。任何使这更快的帮助将不胜感激。

PROCEDURE `add_part_number_to_item_mysql`(_order_items_id INT, _part_numbers VARCHAR(5000))
BEGIN

DECLARE _part_number VARCHAR(50);
DECLARE _existing_part_number_id INT;

WHILE _part_numbers IS NOT NULL DO
SET _part_number = (SELECT CASE LOCATE(";", _part_numbers, 1)
 WHEN 0 THEN _part_numbers -- empty or single word
 ELSE SUBSTRING(_part_numbers, 1, LOCATE(";", _part_numbers, 1) - 1) -- multi-word
END);

SET _part_number = TRIM(_part_number);

SET _existing_part_number_id = (SELECT ID FROM inventory WHERE item_number = _part_number);

IF _existing_part_number_id IS NULL THEN
INSERT INTO inventory (item_number) VALUES (UPPER(_part_number));
SET _existing_part_number_id = @@IDENTITY;
END IF;

INSERT INTO order_items_part_numbers (order_items_id, inventory_id) VALUES (_order_items_id, _existing_part_number_id);


SET _part_numbers = (SELECT CASE LOCATE(";", _part_numbers, 1)
 WHEN 0 THEN NULL -- empty or single word
 ELSE SUBSTRING(_part_numbers, LOCATE(";", _part_numbers)+1) -- multi-word
 END);

SET _part_numbers = TRIM(_part_numbers);
END WHILE;

END 

1 个答案:

答案 0 :(得分:0)

不是使用相同的参数值重复运行LOCATE,而是将输出存储在局部变量中并在需要的地方使用。

如果你在php中使用完全相同的算法,我建议使用explode(),然后循环遍历生成的数组。