CREATE PROCEDURE GetCustomerOrders
@customerid NCHAR(5)
AS
BEGIN
SELECT orderid, customerid, orderdate, shippeddate
FROM Orders
WHERE customerid = @customerid;
END
CREATE PROCEDURE GetCustomerOrders
@customerid NCHAR(5)
AS
BEGIN
DECLARE @local_customerid NCHAR(5);
SET @local_customerid = @customerid;
SELECT orderid, customerid, orderdate, shippeddate
FROM Orders
WHERE customerid = @local_customerid;
END
EXEC GetCustomerOrders @customerid = N'CACYK' WITH RECOMPILE;
CREATE PROCEDURE GetCustomerOrders
@customerid NCHAR(5)
AS
BEGIN
SELECT orderid, customerid, orderdate, shippeddate
FROM Orders
WHERE customerid = @customerid
OPTION (RECOMPILE);
END
CREATE PROCEDURE GetCustomerOrders
@customerid NCHAR(5)
AS
BEGIN
SELECT orderid, customerid, orderdate, shippeddate
FROM Orders
WHERE customerid = @customerid
OPTION (OPTIMIZE FOR (@customerid = N'CACYK'));
END
EXEC sp_create_plan_guide
@name = N'SolveParameterSniffing',
@stmt = N'SELECT orderid, customerid, orderdate, shippeddate
FROM Orders
WHERE customerid = @customerid',
@type = N'OBJECT',
@module_or_batch = N'GetCustomerOrders',
@params = NULL,
@hints = N'OPTION (RECOMPILE)';
Labels: performance tuning, t-sql programming