按组性能问题的MS Access 2010 SQL Top N查询

时间:2014-03-28 16:10:06

标签: sql performance ms-access-2010

我有2个表,一个有项目量(MASE_Table),另一个有项目属性(SKU)。项目属性可以是Planner或ABCByPick或XYZ或其他属性。

通过查询,我想通过属性ABCByPick获取此度量的前10项(AvgOfScaledError)。

我想让查询尽可能通用,以便我可以复制它并用属性规划器替换ABCByPick

我在这里找到了我尝试过的提示。只要MASE_Table中的记录数少于1000,我就可以执行查询。如果我想在MASE_Table中使用8000条记录运行它,则查询不再产生结果(没有错误消息但是超时)。

以下是我对ABCByPick前10名的查询:

SELECT s2.ABCByPick
       ,m2.DmdUnit
       ,ROUND(m2.AvgOfScaledError, 2) AS MASE
    FROM MASE_Table AS m2
    INNER JOIN SKU AS s2
        ON ( m2.Loc = s2.Loc )
           AND ( m2.DmdUnit = s2.Item )
    WHERE m2.AvgOfScaledError IN ( SELECT TOP 10 m1.AvgOfScaledError
                                    FROM (
                                           SELECT m1.Loc
                                               ,m1.DmdUnit
                                               ,s1.ABCByPick
                                               ,m1.AvgOfScaledError
                                            FROM MASE_Table AS m1
                                            INNER JOIN SKU AS s1
                                                ON m1.Loc = s1.Loc
                                                   AND m1.DmdUnit = s1.Item
                                         )
                                    WHERE s1.ABCByPick = s2.ABCByPick
                                    ORDER BY m1.AvgOfScaledError DESC )
    ORDER BY s2.ABCByPick
       ,m2.AvgOfScaledError DESC;

MASE_Table上的主键是Loc和DmdUnit
SKU上的主键是Loc和Item

1 个答案:

答案 0 :(得分:0)

这应该对某些人有所帮助:

SELECT s2.ABCByPick,
    m2.DmdUnit,
    ROUND(m2.AvgOfScaledError, 2) AS MASE
FROM MASE_Table AS m2
INNER JOIN SKU AS s2 ON m2.Loc = s2.Loc
    AND m2.DmdUnit = s2.Item
WHERE m2.AvgOfScaledError IN (
        SELECT TOP 10 m1.AvgOfScaledError
        FROM MASE_Table AS m1
        INNER JOIN SKU AS s1 ON m1.Loc = s1.Loc
            AND m1.DmdUnit = s1.Item
        WHERE s1.ABCByPick = s2.ABCByPick
        ORDER BY m1.AvgOfScaledError DESC
        )
ORDER BY s2.ABCByPick,
    m2.AvgOfScaledError DESC;