2. Addhoc Queries
Any non-Parameterized queries are called addhoc queries. For
example :
SELECT MsgID, Severity FROM SqlMessage WHERE MsgID = 100
In sql server if we execute a sql query it goes through two steps
just like any other programming languages:
• 1. Compilation
• 2. Execution
3. Properties of addhoc query
• Case sensitive
• Space sensitive
• Parameter sensitive
Sql severs treats two same sql query but of different parameters
as different sql statements. For example:
• SELECT MsgID, Severity FROM SqlMessage WHERE MsgID = 1
• SELECT MsgID, Severity FROM SqlMessage WHERE MsgID = 2
4. Effect of faulty C# code
• Sql server has took extra n * (Compilation time) ms to display
records
• Extra time to insert records in cached plans.
• Sql server has to frequently fire a job to delete the cached
plan since it will reach the max limit very soon.
• It will not only decrease the performance of this sql query but
all sql queries of other application since this faulty code will
force to delete cached query plans of other sql statement.
6. Query Processing
• Non-Procedural: . SELECT, DELETE, UPDATE
statements etc.
• a. Analyze the query.
• b. Get execution plan.
• b. Process the query.
• Procedural: Stored procedure, sql functions
etc.
8. Query Optimizer
The query optimizer in SQL Server is cost-based. It includes:
1. Cost for using different resources (CPU and IO)
2. Total execution time
It determines the cost by using:
• Cardinality: The total number of rows processed at each level of a
query plan with the help of histograms , predicates and constraint
• Cost model of the algorithm: To perform various operations like
sorting, searching, comparisons etc.
9. Execution Plan and Caching
• Query Plan
• Execution Context
• Database Engine uses a cost-based approach
to determine which execution plans to remove
from the procedure cache.
10. Assignments
• 1. Give any example in which join performs better
than sub query.
• 2. Give me any example in which order matters in
WHERE clause.
• 3. Give me any three examples which query
returns same output but have different execution
plan
http://msdn.microsoft.com/en-
us/library/ms175913(v=sql.105).aspx