如何优化SQL查询性能

问题描述 投票:0回答:1

introduction

我在做一个查询多次(在给定的位置按“服务”一个查询)。查询,性能测试结果和数据集的大小如下。请给我如何来优化性能的建议。谢谢

size of dataset

  • 服务表:8个1000行
  • how_rates_policies:0.2万行
  • how_rates:170万行

performance results

Botswana

  • 服务数量:4
  • 延迟:7秒
  • 每个服务延迟:2s左右/服务

Cape Town CBD

  • 服务号码:34
  • 延迟:4.5min
  • 每个服务延时:约7S /服务

query

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 ;
sql performance hex
1个回答
0
投票

问题是它不是运行查询的时候,我们猜测它是(博茨瓦纳:4次;和开普敦CBD:34倍)量它实际上是运行了很多次

© www.soinside.com 2019 - 2024. All rights reserved.