开发者

What are SQL Execution Plans and how can they help me?

开发者 https://www.devze.com 2022-12-18 06:54 出处:网络
I\'ve been hearing a lot lately that I ought to take a look at the execution plan of my SQL to make a judgment on how well it will perform.However, I\'m not really sure where to begin with this featur

I've been hearing a lot lately that I ought to take a look at the execution plan of my SQL to make a judgment on how well it will perform. However, I'm not really sure where to begin with this feature or what exactly it means.

I'm looking for either a good explanation o开发者_JAVA百科f what the execution plan does, what its limitations are, and how I can utilize it or direction to a resource that does.


It describes actual algorithms which the server uses to retrieve your data.

An SQL query like this:

SELECT  *
FROM    mytable1
JOIN    mytable2
ON      …
GROUP BY
        …
ORDER BY
        …

, describes what should be done but not how it should be done.

The execution plan shows how: which indexes are used, which join methods are chosen (nested loops or hash join or merge join), how the results are grouped (using sorting or hashing), how they are ordered etc.

Unfortunately, even modern SQL engines cannot automatically find the optimal plans for more or less complex queries, it still takes an SQL developer to reformulate the queries so that they are performant (even they do what the original query does).

A classical example would be these too queries:

SELECT  (
        SELECT  COUNT(*)
        FROM    mytable mi
        WHERE   mi.id <= mo.id
        )
FROM    mytable mo
ORDER BY 
        id

and

SELECT  RANK() OVER (ORDER BY id)
FROM    mytable

, which do the same and in theory should be executed using the same algorithms.

However, no actual engine will optimize the former query to implement the same algorithms, i. e. store a counter in a variable and increment it.

It will do what it's told to do: count the rows over and over and over again.

To optimize the queries you need to actually see what's happening behind the scenes, and that's what the execution plans show you.

You may want to read this article in my blog:

  • Double-thinking in SQL


Here and Here are some article check it out. Execution plans lets you identify the area which is time consuming and therefore allows you to improve your query.


  1. An execution plan shows exactly how SQL Server processes a query
  2. it is produced as part of the query optimisation process that SQL Server does. It is not something that you directly create.
  3. it will show what indexes it has decided are best to be used, and basically is a plan for how SQL server processes a query
  4. the query optimiser will take a query, analyse it and potentially come up with a number of different execution plans. It's a cost-based optimisation process, and it will choose the one that it feels is the best.
  5. once an execution plan has been generated, it will go into the plan cache so that subsequent calls for that same query can reuse the same plan again to save having to redo the work to come up with a plan.
  6. execution plans automatically get dropped from the cache, depending on their value (low value plans get removed before high value plans do in order to provide maximum performance gain)
  7. execution plans help you spot performance issues such as where indexes are missing


A way to ease into this, is simply by using "Ctrl L" (Query | Display Estimated Execution Plan) for some of your queries, in SQL Management Studio.

This will result in showing a graphic view of Execution Plan, which, at first are easier to "decode" than the text version thereof.

Query plans in a tiny nutshell:
Essentially the query plan show the way SQL Server intends to use in resolving a query.
There are indeed many options, even with simple queries.
For example when dealing with a JOIN, one needs to decide whether to loop through the [filtered] rows of "table A" and to lookup the rows of "table B", or to loop through "table B" first instead (this is a simplified example, as there are many other tricks which can be used in dealing with JOINs). Typically, SQL will estimate the number of [filtered] rows which will be produced by either table and pick the one which the smallest count for the outer loop (as this will reduce the number of lookups in the other table)
Another example, is to decide which indexes to use (or not to use).

There are many online resources as well as books which describe the query plans in more detail, the difficulty is that SQL performance optimization is a very broad and complex problem, and many such resources tend to go into too much detail for the novice; One first needs to understand the fundamental principles and structures which underlie SQL Server (the way indexes work, the way the data is stored, the difference between clustered indexes and heaps...) before diving into many of the [important] details of query optimization. It is a bit like baseball: first you need to know the rules before understanding all the subtle [and important] concepts related to the game strategy.

See this related SO Question for additional pointers.


Here's a great resource to help you understand them http://downloads.red-gate.com/ebooks/HighPerformanceSQL_ebook.zip

This is from red-gate which is a company that makes great SQL server tools, it's free and it's well worth the time to download and read.


it is a very serious part of knowledge. And I highly to recommend special training courses about that. As for me after spent week on courses I boosted performance of queries about 1000 times (nostalgia)


The Execution Plan shows you how the database is fetching, sorting and filtering the data required for your query.

For example:

SELECT
    *
FROM
    TableA
INNER JOIN
    TableB
ON
    TableA.Id = TableB.TableAId
WHERE
    TableB.TypeId = 2
ORDER BY
    TableB.Date ASC

Would result in an execution plan showing the database getting records from TableA and TableB, matching them to satisfy the JOIN, filtering to satisfy the WHERE and sorting to satisfy the ORDER BY.

From this, you can work out what is slowing down the query, whether it would be beneficial to review your indexes or if you can speed things up in another way.

0

精彩评论

暂无评论...
验证码 换一张
取 消