?? ????, ??????? ?? ??? ???? ????? ????? ????? ?? ? 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);
?? ????? ?????? ????, ??? ?? ??? ???? , ?????? ?????:
- ???? ??? ????? ??? ???????: ?????? ???? ??? ?????? ????? ?? ??? Parameterization.
- ?????? Parameterization ? Database – ??????? ???????? ?? ?? ??? ?????????.
- ?????, ????? ????? ?????? ?????? ?? ????? ??????????? ??????? ?????? ??? ???? ??? ???? ??? ?????? ? Database.
Parameterization – ??? ????
???? ?????, ???? ?? ?????????? ?? ????? ?????? ?????? ?? ?????? ??????, ??? ???? ??????? ????? ???? ???, ????? ?????? ??? ????? ?????? ???? ???? ?? ?????? ?????. ????, ????? ????? ?? ??????? ?? ?????????? ???? ????? ?????? ?????? ???. ?????? ??????? ????? ?? ????? Execution Plan. ??? ???? ?- Plan ????? ???? ?????? ???? ????? ??? ????? ???? ????? ???? ?? ?????? ?? ???????
?????, ?????? ????? ??? ?- Execution Plan ????????? ????? ?? ?? ?????? ????????, ???? ???? ???????:
- ???? ?? ???? ??? ????? ?? ???????, ?- Execution Plan ????????? ????? ?- Indexed View ??? ??? ?? ???? ?? ??? ?????? ????? ??? ?????? ?? ???????.
- ?????? ?? ???? ???? Partitions ?? ????? ????? Execution Plan ???? ???? ???? ????? ??????? ? Partition?? ?????.
- ?????, ????? ??? ?? ??? ????? ?? ??????? ????? ???? ???? ?? ?????? ??-Query Optimizer ????? ??? ?????? ? Index, ????? ??? ??? ????? ???? ???? ?? ?????? ??? ????? ???? ?????? ? Index.
- ????? ??? ???? ?? Filtered Index ? Execution Plan ???? ????? ???? ???? ?? ??? ??????.
????? Parameterization ? Database
??? ??????, ?? ???? ???? ???? ?????????? ?? ???????? ??? ?? ?????? ??? ???? ?? ?????????? ??? ???? ????? ??? ?Database . ?? ????? ??? ???? ???? ??????? ????? ???????????. ??? ???????? ??:
- Simple Parameterization – ???? ?????? ???????????? ??? ????? Database. ?????? ??? ???? ?????????? ???????? ?? ??? ????? ?????? ? Execution Plan ?? ????? ?????? ????????????. ?????, ?????? ??? ??????? ?????????? ??:
- Forced Parameterization – ??? ????? ?????, ?????? ??? ??????? ?????????? ??? Simple Parameterization ??????? ????, ?????? ??? ??????? ??????? ????? ???. ??? ????? ?????? ?? Forced Parameterization ??? ???? ???? ???? ????? ???? ???? ???????, ?? ????? ??????? ??? ???? ??????? ?????? Plan ????? ???? ??? ???????????. ??? ???? ???? ????? ?????? ???????????? ? Simple ? Forced ??? ????? ?? ??????. ?? ? Forced Parameterization ?? ????? ??? ??????????? ?? ???????, ?? ?????? ?? ????? ??? ???? ????? ?: http://technet.microsoft.com/en-us/library/ms175037.aspx
|
|
????? ???? ????? ?????? 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();