Is there anyway to speed up SQL Server Management Objects traversal of a existing database?
Asked Answered
E

3

3

I'm currently using SMO and C# to traverse databases to create a tree of settings representing various aspects of the two databases, then comparing these trees to see where and how they are different.

The problem is, for 2 reasonably sized database, it takes almost 10mins to crawl them locally and collect table/column/stored procedure information I wish to compare.

Is there a better interface then SMO to access databases in such a fashion? I would like to not include any additional dependencies, but I'll take that pain for a 50% speed improvement. Below is a sample of how I'm enumerating tables and columns.

        Microsoft.SqlServer.Management.Smo.Database db = db_in;
        foreach (Table t in db.Tables)
        {
            if (t.IsSystemObject == false)
            {

                foreach (Column c in t.Columns)
                {
                }                    
            }
        }
Encumber answered 30/12, 2008 at 22:40 Comment(1)
One note, make sure to set you application to MTAThreadEncumber
H
8

Try to force SMO to read all the required fields at once, instead of querying on access. See this blog for more information


EDIT: Link is dead but I found the page on archive.org. Here's the relevant code:

Server server = new Server();

// Force IsSystemObject to be returned by default.
server.SetDefaultInitFields(typeof(StoredProcedure), "IsSystemObject");

StoredProcedureCollection storedProcedures = server.Databases["AdventureWorks"].StoredProcedures;

foreach (StoredProcedure sp in storedProcedures) {
    if (!sp.IsSystemObject) {
        // We only want user stored procedures
    }
}
Hessian answered 30/12, 2008 at 22:59 Comment(2)
This has been a significant speed up without requiring drastic change in approach like the above option, thanks.Encumber
What used to take 10 mins now executes in ~40secondsEncumber
U
2

Use the system views in each database and query conventionally.

http://www.microsoft.com/downloads/details.aspx?familyid=2EC9E842-40BE-4321-9B56-92FD3860FB32&displaylang=en

Unsnarl answered 30/12, 2008 at 22:52 Comment(0)
S
0

There is little that you can't get via TSQL queries. Getting metadata that way is usually very fast.

Symptomatology answered 30/12, 2008 at 23:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.