Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are SQL Execution Plans based on Schema or Data or both?

I hope this question is not too obvious...I have already found lots of good information on interpreting execution plans but there is one question I haven't found the answer to.

Is the plan (and more specifically the relative CPU cost) based on the schema only, or also the actual data currently in the database?

I am try to do some analysis of where indexes are needed in my product's database, but am working with my own test system which does not have close to the amount of data a product in the field would have. I am seeing some odd things like the estimated CPU cost actually going slightly UP after adding an index, and am wondering if this is because my data set is so small.

I am using SQL Server 2005 and Management Studio to do the plans

like image 870
Rich Avatar asked Jan 24 '11 21:01

Rich


People also ask

How does SQL execution plan work?

The SQL Server execution plan (query plan) is a set of instructions that describes which process steps are performed while a query is executed by the database engine. The query plans are generated by the query optimizer and its essential goal is to generate the most efficient (optimum) and economical query plan.

What does SQL Server execution plan look for?

SQL Server execution plans are a gateway for query optimizer and query executions for database professionals. They reveal query processing and involved phases such as affected tables, indexes, statistics, types of joins, the number of affected rows, query processing, data sort and data retrieval.

What are the two types of execution plans available in SSMS?

SQL Server Management Studio has three options to display execution plans: The Estimated Execution Plan is the compiled plan, as produced by the Query Optimizer based on estimations. This is the query plan that is stored in the plan cache. The Actual Execution Plan is the compiled plan plus its execution context.

What is the execution plan and when is it created?

An execution plan in SQL Server is a simple graphical representation of the operations that the query optimizer generates to calculate the most efficient way to return a set of results.


1 Answers

It will be based on both Schema and Data. The Schema tells it what indexes are available, the Data tells it which is better.

The answer can change in small degrees depending on the DBMS you are using (you have not stated), but they all maintain statistics against indexes to know whether an index will help. If an index breaks 1000 rows into 900 distinct values, it is a good index to use. If an index only results in 3 different values for 1000 rows, it is not really selective so it is not very useful.

like image 143
RichardTheKiwi Avatar answered Sep 28 '22 01:09

RichardTheKiwi