SQL Server: Tuning your SMO Application for great performance -- PART 2
In the previous post I provided you with a fundamental tool to minimize the amount of SQL statements emitted and therefore limit expensive network round-trips (and SQL statements) using the Server.SetDefaultInitFields() method. In this post I will provide you with some more detail about this method, alongside with a recommendation about its use.
Let's take a quick look at the optimized sample that I provided last article:
Server svr = new Server();
Database db = svr.Databases["AdventureWorks"];
svr.SetDefaultInitFields(typeof(Table), "CreateDate");
foreach (Table t in db.Tables)
{
Console.WriteLine(t.Schema + "." + t.Name + " " + t.CreateDate);
}
This optimization is particular to this piece of code, but the setting is global to the Server instance so every time you will retrieve a Table object later in your code, it will load the same fields as just specified. It's recommended to save the state of the previous SetDefaultInitFields setting so you can set it to the same value after you have retrieved your objects. The following sample shows you have to save the state, and then set if back to that state:
Server svr = new Server();
Database db = svr.Databases["AdventureWorks"];
StringCollection sc = svr.GetDefaultInitFields(typeof(Table));
svr.SetDefaultInitFields(typeof(Table), "CreateDate");
foreach (Table t in db.Tables)
{
Console.WriteLine(t.Schema + "." + t.Name + " " + t.CreateDate);
}
svr.SetDefaultInitFields(typeof(Table), sc);
The reason you want to be careful with this is that the SetDefaultInitFields setting is global to the Server object reference, and you are likely to hold on to the Server object instance reference (svr in the above example) to perform additional work with it. For example, if you were to call Refresh() on the Tables collection, any additional operation that would cause a table object to be instantiated in the collection will use the latest SetDefaultInitFields setting.
SetDefaultInitFields has a few more helpful overloads that I will describe here:
SetDefaultInitFields(System.Type typeObject, System.Boolean allFields) |
Specifies all properties are fetched when the specified object is instantiated. When set to false, the fields will be reset to default. |
SetDefaultInitFields(System.Boolean allFields) |
Specifies all properties are fetched when any object is instantiated. When set to false, the fields will be reset to default.. |
Recommendation: make sure your application is aware of this global Server state, and either specifically set the fields or reset to the previous value. If you do not take care about this, you may see sudden application degradation (or your customers may start to notice), and as it will be very hard to test all code paths you cannot know beforehand how your application is going to behave in the future if· this is not carefully managed. Also, if you pass the Server reference on to another library that component may change the field settings and cause 'mysterious' performance degradation that may be hard to debug.
Comments
Anonymous
May 02, 2005
I'm starting to post articles around SMO and other topics, like WMI, SQLCMD, and other areas. This index...Anonymous
September 18, 2005
Hi,
Thanks for your example.. I have little question about 'SetDefaultInitFields' method.
To display tables and their columns' name for a database I've written a piece of code. But it raises an error 'A column named 'ID' already belongs to this DataTable.'
Thanks..
The code is similar to following:
Server server = new Server(".");
server.SetDefaultInitFields(true);
Database db = server.Databases["AdventureWorks"];
foreach (Table t in db.Tables)
{
foreach (Column column in t.Columns)
{
string s = column.Name;
}
}Anonymous
April 10, 2006
What about collection caching Michael ? How does one prevent SMO round-tripping to the database for each object in an SMO collection?
An example of this would be the Parameters collection of a StoredProcedure - how can we get all the parameters in one go rather than roundtripping to the database for each parameter?Anonymous
August 15, 2006
how can i know what are the SMO's defaults for each object ?Anonymous
November 29, 2006
Today was a complete loss. I feel like a fog. I've just been hanging out doing nothing, but eh...Anonymous
November 30, 2006
I've just been letting everything pass me by lately. I've more or less been doing nothing. Not much going on lately. I can't be bothered with anything recently.Anonymous
November 30, 2006
Not much on my mind recently. I haven't gotten much done. What can I say? I haven't been up to much these days, but such is life. I feel like a complete blank, but so it goes...)))Anonymous
December 04, 2006
Good site! Well.. i like design!Anonymous
December 09, 2006
I love peace! and..your site..)))Anonymous
December 10, 2006
Well...good news, i like your site, Happy new year! )))Anonymous
December 17, 2006
Happy New Year! real good site!Anonymous
December 17, 2006
real good site!
Anonymous
December 18, 2006
real good news! good site, respect webmaster!Anonymous
December 19, 2006
...good day! Congratulations on a great web site....))Anonymous
December 20, 2006
Good Site .Nice work.,Good Site .Nice work.Anonymous
December 21, 2006
Good Site .Nice work.,Good Site .Nice work.Anonymous
December 23, 2006
Excellent browsing have the to,Excellent browsing have the toAnonymous
December 24, 2006
Excellent browsing have the to,Excellent browsing have the toAnonymous
December 24, 2006
While site keep Good work,While site keep Good workAnonymous
December 27, 2006
http://allergies-pedia.googlegroups.com/web/allergy%20asthma%20induced.html?gda=rIwMQlEAAAALW6FvpEbToAJuf0u5jX5YFRzacMt3mssOVk0t9bMaCRVJVT3hrgKrn_IySc8la3I82CZxIrPCghV5P_RnjFgqR-N6lcbUGhxyEiqiLs_zEi08VVU3fRS65b6689NGZ3Q <a href="http://allergies-pedia.googlegroups.com/web/allergy%20asthma%20induced.html?gda=rIwMQlEAAAALW6FvpEbToAJuf0u5jX5YFRzacMt3mssOVk0t9bMaCRVJVT3hrgKrn_IySc8la3I82CZxIrPCghV5P_RnjFgqR-N6lcbUGhxyEiqiLs_zEi08VVU3fRS65b6689NGZ3Q">allergy asthma induced</a>Anonymous
December 29, 2006
Very good website you have here.: Thanks!,Very good website you have here.: Thanks!Anonymous
December 31, 2006
You have very nice site! well,,, happy new Year!!! My site: http://www.onlinewebservice6.de/gastbuch.php?id=126942Anonymous
January 05, 2007
Very good website you have here.: Thanks!,Very good website you have here.: Thanks!Anonymous
January 08, 2007
You have a good site! Real good html-codeAnonymous
January 09, 2007
Best site! Great! wow wow wow!Anonymous
January 19, 2007
Nise site. Thanks you Thanks!,Nise site. Thanks you Thanks!Anonymous
January 21, 2007
The comment has been removedAnonymous
January 22, 2007
Thank you! http://symy.jp/?Ct_220745,Thank">http://symy.jp/?Ct_220745,Thank you! http://symy.jp/?Ct_220745Anonymous
January 23, 2007
Will return in the near future. good job. excellent site. Thanks!,Will return in the near future. good job. excellent site. Thanks!Anonymous
January 24, 2007
Really amazing! interesting site. keep up the good work. Thanks!,Really amazing! interesting site. keep up the good work. Thanks!Anonymous
January 25, 2007
Excellent browsing have the toAnonymous
January 25, 2007
Nice site! http://z.la/1myuh,Nice">http://z.la/1myuh,Nice site! http://z.la/1myuhAnonymous
January 28, 2007
Nice site! http://32url.com/?iSlf,Nice">http://32url.com/?iSlf,Nice site! http://32url.com/?iSlfAnonymous
February 01, 2007
good news about vista! http://www.onlinewebservice6.de/gastbuch.php?id=128486Anonymous
February 02, 2007
Nice html code, good design! thanksssss... http://www.onlinewebservice6.de/gastbuch.php?id=128623Anonymous
February 03, 2007
This is my site: http://shurl.net/2SU ,This is my site: http://shurl.net/2SUAnonymous
February 06, 2007
This is my site: http://32url.com/?88KM ,This is my site: http://32url.com/?88KMAnonymous
February 18, 2007
More information, you work, stress, then you focus on a creative. Appraisal discussion, ask figure of his new overcome, or off to describe. Foundation for them out the basic level, this regularly undertake a [url=http://learnin.43i.net ]learning center.[/url].Anonymous
March 15, 2007
<a href="http://cigarsworld.net/Quintero-cigars.php">Quintero cigars</a> are famous because of their specific aromas and strong flavors, which can be sweet or spicy depending on the cigar type.Anonymous
March 18, 2007
best <a href="http://replica-watchesz.com">replica watch</a> retailer in the worldAnonymous
March 18, 2007
best <a href="http://replica-watchesz.com">replica watch</a> retailer in the worldAnonymous
March 20, 2007
I think what you are doing is great! <a href="http://losing-weight.exercise-vip.info/losing-weight.html ">losing weight</a> [url=http://losing-weight.exercise-vip.info/losing-weight.html ]losing weight[/url] http://losing-weight.exercise-vip.info/losing-weight.htmlAnonymous
March 30, 2007
Very nice! I have some LJ with news, check this out: <a href= http://michelas.livejournal.com >My live journal</a> <a href= http://homerius.livejournal.com >Lastest news</a> <a href= http://johnyknoxw.livejournal.com >My live journal</a>Anonymous
March 30, 2007
Very nice! I have some LJ with news, check this out: <a href= http://iwantubadlyz.livejournal.com >Newest news</a> <a href= http://annakubat.livejournal.com >Check this out</a> <a href= http://jackie_simpson.livejournal.com >livejournal</a>Anonymous
April 05, 2007
All <a href="http://replica-watchesz.com/Fendi-Watches.htm">Fendi watches</a> contain ETA Swiss movements and are water resistant to 100 feet.Anonymous
April 05, 2007
All <a href="http://replica-watchesz.com/Fendi-Watches.htm">Fendi watches</a> contain ETA Swiss movements and are water resistant to 100 feet.Anonymous
April 13, 2007
<a href="http://muonline-info.blogspot.com">MU</a>-online info - gameplay, characters, maps..Anonymous
April 18, 2007
The Best Catalog. <a href=http://healthpiece.info/>Real">http://healthpiece.info/>Real Catalog</a>[url=http://healthpiece.info/]The Real Catalog[/url]Anonymous
April 24, 2007
Hi Sam! Photos i send on e-mail. Green,Hi Sam! Photos i send on e-mail. GreenAnonymous
May 03, 2007
http://buyvaliumm.blogcu.com buy valium <a href="http://buyvaliumm.blogcu.com">buy valium</a> [url=http://buyvaliumm.blogcu.com]buy valium[/url]