Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Query using "or" statement versus Query Using "Union All". Logically the same - using different Indexes

$
0
0
Trying to understand why these two queries which are logically the same do not use the same indexes.
QUERY #1

SELECT TOP 400
        *
FROM    AuditLog WITH ( NOLOCK )
WHERE   CustomerID = 2379
        AND PracticeID = 55
        AND ( ( AuditTypeID = 4
                AND EntityTypeID = 1022638
              )
              OR ( AuditTypeID = 6
                   AND SubEntityTypeID = 1022638
                 )
            )
ORDER BY CreatedDate DESC 



QUERY #2


SELECT TOP 400 *
FROM (

SELECT TOP 400
        *
FROM    AuditLog WITH ( NOLOCK )
WHERE   CustomerID = 2379
        AND PracticeID = 55
        AND  ( AuditTypeID = 4
                AND EntityTypeID = 1022638
              )
ORDER BY CreatedDate

UNION ALL

SELECT TOP 400
        *
FROM    AuditLog WITH ( NOLOCK )
WHERE   CustomerID = 2379
        AND PracticeID = 55
		AND  ( AuditTypeID = 6
                   AND SubEntityTypeID = 1022638
                 )
ORDER BY CreatedDate)sub
ORDER BY sub.CreatedDate


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>