我有一个存储过程,它运行缓慢。有没有更好的方法将表 ID 传递给过程而不是使用 FIND_IN_SET?
表列 e.fk_trans_history_id 是 INT(32)
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `1_trans_hist_ent_items_sel`(b INT,tid TEXT)
BEGIN
SELECT
e.id,
e.fk_trans_history_id,
e.fk_prod_pack_id,
e.line_num,
e.vat_code,
e.code,
e.make,
e.model,
e.price,
e.discount,
e.cover,
e.warranty,
p.make,
p.model,
p.weight,
p.width,
p.depth,
p.height,
p.cost,
p.cover_value_each,
p.web_model,
k.stock - k.repair
FROM
1_trans_history_entries e
LEFT JOIN
1_products p ON p.id = e.fk_prod_pack_id
AND LEFT(code, 1) <> 'P'
LEFT JOIN
1_stock k ON k.fk_products_id = p.id AND k.branch = b
WHERE
(e.code IN ('MiscShip' , 'Collect')
OR (ASCII(e.code) > 47
AND ASCII(e.code) < 58))
AND FIND_IN_SET(e.fk_trans_history_id, tid)
ORDER BY e.id;
END
最佳答案
将要匹配的 ID 放入临时表中,并与该表连接。
CREATE TEMPORARY TABLE temp_trans_hist_ent_items_sel_ids (
id
);
INSERT INTO temp_trans_hist_ent_items_sel_ids VALUES (1), (10), ...;
那么程序中的查询就可以使用:
JOIN temp_trans_hist_ent_items_sel_ids AS temp ON e.fk_trans_history_id = temp.id
关于mysql - FIND_IN_SET 慢,无法使用 IN(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28384327/