Blog Post

Parameterization- ?? ?? ?????? ???? ??? ????? ?????

,

post thumbnail

?? ????, ??????? ?? ??? ???? ????? ????? ????? ?? ? Database-?? ???????. ????? ??????? ?? ?????? ????? ????? ???????????? ???? ????? ??????????, ???? ????? ?????? ??????? ??????? ??????. ???? ??????? ????? ?? ???????? ??????? ? Query Plan Cache , ????? ??? ????????? ??????: 

Query Text           

Execution Count           

SELECT ClientId
FROM clients
WHERE (StateId = 2) and (TransactionId = 5)

1           

SELECT ClientId
FROM clients
WHERE (StateId = 2) and (TransactionId = 6)

1           

SELECT ClientId
FROM clients
WHERE (StateId = 2) and (TransactionId = 7)

1           

SELECT ClientId
FROM clients
WHERE (StateId = 2) and (TransactionId = 8)

1           

    

??? ????? ??????? ????.  ???? ??????? ????? ????? ???? ?? ????? ?????.  ??? ????? ????? ??????, ?- Query Optimizer  ?? ???? ?????? ????? ???? ?????? ???? ????? ?? ?????? ????.  ????, ????? ????? ??? ? Query Optimizer  ???? ?? ?????? ????????, ???? ??????? ????? ?????? ???? ???? ????? ???? ?? ??? ????. ????? ?? ??????  Parameterization, ??? ?????? ??????? ????????.  ????? ??????? ??????? ?????? ????? ??:           

DECLARE @a, @b int;
SELECT ClientId
FROM clients
WHERE (StateId = @a) and (TransactionId = @b);

  ?? ????? ?????? ????, ??? ?? ??? ???? :) , ?????? ?????:           

  1. ???? ??? ????? ??? ???????: ?????? ???? ??? ?????? ????? ?? ???  Parameterization.
  2. ?????? Parameterization ? Database – ??????? ???????? ?? ?? ??? ?????????.
  3. ?????, ????? ????? ?????? ?????? ?? ????? ??????????? ??????? ?????? ??? ???? ??? ???? ??? ?????? ? Database.     

Parameterization – ??? ????           

???? ?????, ???? ?? ?????????? ?? ????? ?????? ?????? ?? ?????? ??????, ??? ???? ??????? ????? ???? ???, ????? ?????? ??? ????? ?????? ???? ???? ?? ?????? ?????. ????, ????? ????? ?? ??????? ?? ?????????? ???? ????? ?????? ?????? ???. ?????? ??????? ????? ?? ????? Execution Plan. ??? ???? ?- Plan ????? ???? ?????? ???? ????? ??? ????? ???? ????? ???? ?? ?????? ?? ???????           

?????, ?????? ????? ??? ?- Execution Plan ????????? ????? ?? ?? ?????? ????????, ???? ???? ???????:           

  1. ???? ?? ???? ??? ????? ?? ???????, ?- Execution Plan  ????????? ????? ?- Indexed View ??? ??? ?? ???? ?? ??? ?????? ????? ??? ?????? ?? ???????.
  2. ?????? ?? ???? ???? Partitions ?? ????? ????? Execution Plan ???? ???? ???? ????? ??????? ? Partition?? ?????.
  3. ?????, ????? ??? ?? ??? ????? ?? ??????? ????? ???? ???? ?? ?????? ??-Query Optimizer ????? ??? ?????? ? Index, ????? ??? ??? ????? ???? ???? ?? ?????? ??? ????? ???? ?????? ? Index.
  4. ????? ??? ???? ?? Filtered Index ? Execution Plan ???? ????? ???? ???? ?? ??? ??????.

????? Parameterization ? Database           

??? ??????, ?? ???? ???? ???? ?????????? ?? ???????? ??? ?? ?????? ??? ???? ?? ?????????? ??? ???? ????? ??? ?Database . ?? ????? ??? ???? ???? ??????? ????? ???????????. ??? ???????? ??:           

  1. Simple Parameterization – ???? ?????? ???????????? ??? ????? Database. ?????? ??? ???? ?????????? ???????? ?? ??? ????? ?????? ? Execution Plan ?? ????? ?????? ????????????. ?????, ?????? ??? ??????? ?????????? ??:
  2. Forced Parameterization – ??? ????? ?????, ?????? ??? ??????? ?????????? ??? Simple Parameterization ??????? ????, ?????? ??? ??????? ??????? ????? ???. ??? ????? ?????? ?? Forced Parameterization ??? ???? ???? ???? ????? ???? ???? ???????, ?? ????? ??????? ??? ???? ??????? ?????? Plan ????? ???? ??? ???????????. ??? ???? ???? ????? ?????? ???????????? ? Simple ? Forced ??? ????? ?? ??????. ?? ? Forced Parameterization ?? ????? ??? ??????????? ?? ???????, ?? ?????? ?? ????? ??? ???? ????? ?: http://technet.microsoft.com/en-us/library/ms175037.aspx
  • Single Table – No JOINs
  • No IN clause
  • No UNION
  • No SELECT INTO
  • No Query Hints
  • No DISTINCT or TOP
  • No full-text, linked servers or table variables
  • No sub-queries
  • No GROUP BY
  • No <> in WHERE clause
  • No functions
  • No DELETE or UPDATE with FROM clause
  • Parameter values can’t affect plan

????? ???? ????? ?????? Simple Parameterization ?????? ????? ????? ???????? ??????? ?????? ????? ?????????? (??? ?????? ?????).  ?????? ???? Performance Counters ??? ?????? ????? ?? ???? ??????????? ?? ???????. ??????, ???? ???? ??? ? counters ???? ???? ?? ???? ???????? ??? ???? Parameterization.           

SQLServer : SQL Statistics: Auto-Param Attempts/Sec

SQLServer : SQL Statistics: Batch Request / Sec          

????? ????? ????? ?? ???????? ? Query Plan Cache ?????? ??? ?? ???? ?????? ?????????? ???? ????.         

????? Parameterization ??????? ????? (?? ????? ??????? ???? ??? ?????? ? database)          

1.  ?????????? ??? ????? ? Plan Guide:

???? ?????? ????? ??????????  (simple \ forced) ?????? ???? ?????? ??????, ??? ??? ?????? ? Database. ??? ?? ?? ??? ????? ?????? ????. ?????, ????? ?- Query Template ?? ???????, ??????:         

DECLARE @statement nvarchar(max);
DECLARE @parameters nvarchar(max);
EXEC sp_get_query_template
N'SELECT ClientId FROM clients WHERE (StateId = 2) and (TransactionId = 8)',
               @statement OUTPUT,
                @parameters OUTPUT  

    

(????? ?????? ????? ?? ??????? ?????? ?????? ?? ??????? ????)

???? ???, ????? ? Plan Guide:   

    

    

EXEC sp_create_plan_guide N'ForceParameterizationPlanGuide'
    @statement
    N'TEMPLATE',
    NULL,
    @parameters
    N'OPTION(PARAMETERIZATION FORCED)';  

????: ???? ????? ???? ?? ??????   PARAMETERIZATION SIMPLE???? ??????? ????.    

2.  ?????????? ???  ADO –

???????? ???????, ?? ???? ?????? ??? ????? ?? ???????? ??????? ???? ?????, ?? ?????? ?????? ???? ??????? ??????? ?????? ????????.

???? ????? ????? ? c# (????? ????? ? Syntax ???? ????):   

SqlCommand command = new SqlCommand(String.Empty, connection);  
// Instead of id = 4, use parameterization: id = @id  
string query = "SELECT Name, DateTaken FROM Test Where id = @id;";
command.CommandText = query
// Enter value for parameter @id  
command.Parameters.Add("@id", SqlDbType.Int).Value = 4;
command.ExecuteReader();

         

       

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating