I'm getting significantly different performance from a query that uses parameters vs the same query where the same values are hardcoded in the where clause.
It comes down to 2 child tasks in the execution plan. The hardcoded query can perform an index seek but the parameterised query does an index scan. Since the table contains a million rows the difference is massive.
The query is pretty ugly...
DECLARE @MethodOfDelivery nvarchar(3) = 'AAA'
DECLARE @OutwardPostcode nvarchar(4) = 'BBB'
DECLARE @InwardPostcode nvarchar(3) = 'CCC'
SELECT TOP 1
slr.*
FROM
[dbo].[SortLevelRecord](NOLOCK) slr
INNER JOIN dbo.RoutingDataVersion(NOLOCK) v ON v.Version = slr.Version
INNER JOIN dbo.Routing(NOLOCK) psr ON
(
slr.SortLevelKey = psr.Day1SortLevelKey OR
slr.SortLevelKey = psr.Day2SortLevelkey OR
slr.SortLevelKey = psr.Day3SortLevelkey OR
slr.SortLevelKey = psr.Day4SortLevelkey OR
slr.SortLevelKey = psr.Day5SortLevelkey OR
slr.SortLevelKey = psr.Day6SortLevelkey OR
slr.SortLevelKey = psr.Day7SortLevelkey OR
slr.SortLevelKey = psr.Day8SortLevelkey OR
slr.SortLevelKey = psr.Day9SortLevelkey OR
slr.SortLevelKey = psr.Day10SortLevelkey OR
slr.SortLevelKey = psr.Day11SortLevelkey OR
slr.SortLevelKey = psr.Day12SortLevelkey OR
slr.SortLevelKey = psr.Day13SortLevelkey OR
slr.SortLevelKey = psr.Day14SortLevelkey
) AND
psr.Version = v.Version AND
psr.MethodOfDelivery = slr.MethodOfDelivery AND
psr.OutwardPostcode = @OutwardPostcode AND
(
psr.InwardPostcode = @InwardPostcode OR
psr.InwardPostcode = SUBSTRING(@InwardPostcode, 1, 1) OR
psr.InwardPostcode = ''
)
WHERE
slr.MethodOfDelivery = @MethodOfDelivery
ORDER BY
LEN(psr.InwardPostcode) DESC
The index scan/seek is on the Routing
table.
Something else that seems a bit strange is that the parameterised query uses Parallelism to gather streams but there are no parallel operations, only 1.
This is on Azure SQL server. I'm not sure what version that correlates to.
varchar
ornvarchar
?nvarchar
parameters used againstvarchar
columns will not use indexes efficiently with a legacy SQL collation. Upload your plans and add the link to your question for more specifics as to the cause.