SQL Server: Tuning your SMO Application for great performance -- PART 2

Published 02 May 05 02:21 AM | mwories 

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.
Filed under: ,

Comments

# Michiel Wories' WebLog said on May 2, 2005 3:32 PM:
I'm starting to post articles around SMO and other topics, like WMI, SQLCMD, and other areas. This index...
# evreno said on September 19, 2005 3:37 AM:
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;
}
}
# John Gunning said on April 11, 2006 2:15 AM:
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?
# tomer said on August 16, 2006 3:51 AM:
how can i know what are the SMO's defaults for each object ?
# Donna said on November 29, 2006 11:32 AM:

Today was a complete loss. I feel like a fog. I've just been hanging out doing nothing, but eh...

# Linda said on November 30, 2006 2:23 PM:

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.

# Alise said on December 1, 2006 3:10 AM:

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...)))

# Mikle said on December 4, 2006 10:01 AM:

Good site! Well.. i like design!

# Mister said on December 9, 2006 2:06 PM:

I love peace! and..your site..)))

# Milas said on December 10, 2006 9:03 AM:

Well...good news, i like your site, Happy new year! )))

# Jonn said on December 18, 2006 2:02 AM:

Happy New Year! real good site!

# Jak said on December 18, 2006 7:25 AM:

real good site!

-

# Dingo said on December 18, 2006 9:40 AM:

real good news! good site, respect webmaster!

# Lohness said on December 19, 2006 10:25 PM:

...good day! Congratulations on a great web site....))

# ware,ware said on December 21, 2006 7:35 AM:

Good Site .Nice work.,Good Site .Nice work.

# besti,besti said on December 22, 2006 5:59 AM:

Good Site .Nice work.,Good Site .Nice work.

# sarde,sarde said on December 23, 2006 7:40 PM:

Excellent browsing have the to,Excellent browsing have the to

# assic,assic said on December 24, 2006 9:04 AM:

Excellent browsing have the to,Excellent browsing have the to

# cantant,cantant said on December 25, 2006 4:45 AM:

While site keep Good work,While site keep Good work

# Allergy said on December 27, 2006 11:01 AM:

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>

# [3!]fiva,[3!]fiva said on December 29, 2006 9:14 PM:

Very good website you have here.: Thanks!,Very good website you have here.: Thanks!

# Unison said on December 31, 2006 1:14 PM:

You have very nice site! well,,, happy new Year!!!

My site: http://www.onlinewebservice6.de/gastbuch.php?id=126942

# [3!]ferta,[3!]ferta said on January 5, 2007 4:21 PM:

Very good website you have here.: Thanks!,Very good website you have here.: Thanks!

# Bob said on January 8, 2007 3:33 PM:

You have a good site! Real good html-code

# Unison said on January 9, 2007 8:53 PM:

Best site! Great! wow wow wow!

# [5!],[5!] said on January 19, 2007 11:14 AM:

Nise site. Thanks you Thanks!,Nise site. Thanks you Thanks!

# [3!]ferta,[3!]ferta said on January 22, 2007 12:44 AM:

Wien ist mir im moment zu distanziert... Dann will ich dir wenigstens auf diesem wege meine lieben grube zukommen lassen!!! :) Thanks!,Wien ist mir im moment zu distanziert... Dann will ich dir wenigstens auf diesem wege meine lieben grube zukommen lassen!!! :) Thanks!

# Preved said on January 22, 2007 8:22 AM:

Thank you!

http://symy.jp/?Ct_220745,Thank">http://symy.jp/?Ct_220745,Thank you!

http://symy.jp/?Ct_220745

# [3!]fiva,[3!]fiva said on January 23, 2007 11:45 PM:

Will return in the near future. good job. excellent site. Thanks!,Will return in the near future. good job. excellent site. Thanks!

# [3!]fiva,[3!]fiva said on January 24, 2007 8:07 AM:

Really amazing! interesting site. keep up the good work. Thanks!,Really amazing! interesting site. keep up the good work. Thanks!

# appa[3!] said on January 25, 2007 11:05 AM:

Excellent browsing have the to

# Tomas said on January 26, 2007 2:49 AM:

Nice site!

http://z.la/1myuh,Nice">http://z.la/1myuh,Nice site!

http://z.la/1myuh

# Chuni said on January 28, 2007 9:35 PM:

Nice site!

http://32url.com/?iSlf,Nice">http://32url.com/?iSlf,Nice site!

http://32url.com/?iSlf

# Ron said on February 1, 2007 7:10 PM:

good news about vista!

http://www.onlinewebservice6.de/gastbuch.php?id=128486

# Michael said on February 3, 2007 1:17 AM:

Nice html code, good design! thanksssss...

http://www.onlinewebservice6.de/gastbuch.php?id=128623

# Womens said on February 3, 2007 9:34 AM:

This is my site:

http://shurl.net/2SU

,This is my site:

http://shurl.net/2SU

# Michael Kors said on February 7, 2007 3:02 AM:

This is my site:

http://32url.com/?88KM

,This is my site:

http://32url.com/?88KM

# Bill said on February 18, 2007 1:58 PM:

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].

# Jak said on March 16, 2007 1:45 AM:

<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.

# Guruchel said on March 18, 2007 3:22 PM:

best <a href="http://replica-watchesz.com">replica watch</a> retailer in the world

# Rick said on March 18, 2007 3:56 PM:

best <a href="http://replica-watchesz.com">replica watch</a> retailer in the world

# fitness equipment said on March 20, 2007 3:32 PM:

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.html

# Britneyyeyea said on March 31, 2007 2:40 AM:

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>

# Britneyclooe said on March 31, 2007 7:34 AM:

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>

# Victor said on April 6, 2007 7:18 AM:

All <a href="http://replica-watchesz.com/Fendi-Watches.htm">Fendi watches</a> contain ETA Swiss movements and are water resistant to 100 feet.

# Jak said on April 6, 2007 7:42 AM:

All <a href="http://replica-watchesz.com/Fendi-Watches.htm">Fendi watches</a> contain ETA Swiss movements and are water resistant to 100 feet.

# Rick said on April 13, 2007 9:21 AM:

<a href="http://muonline-info.blogspot.com">MU</a>-online info - gameplay, characters, maps..

# healthbgc,healthbgc,healthbgc said on April 18, 2007 4:59 PM:

The Best Catalog.

<a href=http://healthpiece.info/>Real">http://healthpiece.info/>Real Catalog</a>[url=http://healthpiece.info/]The Real Catalog[/url]

# Hi Sam! Photos i send on e-mail. Green,Green said on April 25, 2007 6:27 AM:

Hi Sam! Photos i send on e-mail.

Green,Hi Sam! Photos i send on e-mail.

Green

# konan said on May 3, 2007 9:27 PM:

http://buyvaliumm.blogcu.com buy valium

<a href="http://buyvaliumm.blogcu.com">buy valium</a>

[url=http://buyvaliumm.blogcu.com]buy valium[/url]

Anonymous comments are disabled
Page view tracker