Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Query plan

From Wikipedia, the free encyclopedia
Sequence of steps used to access data in a SQL relational database management system

Aquery plan (orquery execution plan) is a sequence of steps used toaccess data in aSQLrelational database management system. This is a specific case of therelational model concept of access plans.

SinceSQL isdeclarative, there are typically many alternative ways to execute a given query, with widely varying performance. When aquery is submitted to the database, thequery optimizer evaluates some of the different, correct possible plans for executing the query and returns what it considers the best option. Because query optimizers are imperfect, database users and administrators sometimes need to manually examine and tune the plans produced by the optimizer to get better performance.

Generating query plans

[edit]

A given database management system may offer one or more mechanisms for returning the plan for a given query. Some packages feature tools which will generate a graphical representation of a query plan. Other tools allow a special mode to be set on the connection to cause the DBMS to return a textual description of the query plan. Another mechanism for retrieving the query plan involves querying a virtual database table after executing the query to be examined. In Oracle, for instance, this can be achieved using the EXPLAIN PLAN statement.

Graphical plans

[edit]
Microsoft SQL Server Management Studio displaying a sample query plan.

TheMicrosoft SQL Server Management Studio tool, which ships withMicrosoft SQL Server, for example, shows this graphical plan when executing this two-table join example against an included sample database:

SELECT*FROMHumanResources.EmployeeASeINNERJOINPerson.ContactAScONe.ContactID=c.ContactIDORDERBYc.LastName

The UI allows exploration of various attributes of the operators involved in the query plan, including the operator type, the number of rows each operator consumes or produces, and the expected cost of each operator's work.

Textual plans

[edit]

The textual plan given for the same query in the screenshot is shown here:

StmtText----|--Sort(ORDER BY:([c].[LastName] ASC))|--Nested Loops(Inner Join, OUTER REFERENCES:([e].[ContactID], [Expr1004]) WITH UNORDERED PREFETCH)|--Clustered Index Scan(OBJECT:([AdventureWorks].[HumanResources].[Employee].[PK_Employee_EmployeeID] AS [e]))|--Clustered Index Seek(OBJECT:([AdventureWorks].[Person].[Contact].[PK_Contact_ContactID] AS [c]),SEEK:([c].[ContactID]=[AdventureWorks].[HumanResources].[Employee].[ContactID]as[e].[ContactID])ORDEREDFORWARD)

It indicates that the query engine will do a scan over the primary key index on the Employee table and a matching seek through the primary key index (the ContactID column) on the Contact table to find matching rows. The resulting rows from each side will be shown to a nested loops join operator, sorted, then returned as theresult set to the connection.

In order to tune the query, the user must understand the different operators that the database may use, and which ones might be more efficient than others while still providing semantically correct query results.

Database tuning

[edit]
Main article:Database tuning

Reviewing the query plan can present opportunities for newindexes or changes to existing indexes. It can also show that the database is not properly taking advantage of existing indexes (seequery optimizer).

Query tuning

[edit]
Main article:Query optimizer

A query optimizer will not always choose the most efficient query plan for a given query. In some databases the query plan can be reviewed, problems found, and then thequery optimizer giveshints on how to improve it. In other databases, alternatives to express the same query (other queries that return the same results) can be tried. Some query tools can generate embedded hints in the query, for use by the optimizer.

Some databases - like Oracle - provide aplan table for query tuning. This plan table will return the costand time for executing a query. Oracle offers two optimization approaches:

  1. CBO or Cost Based Optimization
  2. RBO or Rule Based Optimization

RBO is slowly being deprecated. For CBO to be used, all the tables referenced by the query must be analyzed. To analyze a table, a DBA can launch code from the DBMS_STATS package.

Other tools for query optimization include:

  1. SQL Trace[1]
  2. Oracle Trace and TKPROF[2]
  3. Microsoft SMS (SQL) Execution Plan[3]
  4. Tableau Performance Recording (all DB)[4]
Types
Concepts
Objects
Components
Functions
Related topics

References

[edit]
  1. ^"SQL Trace".Microsoft.com. Microsoft. Retrieved30 March 2020.
  2. ^"Using SQL Trace and TKPROF".Oracle.com. Retrieved30 March 2020.
  3. ^"Execution Plans".Microsoft.com. Microsoft. Retrieved30 March 2020.
  4. ^"Optimize Workbook Performance".Tableau.com. Tableau Inc. Retrieved30 March 2020.
Retrieved from "https://en.wikipedia.org/w/index.php?title=Query_plan&oldid=1296956659"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp