MS Access查询性能问题

时间:2016-01-25 15:25:34

标签: sql ms-access ms-access-2010

以下查询有时会起作用。

运行查询时常见的问题是,虽然确实显示了所有其他数据,但并未显示超过180岁的记录的记录数。

l_section& dim_performance_score是SharePoint 2010列表。

我想要一份报告,显示小于90天的记录,> = 90和< 180天,并且> = 180天。

如果我创建五个不同的存储查询并将其加入,则查询将始终如一地运行。但是,当我将所有SQL合并到一个存储的查询中时,事情就变得松懈了。如果我尝试在设计窗口中查看合并查询,则无法承受性能问题,MS Access崩溃。这是JET的已知问题吗?

维持5个或6个不同的查询以获得一个答案似乎效率低下。

我正在做的是获得所需数据的最佳方法吗?

PARAMETERS [compare date] DateTime;

SELECT
    l_section.section, 
    [compare date] AS [As of Date],
    total_count.[Total Records],
    IIf([less_than_90].[<90 Days] Is Null,0,[less_than_90].[<90 Days]) AS [<90 Days],
    IIf([greater_than_90].[>=90 & <180Days] Is Null,0,[greater_than_90].[>=90 & <180Days]) AS [>=90 & <180Days],
    IIf([greater_than_180].[>=180 Days] Is Null,0,[greater_than_180].[>=180 Days]) AS [>=180 Days]

FROM 
    (
        (
            (
                l_section 
                LEFT JOIN
                    (
                        SELECT
                            since_modified.section,
                            Count(since_modified.section) AS [>=180 Days]
                        FROM 
                            (
                                SELECT 
                                    l_section.section, 
                                    IIf(dim_performance_score.[Modified] Is Null,0,DateDiff("d",dim_performance_score.[Modified],[compare date])) AS days_since_update
                                FROM
                                    l_section
                                LEFT JOIN
                                    dim_performance_score
                                ON
                                    l_section.section = dim_performance_score.section
                            ) as since_modified
                        WHERE 
                            (((since_modified.days_since_update)>=180))
                        GROUP BY 
                            since_modified.section
                    ) as greater_than_180 
                ON
                    l_section.section = greater_than_180.section
            )
            LEFT JOIN
                (
                    SELECT 
                        since_modified.section,
                        Count(since_modified.section) AS [<90 Days]
                    FROM
                        (
                            SELECT 
                                l_section.section, 
                                IIf(dim_performance_score.[Modified] Is Null,0,DateDiff("d",dim_performance_score.[Modified],[compare date])) AS days_since_update
                            FROM
                                l_section
                            LEFT JOIN
                                dim_performance_score
                            ON
                                l_section.section = dim_performance_score.section
                        ) as since_modified
                    WHERE 
                        (((since_modified.days_since_update)<90))
                    GROUP BY
                        since_modified.section
                ) as less_than_90
            ON 
                l_section.section = less_than_90.section
        )
        LEFT JOIN
            (
                SELECT
                    since_modified.section,
                    Count(since_modified.section) AS [>=90 & <180Days]
                FROM 
                    (
                        SELECT 
                            l_section.section, 
                            IIf(dim_performance_score.[Modified] Is Null,0,DateDiff("d",dim_performance_score.[Modified],[compare date])) AS days_since_update
                        FROM
                            l_section
                        LEFT JOIN
                            dim_performance_score
                        ON
                            l_section.section = dim_performance_score.section
                    ) as since_modified                 
                WHERE 
                    (((since_modified.days_since_update)>=90 
                    And
                    (since_modified.days_since_update)<180))
                GROUP BY 
                    since_modified.section
            ) as greater_than_90 
        ON
            l_section.section = greater_than_90.section
    )
    LEFT JOIN 
        (
            SELECT
                l_section.section,
                Count(IIf([section] Is Null,0,[section])) AS [Total Records]
            FROM
                l_section
            LEFT JOIN
                dim_performance_score
            ON
                l_section.section = dim_performance_score.section
            GROUP BY l_section.section
        ) as total_count 
    ON
        l_section.section = total_count.section
ORDER BY l_section.section;

2 个答案:

答案 0 :(得分:0)

三重嵌套联接?这不可能是正确的。看起来每个连接都是(或应该)在同一级别上。

PARAMETERS [compare date] DateTime;

SELECT
    l_section.section, 
    [compare date] AS [As of Date],
    total_count.[Total Records],
    IIf([less_than_90].[<90 Days] Is Null,0,[less_than_90].[<90 Days]) AS [<90 Days],
    IIf([greater_than_90].[>=90 & <180Days] Is Null,0,[greater_than_90].[>=90 & <180Days]) AS [>=90 & <180Days],
    IIf([greater_than_180].[>=180 Days] Is Null,0,[greater_than_180].[>=180 Days]) AS [>=180 Days]

FROM l_section 
LEFT JOIN
    (
        SELECT
            since_modified.section,
            Count(since_modified.section) AS [>=180 Days]
        FROM 
            (
                SELECT 
                    l_section.section, 
                    IIf(dim_performance_score.[Modified] Is Null,0,DateDiff("d",dim_performance_score.[Modified],[compare date])) AS days_since_update
                FROM
                    l_section
                LEFT JOIN
                    dim_performance_score
                ON
                    l_section.section = dim_performance_score.section
            ) as since_modified
        WHERE 
            (((since_modified.days_since_update)>=180))
        GROUP BY 
            since_modified.section
    ) as greater_than_180 
ON
    l_section.section = greater_than_180.section

LEFT JOIN
    (
        SELECT 
            since_modified.section,
            Count(since_modified.section) AS [<90 Days]
        FROM
            (
                SELECT 
                    l_section.section, 
                    IIf(dim_performance_score.[Modified] Is Null,0,DateDiff("d",dim_performance_score.[Modified],[compare date])) AS days_since_update
                FROM
                    l_section
                LEFT JOIN
                    dim_performance_score
                ON
                    l_section.section = dim_performance_score.section
            ) as since_modified
        WHERE 
            (((since_modified.days_since_update)<90))
        GROUP BY
            since_modified.section
    ) as less_than_90
ON 
    l_section.section = less_than_90.section

LEFT JOIN
    (
        SELECT
            since_modified.section,
            Count(since_modified.section) AS [>=90 & <180Days]
        FROM 
            (
                SELECT 
                    l_section.section, 
                    IIf(dim_performance_score.[Modified] Is Null,0,DateDiff("d",dim_performance_score.[Modified],[compare date])) AS days_since_update
                FROM
                    l_section
                LEFT JOIN
                    dim_performance_score
                ON
                    l_section.section = dim_performance_score.section
            ) as since_modified                 
        WHERE 
            (((since_modified.days_since_update)>=90 
            And
            (since_modified.days_since_update)<180))
        GROUP BY 
            since_modified.section
    ) as greater_than_90 
ON
    l_section.section = greater_than_90.section

LEFT JOIN 
    (
        SELECT
            l_section.section,
            Count(IIf([section] Is Null,0,[section])) AS [Total Records]
        FROM
            l_section
        LEFT JOIN
            dim_performance_score
        ON
            l_section.section = dim_performance_score.section
        GROUP BY l_section.section
    ) as total_count 
ON
    l_section.section = total_count.section

ORDER BY l_section.section;

答案 1 :(得分:0)

嗯,我想出了一个不同的方式来获得我想要的东西,它更简单。我发布作为我自己的答案,以显示之前和之后。

SELECT

        section_last_modified.section,
        Count(section_last_modified.section) AS [Total Records],
        Sum(IIf([days_since_modified]<90,1,0)) AS [< 90 days],
        Sum(IIf([days_since_modified] Between 90 And 180,1,0)) AS [>=90 and <180 days],
        Sum(IIf([days_since_modified]>=180,1,0)) AS [>=180 days]

FROM section_last_modified

GROUP BY section_last_modified.section;



-----section_last_modified-------

PARAMETERS [Compare Date] DateTime;
SELECT
        dim_performance_score.section,
        DateDiff("d",dim_performance_score.[Modified],[Compare Date]) AS days_since_modified
FROM
        dim_performance_score
WHERE
        (((dim_performance_score.[Content Type])="stacker maintenance"))
ORDER BY
        dim_performance_score.section;