由于查询性能差,MySQL Union替代方案

f8rj6qna  于 2022-10-22  发布在  Mysql
关注(0)|答案(1)|浏览(221)

我的数据库结构如下:


小时
以及正在查询数据以尝试找到上下最近匹配率的存储过程。例如:
对于价格为“600”的产品“Apple”,年龄=6,距离=103应返回费率Id 1和费率Id 2(两行),因为这些费率都在指定的参数范围内。例如,在没有精确匹配的情况下,速率越低,速率越高。

select t.rateId as id, t.Rate as rate, t.Age as age, t.Distance as Distance from (
(select t.*
    from rates t
    where Age <= (in_age + 1) and Distance <= in_Distance and
    in_Price > MinPrice and in_Price <= MaxPrice
    and Product=in_Product and Type=in_Type
    order by Age desc, Distance desc limit 1
) union
(select t.*
    from rates t
    where Age >= (in_age + 1) and Distance <= in_Distance and
    in_Price > MinPrice and in_Price <= MaxPrice
    and Product=in_Product and Type=in_Type
    order by Age asc, Distance desc limit 1
) union
(select t.*
    from rates t
    where Age <= (in_age + 1) and Distance >= in_Distance and
    in_Price > MinPrice and in_Price <= MaxPrice
    and Product=in_Product and Type=in_Type
    order by Age desc, Distance asc  limit 1
) union
(select t.*
    from rates t
    where Age >= (in_age + 1) and Distance >= in_Distance and
    in_Price > MinPrice and in_Price <= MaxPrice
    and Product=in_Product and Type=in_Type
    order by Age asc, Distance asc  limit 1
)) t;

上述语句中的任何一个单独选择都需要100秒才能运行。但是,当它们单独联合在一起时,整个查询现在需要约1秒。
请注意,根据输入参数,结果可以是1、2或4个速率行。1结果将是与指定的年龄和距离完全匹配的地方。2表示年龄或距离中的一个精确匹配,如果年龄或距离都没有精确匹配,则最多可以得到4个结果。
除了UNION之外,我还尝试了许多不同的方法,并获得了相当的性能。例如,我尝试过:

...where rateId in (
    ...QRY 1...
) or rateId in (
    ...QRY 2...
) or rateId in (
    ...QRY 3...
) or rateId in (
    ...QRY 4...
);

然后分别选择一个临时表,然后返回临时表的内容。然而,在所有情况下,性能大致相同~如果单独运行,则4个查询中的每一个都需要1秒,而不是约0.001秒!
关于如何提高绩效或我做错了什么,有什么想法吗?
谢谢贾布拉尔
样本数据:

rateId,Age,Distance,Rate,Product,MinPrice,MaxPrice
269369,95,500,1000,Apple,50,110
269385,95,1000,1058,Apple,50,110
269401,95,1500,1147,Apple,50,110
269417,95,2000,1223,Apple,50,110
269433,95,2500,1225,Apple,50,110
269449,95,3000,1294,Apple,50,110
269465,95,3500,1368,Apple,50,110
269481,95,4000,1436,Apple,50,110
269497,95,4500,1454,Apple,50,110
269513,95,5000,1477,Apple,50,110
269529,95,5500,1559,Apple,50,110
269545,95,6000,1566,Apple,50,110
269561,95,6500,1584,Apple,50,110
269577,95,7000,1614,Apple,50,110
269593,100,500,1637,Apple,50,110
269609,100,1000,1730,Apple,50,110
269625,100,1500,1775,Apple,50,110
269641,100,2000,1794,Apple,50,110
269657,100,2500,1830,Apple,50,110
269673,100,3000,1903,Apple,50,110
269689,100,3500,1912,Apple,50,110
269705,100,4000,1931,Apple,50,110
269721,100,4500,1947,Apple,50,110
269737,100,5000,2047,Apple,50,110
269753,100,5500,2051,Apple,50,110
269769,100,6000,2094,Apple,50,110
269785,100,6500,2152,Apple,50,110
269801,100,7000,2189,Apple,50,110
269817,125,500,2280,Apple,50,110
269833,125,1000,2358,Apple,50,110
269849,125,1500,2412,Apple,50,110
269865,125,2000,2424,Apple,50,110
269881,125,2500,2475,Apple,50,110
269897,125,3000,2485,Apple,50,110
269913,125,3500,2564,Apple,50,110
269929,125,4000,2641,Apple,50,110
269945,125,4500,2735,Apple,50,110
269961,125,5000,2772,Apple,50,110
269977,125,5500,2826,Apple,50,110
269993,125,6000,2837,Apple,50,110
270009,125,6500,2926,Apple,50,110
270025,125,7000,2930,Apple,50,110
270041,150,500,2955,Apple,50,110
270057,150,1000,2973,Apple,50,110
270073,150,1500,3064,Apple,50,110
270089,150,2000,3108,Apple,50,110
270105,150,2500,3180,Apple,50,110
270121,150,3000,3219,Apple,50,110
270137,150,3500,3315,Apple,50,110
270153,150,4000,3389,Apple,50,110
270169,150,4500,3395,Apple,50,110
270185,150,5000,3417,Apple,50,110
270201,150,5500,3442,Apple,50,110
270217,150,6000,3451,Apple,50,110
270233,150,6500,3493,Apple,50,110
270249,150,7000,3519,Apple,50,110
270265,175,500,3568,Apple,50,110
270281,175,1000,3614,Apple,50,110
270297,175,1500,3708,Apple,50,110
270313,175,2000,3763,Apple,50,110
270329,175,2500,3845,Apple,50,110
270345,175,3000,3910,Apple,50,110
270361,175,3500,3934,Apple,50,110
270377,175,4000,3950,Apple,50,110
270393,175,4500,4009,Apple,50,110
270409,175,5000,4077,Apple,50,110
270425,175,5500,4108,Apple,50,110
270441,175,6000,4158,Apple,50,110
270457,175,6500,4239,Apple,50,110
270473,175,7000,4339,Apple,50,110
270489,200,500,4369,Apple,50,110
270505,200,1000,4438,Apple,50,110
270521,200,1500,4510,Apple,50,110
270537,200,2000,4533,Apple,50,110
270553,200,2500,4622,Apple,50,110
270569,200,3000,4690,Apple,50,110
270585,200,3500,4786,Apple,50,110
270601,200,4000,4868,Apple,50,110
270617,200,4500,4955,Apple,50,110
270633,200,5000,5009,Apple,50,110
270649,200,5500,5080,Apple,50,110
270665,200,6000,5151,Apple,50,110
270681,200,6500,5198,Apple,50,110
270697,200,7000,5250,Apple,50,110
270713,225,500,5304,Apple,50,110
270729,225,1000,5349,Apple,50,110
270745,225,1500,5386,Apple,50,110
270761,225,2000,5430,Apple,50,110
270777,225,2500,5499,Apple,50,110
270793,225,3000,5500,Apple,50,110
270809,225,3500,5528,Apple,50,110
270825,225,4000,5582,Apple,50,110
270841,225,4500,5610,Apple,50,110
270857,225,5000,5690,Apple,50,110
270873,225,5500,5736,Apple,50,110
270889,225,6000,5748,Apple,50,110
270905,225,6500,5820,Apple,50,110
270921,225,7000,5868,Apple,50,110
270937,250,500,5911,Apple,50,110
270953,250,1000,5998,Apple,50,110

使用上述数据对预期结果进行抽样,如下所示:
年龄=95,距离=500,产品“Apple”的价格为“75”->单个结果:RateId 269369
年龄=95,距离=750,对于价格为“75”的产品“Apple”->两个结果:RateId 269369和269385(这基本上给出了最近的较低距离和最近的较高距离的费率,因为不存在精确距离750的费率。
年龄=97,距离=750,价格为“75”的产品“Apple”->四个结果:RateId 269369、269385、269593、269609(与上面类似,但现在也给出了年龄的下一个最接近的下限和上限,并且没有与年龄97完全匹配。

sdnqo3pr

sdnqo3pr1#

首先,关注一个“指标”排序公式。
注意:布尔表达式的计算结果为1(表示true)或0(表示false)。

metric = (age = in_age)   -- increase metric if exact
       + (age <= in_age+1)  -- increase again (I don't grok <=)
       + 2*(distance <= in_distance) -- close enuf counts as 2
       + ...

然后做一些类似的事情

SELECT ...,
       (...) AS metric
    FROM ...
    ORDER BY metric
    LIMIT 4;

这可能是您的4倍,因为它只有1个查询。但它仍然很慢,因为它需要检查每一行。(metrid的复杂性不是很大的性能因素。)

相关问题