任何人都可以帮我优化此查询。我无法执行它,因为phpMyAdmin连接超时,我无权更改超时参数。
这是一个查询:
INSERT INTO `goods` (`goods_id`, `price`, `name`)
SELECT `sales_goods`.`goods_id`, `sales_goods`.`price`, `sales_goods`.`goods_id`
FROM `sales_goods`
WHERE `sales_goods`.`sales_goods_id`
IN (
SELECT MAX(`sales_goods`.`sales_goods_id`)
FROM `sales_goods`
WHERE `sales_goods`.`sales_goods_id`
IN (
SELECT `sales_goods`.`sales_goods_id` FROM `sales_goods`
WHERE `sales_goods`.`goods_id` NOT IN (SELECT `goods`.`goods_id` FROM `goods`)
)
GROUP BY `sales_goods`.`goods_id`
)
答案 0 :(得分:0)
除了确保您的表格属性已编入索引外,您可以将IN
子查询替换为INNER JOIN
,将NOT IN
子查询替换为LEFT JOIN/IS NULL
,这样可以提高性能:< / p>
INSERT INTO `goods` (`goods_id`, `price`, `name`)
SELECT `sales_goods`.`goods_id`,
`sales_goods`.`price`,
`sales_goods`.`goods_id`
FROM `sales_goods`
INNER JOIN
( SELECT MAX(`sales_goods`.`sales_goods_id`)
FROM `sales_goods`
LEFT JOIN `goods`
ON `goods`.`goods_id` = `sales_goods`.`goods_id`
WHERE `goods`.`goods_id` IS NULL
GROUP BY `sales_goods`.`goods_id`
) MaxSalesGoods
ON MaxSalesGoods.sales_goods_id = `sales_goods`.`sales_goods_id`