Optimized Instantiation

Optimized Instantiation, also called Lazy Instantiation, is one of the performance features of SMO. When an object is first created, by default all its properties are not immediately retrieved from the server; only the essential properties needed for scripting are retrieved. This is done to improve the performance of the object model by saving extra CPU cycles and internal process memory needed for retrieval of properties that are infrequently used. The state of the object when only partial properties are available is called the partially instantiated state. You can also request for an object to be fully populated with properties upon creation. The state of an object when all its properties, including more and less frequently used ones, are retrieved from the server is called the fully instantiated state. Requesting a fully instantiated object on creation will reduce load on the network and query engine because if all properties are going to be needed, it makes sense to retrieve them once and thus submit fewer queries to the Database Engine. Some objects may also have expensive properties that are retrieved only when requested, even after an object is already in a fully instantiated state. Figure 11-7 demonstrates the tradeoffs of delayed instantiation versus full instantiation.

Figure 11-7. Optimized instantiation design trade-offs.

For the advanced programmer, there is an even finer-grained control over what properties are retrieved when an object is first created. You can actually request and specify what properties are retrieved for any object type by calling the GetdefaultInitFields and SetDefaultInitFields methods on a Server object. This way, you can even request any of the expensive properties be retrieved up front. For example, the following sets default fields for any Database object to be its Name and Collation:

serverInstance.SetDefaultInitFields(typeof(Database), new string[] { "Name", "Collation" }); 

If you want any Database object to be fully instantiated from the start, you make the following call:

serverInstance.SetDefaultInitFields(typeof(Database), true); 

Suppose the object is already created and you want to make sure it is fully initialized. You can achieve this goal by calling the object's Initialize method and passing the Boolean value "true" as an argument.

Inside SQL Server 2005 Tools
Inside SQL Server 2005 Tools
ISBN: 0321397967
EAN: 2147483647
Year: 2006
Pages: 149

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net