Search code examples
c#.netsql-serversmo

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


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)
                {
                }                    
            }
        }

Solution

  • 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
        }
    }