Hi all,
Considering the following "example" query with a columnstore index:
SELECT ClientId, SUM(OrderAmount)
FROM Order
GROUP BY ClientId
Execution plan shows a nice batch execution mode. When I do this, however:
SELECT Client.CountryId, SUM(OrderAmount)
FROM Order
INNER JOIN Client ON Client.ClientId = Order.ClientId
GROUP BY Client.CountryId
It switches to row execution mode (and is considerably slower). Funnily enough though, if I do:
SELECT Client.CountryId, SUM(OrderAmount)
FROM (SELECT ClientId, SUM(OrderAmount) AS OrderAmount
FROM Order
GROUP BY ClientId) Order
INNER JOIN Client ON Client.ClientId = Order.ClientId
GROUP BY Client.CountryId
I get the same results as the second query, but in batch mode and considerably faster.
Why can't SQL work out it can easily use batch mode for the second query so I don't have to write horrible subqueries for everything I do?
Using SQL Server 2012 RTM.