SQL Server: Tuning your SMO Application for great performance - PART 1 (featured at TechED)
I'm ramping up for TechED 2005 (Both USA and Europe) and will be giving a couple of talks on SMO. This is a repost of an article from SqlJunkies, as I am still moving these blog entries to MSDN. Ping me if you are visiting TechED and are interested in certain SMO topics.
Unlike SQL-DMO, SQL Server Management Objects (SMO) gives you a fairly fine level of control how data is retrieved from the server.
When using default behavior, an SMO object transitions into the following states:
1. Partially instantiated -- some properties are available (such as Name, Schema)
2. Fully instantiated -- all low cost properties are retrieved in bulk when you retrieve any of these
3. Expensive properties -- are fetched when needed, one at the time (such as database space)
The rationale behind this is to allow the object model to scale when a high number of objects need to be retrieved, for example when populating a collection of 10,000 tables. In contrast, SQL-DMO always fetches all properties when populating a collection. This can be compared with doing a 'select * from xxx', when you only want to know something about name and the creation date of the object. SMO fetches only the minimal set of properties that are required to populate the collection (i.e. for Table, Name and Schema are needed to uniquely identify the object).
Now here comes the catch: if your application does fetch extra properties, besides the minimally required ones, it will submit a query to retrieve these extra properties. This can cause your application to become extremely 'chatty', and will likely cause it to perform not as good (or just bluntly bad). This is especially noticed when doing 'foreach' enumerations, such as shown in the example below:
Server svr = new Server();
Database db = svr.Databases["AdventureWorks"];
foreach (Table t in db.Tables)
{
Console.WriteLine(t.Schema + "." + t.Name + " " + t.CreateDate);
}
You can inspect the result by switching SQL Profiler on and looking at the number of batches that are emitted to SQL Server.
When running above sample you will find about 155 rows in the SQL Profiler. The application is certainly chattier than it should be. This app's performance is especially down the drain on networks with a higher latency than a typical LAN, but even on a LAN it does not perform very well.
Enter Server.SetDefaultInitFields() . This call allows you to set the fields that are retrieved when the object gets initialized. This would add one extra line to the above sample:
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);
}
Note that you do not have to indicate any of fields that are retrieved by default (Name, Schema).
After running the above sample, you will find only 11 rows in SQL Profiler, and you will also notice that the first sample you could see it fill the screen line by line (I'm running this on a 1.8Ghz P4 Toshiba notebook with 1Gb of memory) and the optimized sample flashes by and disappears in a split second (could have added some timing to the sample, but for clarity I keeping it as short as possible.
The bottom line is that you need to understand your application behavior and tuning will make a big difference. With more options to tune SMO, the responsibility to tune your application has shifted to you, as SMO cannot guess what your application is going to request. "With great power comes great responsibility" :-)
Next post will be on some of the more advanced tuning options SMO has to offer. If you have a special request about what you like me post on in the SMO space (or SQL-DMO, SQLCMD, OSQL, SQL WMI Provider, SQL Computer Manager, or XP's for that matter), then let me know.
Michiel Wories (a PM on the SQL Server Team)
---
See also: https://staff.develop.com/bobb/weblog/PermaLink.aspx?guid=0c070aff-a30f-4669-b0ae-ef7a2cde81c6
---
This posting is provided "AS IS" with no warranties, and confers no rights.
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
November 27, 2006
<a href= http://forum.lixium.fr/cgi-bin/liste.eur?wellbut > wellbutrin sr </a> [url= http://forum.lixium.fr/cgi-bin/liste.eur?wellbut ] wellbutrin medication [/url]Anonymous
January 30, 2007
What about me? http://www.onlinewebservice6.de/gastbuch.php?id=128486 [URL=http://www.onlinewebservice6.de/gastbuch.php?id=128486]replica watch[/URL]Anonymous
February 10, 2007
Re:http://www.onlinewebservice6.de/gastbuch.php?id=128990 <a href="http://www.onlinewebservice6.de/gastbuch.php?id=128990">Replica bag, handbag</a>Anonymous
March 17, 2007
The comment has been removedAnonymous
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 03, 2007
<a href= http://posto.italyans.info >posto</a> [url=http://posto.italyans.info]posto[/url] <a href= http://dalla.italyans.info >dalla</a> [url=http://dalla.italyans.info]dalla[/url] <a href= http://sembrata.italyans.info >Sembrata</a> [url=http://sembrata.italyans.info]Sembrata[/url] <a href= http://visto.italyans.info >Visto</a> [url=http://visto.italyans.info]Visto[/url]Anonymous
April 04, 2007
Very nice! I have some sites with news, check this out: <a href= http://nuhost.info >Politics news</a> <a href= http://susearch.info >Lastest news</a> <a href= yanasearch.info >Lifestyle news</a>Anonymous
April 04, 2007
Your site is best look my site - <a href="http://freecal.brownbearsw.com/Magnarx">Magna rx</a>Anonymous
April 05, 2007
Very nice! I have some sites with news, check this out: <a href= http://kreolikko.livejournal.com >Politics news</a> <a href= http://icefroggg.livejournal.com >Lastest news</a> <a href= http://icyiceman.livejournal.com >Lifestyle news</a>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 21, 2007
http://videogratisnegrescopano.bitroot.info/ video gratis negre scopano http://ministerodellefinanzeit.bitroot.info/ ministerodellefinanze itAnonymous
April 25, 2007
Hi Sam! Photos i send on e-mail. Green,Hi Sam! Photos i send on e-mail. GreenAnonymous
May 24, 2007
<a href="http://extremedrawing.tripod.com/">adult comics</a>Anonymous
May 27, 2007
<a href= http://erotici.byethost7.com/in-italia-promozione-turismo.html >IN ITALIA PROMOZIONE TURISMO</a> [url=http://erotici.byethost7.com/in-italia-promozione-turismo.html]IN ITALIA PROMOZIONE TURISMO[/url] <a href= http://erotici.byethost7.com/promozione-sito-internet.html >PROMOZIONE SITO INTERNET</a> [url=http://erotici.byethost7.com/promozione-sito-internet.html]PROMOZIONE SITO INTERNET[/url] <a href= http://erotici.byethost7.com/promozione-siti-web.html >Promozione siti web</a> [url=http://erotici.byethost7.com/promozione-siti-web.html]Promozione siti web[/url] <a href= http://erotici.byethost7.com/immobiliare-promozione.html >Immobiliare promozione</a> [url=http://erotici.byethost7.com/immobiliare-promozione.html]Immobiliare promozione[/url]Anonymous
May 31, 2007
<a href= http://lujaho.angelfire.com >time attendance management system</a> <a href= http://felija.angelfire.com >painting tile floors</a> <a href= http://myxune.angelfire.com >diani beach hotels mombasa</a> <a href= http://leloqu.angelfire.com >imaging systems</a> <a href= http://hatodo.angelfire.com >bessey bar clamp</a>Anonymous
July 26, 2007
Great site! You can find related info on the following sites: Preved, losAnonymous
February 09, 2008
By default, when SMO queries the database for the properties of an object, it only loads the basic properties.Anonymous
March 18, 2008
I like this <a href="http://hondaonline4.info/924.html ">honda rebel specs</a> <a href="http://hondaonline4.info/705.html ">honda passports for sale</a> <a href="http://hondaonline4.info/730.html ">honda pilot gas mileage</a> <a href="http://hondaonline4.info/639.html ">honda of slidell</a> [URL=http://hondaonline4.info/594.html]honda odyssey troubleshooting[/URL] [URL=http://hondaonline4.info/489.html]honda nr750[/URL] new