Hi Geeks,

Merge interval operator used to remove duplicated predicates in a query. It also helps to find possible overlapping intervals which in turn helps to optimize filters to avoid scanning same data more than once.

Let us start with following query then jump into the subject.



From the above plan, clustered index seek performed on ProductID to fetch data specified in IN clause then query optimiser used StreamAggregate to compute sum. As of now we do not see Merge Interval in the query plan. Let me modify our query little bit as following.



Note: Query plan image is broken into two parts in order to fit into the window.

I’ve used variables instead of constant values and for that query optimizer created a different plan altogether. The reason behind this is, SQL Server does not know the values of the constants and in case these values turns out to be duplicates it will read same data twice and for the same reason it uses Merge intervals to remove duplicate occurrences.

We are going to explore more on this tomorrow, stay tuned.

Happy learning!



Like us on FaceBook | Join the fastest growing SQL Server group on FaceBook | Follow me on Twitter | Follow me on FaceBook