Welcome to MSDN Blogs Sign in | Join | Help

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.
Published Monday, May 02, 2005 2:21 AM by mwories
Filed under: ,

Comments

# Index for SQL Server related articles

Monday, May 02, 2005 3:32 PM by Michiel Wories' WebLog
I'm starting to post articles around SMO and other topics, like WMI, SQLCMD, and other areas. This index...

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

Monday, September 19, 2005 3:37 AM by evreno
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;
}
}

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

Tuesday, April 11, 2006 2:15 AM by John Gunning
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?

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

Wednesday, August 16, 2006 3:51 AM by tomer
how can i know what are the SMO's defaults for each object ?

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

Wednesday, November 29, 2006 11:32 AM by Donna

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

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

Thursday, November 30, 2006 2:23 PM by Linda

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.

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

Friday, December 01, 2006 3:10 AM by Alise

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

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

Monday, December 04, 2006 10:01 AM by Mikle

Good site! Well.. i like design!

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

Saturday, December 09, 2006 2:06 PM by Mister

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

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

Sunday, December 10, 2006 9:03 AM by Milas

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

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

Monday, December 18, 2006 2:02 AM by Jonn

Happy New Year! real good site!

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

Monday, December 18, 2006 7:25 AM by Jak

real good site!

-

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

Monday, December 18, 2006 9:40 AM by Dingo

real good news! good site, respect webmaster!

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

Tuesday, December 19, 2006 10:25 PM by Lohness

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

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

Thursday, December 21, 2006 7:35 AM by ware,ware

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

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

Friday, December 22, 2006 5:59 AM by besti,besti

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

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

Saturday, December 23, 2006 7:40 PM by sarde,sarde

Excellent browsing have the to,Excellent browsing have the to

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

Sunday, December 24, 2006 9:04 AM by assic,assic

Excellent browsing have the to,Excellent browsing have the to

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

Monday, December 25, 2006 4:45 AM by cantant,cantant

While site keep Good work,While site keep Good work

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

Friday, December 29, 2006 9:14 PM by [3!]fiva,[3!]fiva

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

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

Sunday, December 31, 2006 1:14 PM by Unison

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

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

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

Friday, January 05, 2007 4:21 PM by [3!]ferta,[3!]ferta

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

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

Monday, January 08, 2007 3:33 PM by Bob

You have a good site! Real good html-code

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

Tuesday, January 09, 2007 8:53 PM by Unison

Best site! Great! wow wow wow!

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

Friday, January 19, 2007 11:14 AM by [5!],[5!]

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

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

Monday, January 22, 2007 12:44 AM by [3!]ferta,[3!]ferta

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!

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

Monday, January 22, 2007 8:22 AM by Preved

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

Tuesday, January 23, 2007 11:45 PM by [3!]fiva,[3!]fiva

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

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

Wednesday, January 24, 2007 8:07 AM by [3!]fiva,[3!]fiva

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

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

Thursday, January 25, 2007 11:05 AM by appa[3!]

Excellent browsing have the to

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

Friday, January 26, 2007 2:49 AM by Tomas

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

Sunday, January 28, 2007 9:35 PM by Chuni

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

Thursday, February 01, 2007 7:10 PM by Ron

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

Saturday, February 03, 2007 1:17 AM by Michael

Nice html code, good design! thanksssss...

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

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

Saturday, February 03, 2007 9:34 AM by Womens

This is my site:

http://shurl.net/2SU

,This is my site:

http://shurl.net/2SU

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

Wednesday, February 07, 2007 3:02 AM by Michael Kors

This is my site:

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

,This is my site:

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

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

Sunday, February 18, 2007 1:58 PM by Bill

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

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

Friday, March 16, 2007 1:45 AM by Jak

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

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

Sunday, March 18, 2007 3:22 PM by Guruchel

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

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

Sunday, March 18, 2007 3:56 PM by Rick

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

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

Tuesday, March 20, 2007 3:32 PM by fitness equipment

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

Saturday, March 31, 2007 2:40 AM by Britneyyeyea

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>

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

Saturday, March 31, 2007 7:34 AM by Britneyclooe

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>

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

Friday, April 06, 2007 7:18 AM by Victor

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

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

Friday, April 06, 2007 7:42 AM by Jak

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

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

Friday, April 13, 2007 9:21 AM by Rick

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

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

Wednesday, April 18, 2007 4:59 PM by healthbgc,healthbgc,healthbgc

The Best Catalog.

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

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

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

Hi Sam! Photos i send on e-mail.

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

Green

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

Thursday, May 03, 2007 9:27 PM by konan

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