[Solved] MSAccess 2013: UNION any 2 of 3 tables works, UNION all 3 crashes

How to optimize this SQL query?

In case you have your own slow SQL query, you can optimize it automatically here.

For the query above, the following recommendations will be helpful as part of the SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Avoid Algebric Transformation On Indexable Expressions (modified query below): When algebric modifications are applied to an indexable expression, the database won't be able to utilize the index efficiently for search operations.
  2. Mixed Order By Directions Prevents Index Use (query line: 85): The database will not use a sorting index (if exists) in cases where the query mixes ASC (the default if not specified) and DESC order. To avoid filesort, you may consider using the same order type for all columns. Another option that will allow you to switch one direction to another is to create a new reversed "sort" column (max_sort - sort) and index it instead.
  3. Use UNION ALL instead of UNION (query line: 57): Always use UNION ALL unless you need to eliminate duplicate records. By using UNION ALL, you'll avoid the expensive distinct operation the database applies when using a UNION clause.
The optimized query:
SELECT
        IDAudit,
        TableName,
        dbo_Audit.RowID,
        ChangeDate,
        IDAPSCase 
    FROM
        dbo_APSCEI 
    INNER JOIN
        (
            dbo_Audit 
        INNER JOIN
            dbo_TableType 
                ON dbo_Audit.IDTableType = dbo_TableType.IDTableType
            ) 
                ON dbo_APSCEI.IDAPSCEI = dbo_Audit.RowID 
        WHERE
            (
                (
                    (
                        TableName
                    ) = "APSCEI"
                ) 
                AND (
                    IDAPSCase = 379017
                )
            ) 
        UNION
        SELECT
            IDAudit,
            TableName,
            RowID,
            ChangeDate,
            IDAPSCase 
        FROM
            dbo_APCSUN 
        INNER JOIN
            (
                dbo_Audit 
            INNER JOIN
                dbo_TableType 
                    ON dbo_Audit.IDTableType = dbo_TableType.IDTableType
                ) 
                    ON dbo_APCSUN.IDAPCSUN = dbo_Audit.RowID 
            WHERE
                (
                    (
                        (
                            TableName
                        ) = "APCSUN"
                    ) 
                    AND (
                        IDAPSCase = 379017
                    )
                ) 
            UNION
            SELECT
                IDAudit,
                TableName,
                RowID,
                ChangeDate,
                IDAPSCase 
            FROM
                dbo_APSCAI 
            INNER JOIN
                (
                    dbo_Audit 
                INNER JOIN
                    dbo_TableType 
                        ON dbo_Audit.IDTableType = dbo_TableType.IDTableType
                    ) 
                        ON dbo_APSCAI.IDAPSCAI = dbo_audit.RowID 
                WHERE
                    (
                        (
                            (
                                TableName
                            ) = "APSCAI"
                        ) 
                        AND (
                            IDAPSCase = 379017
                        )
                    ) 
                ORDER BY
                    ChangeDate DESC,
                    IDAPSCase

Related Articles



* original question posted on StackOverflow here.