Query handlingbytheserver

26
QUERY HANDLING

description

Adi Cohen ISUG 124

Transcript of Query handlingbytheserver

Page 1: Query handlingbytheserver

QUERY HANDLING

Page 2: Query handlingbytheserver

OPTIMIZER’S ROLE

• The optimizer should generate an efficient plan to access the data that we need to work with.

Page 3: Query handlingbytheserver

WHY DO WE NEED IT?

• SQL Statement defines what we are looking for. It doesn’t define how to get the data.

• The optimizer has to generate a query plan which tells the server how to get the data.

Page 4: Query handlingbytheserver

OPTIMIZER CHALENGES

Page 5: Query handlingbytheserver

NUMBER OF JOIN ORDERS

Page 6: Query handlingbytheserver

OPTIMIZER

• The optimizer doesn't necessarily look for the best query plan.

• The optimizer searches for a good enough plan.

Page 7: Query handlingbytheserver

WORK STAGES (HIGH LEVEL)

• Create a logical plan• Apply transformation rules• Apply heuristics• Select the cheapest one

Page 8: Query handlingbytheserver

STEPS OF QUERY PROCESSING

Page 9: Query handlingbytheserver

PARSING

• Checks that the syntax is correct• Creates a logical tree that represents the

query.

Page 10: Query handlingbytheserver

DEMO 1

• See logical tree that was produced by parse step.

Page 11: Query handlingbytheserver

BINDIND

• Gets the logical tree that was produced by the parsing step

• Makes sure that all objects referenced by the logical tree exist and that the user can see them.

Page 12: Query handlingbytheserver

OPTIMIZATION

• The most important step. Generates the query plan that will be executed

Page 13: Query handlingbytheserver

TRIVIAL PLAN

• Very simple queries with very simple logical tree won’t get full optimization

• The optimizer will generate a plan that is called a trivial plan

• The trivial plan is very cheap to generate and it won’t be inserted into the plan cache

Page 14: Query handlingbytheserver

TRIVIAL PLAN

• If the trivial plan’s cost is more then the value that is configured as “cost threshold for parallelism”, the query will get full optimization

• You can disable the trivial plan by using trace flag 8757

Page 15: Query handlingbytheserver

SIMPLIFICATION

• The OPTIMIZER rewrites the tree to make it more simple. There are few simplification methods:– Constant folding– Domain simplification– Contradiction detection

Page 16: Query handlingbytheserver

DEMO 2

• Simplification and trivial plan

Page 17: Query handlingbytheserver

OPTIMIZATION LEVELS

• If no trivial plan was found, SQL Server starts optimizing the query.

• SQL Server has 3 stages that are called:– Search 0 (Transaction processing)– Search 1 (Quick plan)– Search 2 (Full optimization)

Page 18: Query handlingbytheserver

OPTIMIZATION LEVELS

• Each level has an entry and termination condition.

• Termination condition can be good enough plan was found, or to much time passed

• Optimization can begin at a low search step and get to a higher search step.

Page 19: Query handlingbytheserver

PROPERTIES

• Each node in the logical tree has properties attached to it.

• There are 2 types of properties– Logical properties (Node cost, output columns,

Type information and nullability, etc)– Physical properties (Sort order, partition

information etc’)

Page 20: Query handlingbytheserver

RULES

• The optimizer has set of more then 350 rules that is using to optimize the query.

• The rules help the optimizer to modify the logical tree in a way that doesn’t effect the query’s results

• The rules also dictate the physical implementation of the logical tree

Page 21: Query handlingbytheserver

RULES

• There are four types of rules that can be used:– Simplification rules– Exploration rules– Implementation rules– Physical properties enforcement rules

Page 22: Query handlingbytheserver

MEMO

• All the trees are stored in memory in a structure that is called Memo

• Each optimization has its own memo• A single memo can get to the size of 1.6 GB

Page 23: Query handlingbytheserver

OPTIMIZATION PROBLEMS

• On rare occasions there can be a timeout for the optimization process

• Most times when it happens, it will be on search 2 stage, and the server will use the query plan that was produced on search 1 stage.

• Sometime the server stops optimization because of memory pressure

Page 24: Query handlingbytheserver

DMVs

• There are 2 DMVs that can give us information about the optimizer:– sys.dm_exec_query_optimizer_info, shows

information about optimization process– sys.dm_exec_query_transformation_stats,

shows information about rules usage

Page 25: Query handlingbytheserver

Demo 3