我在做一个查询多次(在给定的位置按“服务”一个查询)。查询,性能测试结果和数据集的大小如下。请给我如何来优化性能的建议。谢谢
DELIMITER $$
CREATE DEFINER= [i removed the db user name here to censor it] PROCEDURE `spx_GetRatesForProduct`(productId INT, priceCode VARCHAR(2), inDate DATETIME, outDate DATETIME)
BEGIN
DECLARE endRateDate DATETIME;
SET endRateDate := (SELECT MAX(date_to)
FROM how_rates_policies hrp
WHERE hrp.product_id = productId and hrp.price_code = priceCode and hrp.date_to >= outDate);
-- SELECT endRateDate = IFNULL(endRateDate, '19700101');
IF endRateDate = '19700101' THEN
SELECT * FROM how_rates_policies hrp INNER JOIN how_rates hr ON hrp.id = hr.what_rates_policies_id
WHERE hrp.product_id = productId
AND hrp.price_code = priceCode
AND hrp.date_from <= inDate
AND hr.rate_category = 'VAL'
ORDER BY hrp.date_to;
ELSE
SELECT * FROM how_rates_policies hrp
INNER JOIN how_rates hr
ON hrp.id = hr.what_rates_policies_id
WHERE hrp.product_id = productId
AND hrp.price_code = priceCode
AND hrp.date_from <= outDate
AND hrp.date_to >= inDate
AND hrp.date_to <= endRateDate
AND hr.rate_category = 'VAL'
ORDER BY hrp.date_to;
END IF;
END$$
DELIMITER ;
问题是它不是运行查询的时候,我们猜测它是(博茨瓦纳:4次;和开普敦CBD:34倍)量它实际上是运行了很多次