We\'re using SQL Server 2005 to track a fair amount of constantly incoming data (5-15 updates per second). We noticed after it has been in production for a couple months that on
There is an isse with the SQL Server optimizer when using the DISTINCT
keyword.
The solution was to force it to keep the same query plan by breaking out the distinct query separately.
So we too queries such as:
SELECT DISTINCT [typeName] FROM [types] WITH (nolock);
and break it up into the following
SELECT typeName INTO #tempTable1 FROM types WITH (NOLOCK)
SELECT DISTINCT typeName FROM #tempTable1
Another way to get around it is to use a GROUP BY
, which gets a different optimization plan.