I have a quite good number of queries and i want to test each of them with Include Actual Execution Plan feature on sql server management studio
However it is not possible for me to do this manually for 1m + queries
So i wonder can i execute them programmatically (from c#) with Include Actual Execution Plan feature and see whether SQL server suggests any index or not
First, before I go in to how to get the Actual Execution Plan in code and find the ones that report needing indexes I would recommend you look in to using the Database Engine Tuning Adviser (DTA), you can feed it a list of all the queries and it will process them telling you possible indexes, statistics, and many other things that can help out planning your queries.
Even better than giving it a list of 1m+ queries is you can get a trace from the server with the actual queries that are being run and it will focus on the queries that are taking up the most time.
To answer your original question you will need to add
SET STATISTICS XML ON
at the start of the connection, this will give you the XML data that the GUI you showed is based off of. (See here for more info about getting the plans). Once you do that your queries will return with a extra result set containing the xml for the plan in the first row of the first column.Here is a quick and dirty function that does that.
And here is the XML it returned for the query
select TOTAL_SALES from clients where ACTIVE = 0;
that I ran that I had on one of my local databases.Now, because Microsoft is quite nice, if you navigate to the namespace listed in the XML you can actually get a copy of the
.xsd
for the format. You can then from the developer's command prompt doxsd showplanxml.xsd /classes
and it will give you ashowplanxml.cs
that you can use with theXmlSerializer
.Here is a small example program that does a debugger break on a missing index.
I used XmlSerializer and deseralized it to a class but you could just as easily loaded this in to a XDocument then used XPath to find all the nodes named
MissingIndex
.