DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Data Engineering
  3. Data
  4. Query Optimizer and Data Definition Language Queries

Query Optimizer and Data Definition Language Queries

Are you SURE that data definition language (DDL) queries aren't run through your database's optimizer? It turns out that the tests aren't so conclusive.

Grant Fritchey user avatar by
Grant Fritchey
·
Jan. 11, 17 · Analysis
Like (2)
Save
Tweet
Share
3.19K Views

Join the DZone community and get the full member experience.

Join For Free

data definition language queries don’t go through the optimizer, right? normally my short answer to this question in the past would have been yes. however, with testing comes knowledge and i want to be sure about the statement.

i’m working with a team of people to completely rewrite the sql server execution plans book. we’ll probably be published in april or may. it’s going to be amazing. the structure will be completely different and the book will represent five years of additional knowledge in how execution plans work and how to read and interpret them since the last book was written. however, enough on that. let’s answer the question about data definition language.

first of all, we need to quickly define our terms. data definition language (ddl) represents the syntax for queries that build your databases and the data structures within them. as a result, if you read the documentation at msdn, you’ll see that this includes create, alter, drop, enable/disable trigger, truncate table and update statistics. consequently, these command are separated from the data manipulation language (dml) queries that are used to select, update, delete and insert your data (amongst other things).

testing the data definition language query

here is an example of one of the simplest possible ddl queries:

create table dbo.mynewtable (  mynewtableid int primary key identity(1, 1),
                               mynewvalue nvarchar(50)
                            );
go


we can check whether or not you’re going to get an execution plan for this in two easy ways. you can capture the estimated plan, or run the query and capture the actual plan. since most of the time, it’s preferable to work with the added runtime information that an actual plan provides, let’s start there. however, if you execute the query above and try to capture an actual plan, you will not get one. so, let’s capture the estimated plan instead. here’s what we see:

image title

not much of a plan really. the devil is in the details though, so let’s open the properties page (always use the properties page, the tooltip is just too unreliable in the amount of information displayed):

data definition language properties

that is all the properties that are available. the implication is simple, sql server doesn’t create execution plans for ddl statements.

further, the lack of a plan suggests that these queries must not go through the query optimization process. we can check this. there is a dynamic management view (dmv), sys.dm_exec_query_optimizer_info, that shows aggregate values for the work done by the optimizer. it’s a little tricky to capture the metrics of a single query, but it is possible (note: don’t run this on production, please):

dbcc freeproccache();
go

select *
into opinfoafter
from sys.dm_exec_query_optimizer_info as deqoi;
go

drop table opinfoafter;
go

--gather the existing optimizer information
select *
into opinfobefore
from sys.dm_exec_query_optimizer_info as deqoi;
go

--run a query
create table dbo.mynewtable (  mynewtableid int primary key identity(1, 1),
                               mynewvalue nvarchar(50)
                            );
go

select *
into opinfoafter
from sys.dm_exec_query_optimizer_info as deqoi;
go

--display the data that has changed
select oia.counter,
       (oia.occurrence - oib.occurrence) as actualoccurence,
       (oia.occurrence * oia.value - oib.occurrence * oib.value) as actualvalue
from opinfobefore as oib
join opinfoafter as oia
   on oib.counter = oia.counter
where oia.occurrence <> oib.occurrence;
go

drop table opinfobefore;
drop table opinfoafter;
go


if we run this, we won’t see any values. this is because this create table statement doesn’t go through the optimizer. there’s no optimization possible, so you won’t see an execution plan of any kind.

does this mean that my first answer is correct then? does the optimizer skip making execution plans for ddl statements?

changing the data definition language query

let’s modify the query so that we’re doing an alter table instead of a create:

alter table sales.customer  with check add  constraint sometest foreign key(customerid)
references [dbo].[agent] ([agentid]);


if we check the estimated and actual plans using the same methods above, we don’t get an execution plan (well, the estimated plan is the same t-sql place holder that we saw). what happens if we see what the optimizer did? this time, we get results:

counter actualoccurence actualvalue
elapsed time 2 0
final cost 2 0.139429282
insert stmt 1 1
maximum dop 2 0
optimizations 2 2
search 1 2 2
search 1 tasks 2 117
search 1 time 2 0
tables 2 2
tasks 2 117
view reference 1 1


i’ll tell you right now, i don’t know what this represents. i suspect i’d need to run the debugger to see it. maybe it’s a plan for the check process of the check constraint. the optimizer is involved in this process, twice. two different statements were optimized. one involved inserting data and one involved referencing a view. however, at the end of the process, we still don’t see an execution plan displayed.

how about extended events

can we capture the events to see what’s happening? we can try. i’ll spin up a session and capture the following events:

  • query_post_execution_showplan

  • sql_statement_completed

  • sp_statement_completed

in addition, i’ll enable event correlation so that we can see the order in which events occurred. now, i’ll try running the alter table command to see what we can find out:

name statement attach_activity_id.guid attach_activity_id.seq
query_post_execution_showplan null 65a0a74b-e5d5-460c-846d-87808c334283 1
sp_statement_completed select * from [sales].[customer] 65a0a74b-e5d5-460c-846d-87808c334283 2
sql_statement_completed alter table sales.customer  with check add  constraint sometest foreign key(customerid)  references [dbo].[agent] ([agentid]) 65a0a74b-e5d5-460c-846d-87808c334283 3


and there we have it. we can see that there was a plan compiled, first, then a procedure call was made with a single statement: select * from sales.customer. finally our sql batch statement that ran the alter table was created. everything is grouped by the activity guid and we have them in the sequence determined by attach_activity_id.seq.

after creating our plan, there are a couple of takeaways. in short, there was a plan generated here, but it is, as i guessed at, the plan for the check constraint process. there is no plan for the data definition language statement.

conclusion

just saying something doesn’t make it true. you have to be able to prove it. from every evidence that microsoft supplies us, the ddl statements do not generate an execution plan or go through the query optimization process. because we tested this theory by attempting to create a plan, by observation of the optimizer through the sys.dm_exec_query_optimizer_info dmv, and through extended events, we can therefore safely say that none of the statements tested created any kind of plan for the ddl statement.

have i tested every variation on every possible ddl statement? no. is it possible that other statements could have execution plans associated with them? i won’t say that’s impossible, because nothing is impossible. therefore, i’ll simply say, that’s unlikely.

Database Data definition language Data (computing)

Published at DZone with permission of Grant Fritchey, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Fixing Bottlenecks in Your Microservices App Flows
  • Create a CLI Chatbot With the ChatGPT API and Node.js
  • Kubernetes-Native Development With Quarkus and Eclipse JKube
  • 5 Best Python Testing Frameworks

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: