Query plan

Query plan

A query plan (or query execution plan) is a set of steps used to access or modify information in a SQL relational database management system. This is a specific case of the relational model concept of access plans.

Since SQL is declarative, there are typically a large number of alternative ways to execute a given query, with widely varying performance. When a query is submitted to the database, the query optimizer evaluates some of the different, correct possible plans for executing the query and returns what it considers the best alternative. 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

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.

Graphical plans

Textual plans

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] ) ORDERED FORWARD)
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 the result 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

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

Query tuning

The query optimizer will not always choose the best query plan for a given query. In some databases the query plan can be reviewed, problems found, and then the query optimizer given hints 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 a Plan table for query tuning. This plan table will return the costand time for executing a Query. In Oracle there are 2 optimization techniques:
# CBO or Cost Based Optimization
# RBO or Rule Based Optimization

The 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 package DBMS_STATS can be made use of.

The others methods for query optimization include:
# SQL Trace
# Oracle Trace
# TKPROF


Wikimedia Foundation. 2010.

Игры ⚽ Поможем решить контрольную работу

Look at other dictionaries:

  • Query plan — …   Википедия

  • Query optimization — is a function of many relational database management systems in which multiple query plans for satisfying a query are examined and a good query plan is identified. This may or not be the absolute best strategy because there are many ways of doing …   Wikipedia

  • Query language — Query languages are computer languages used to make queries into databases and information systems. Broadly, query languages can be classified according to whether they are database query languages or information retrieval query languages. The… …   Wikipedia

  • Query optimizer — The query optimizer is the component of a database management system that attempts to determine the most efficient way to execute a query. The optimizer considers the possible query plans for a given input query, and attempts to determine which… …   Wikipedia

  • Plan 9 from Bell Labs — Infobox OS name = Plan 9 from Bell Labs caption = Glenda, the Plan 9 Bunny developer = Bell Labs source model = Free software/Open source kernel type = Hybrid supported platforms = x86, MIPS, DEC Alpha, SPARC, PowerPC, ARM ui = rio / rc family =… …   Wikipedia

  • Query and Echo — Superherobox > caption= comic color=background:#8080ff character name=Query and Echo real name=Diedre Vance and Nina Damfino publisher=DC Comics debut= Detective Comics #8 Annual (1995) creators=Chuck Dixon Kieron Dwyer alliance color= alliances …   Wikipedia

  • Plan B free agency — The Plan B free agency was a type of free agency that became active in the National Football League in February 1989. Plan B free agency permitted all teams in the NFL to preserve limited rights of no more than 37 total players a season. If a… …   Wikipedia

  • Plan for Establishing Uniformity in the Coinage, Weights, and Measures of the United States — The Plan for Establishing Uniformity in the Coinage, Weights, and Measures of the United States was a report submitted to the U.S. House of Representatives on July 13, 1790 by Secretary of State Thomas Jefferson.At the First United States… …   Wikipedia

  • 1993 Clinton health care plan — The Clinton health care plan, sometimes called HillaryCare by opponents, [cite book | last=Estrich | first=Susan | authorlink=Susan Estrich | title=The Case for Hillary Clinton | publisher=HarperCollins | year=2005 | isbn=0060839880 p. 104.]… …   Wikipedia

  • 100-Hour Plan — The 100 Hour Plan was a United States Democratic Party political strategy detailing the actions the party pursued upon assuming leadership of the 110th Congress on January 4, 2007. The strategy was announced before the 2006 midterm elections.… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”