With SMO, I meet a big performance problem : I script all Database object (functions, users, roles, table...) and when I script all Database object except table, it take less than 2 minutes, but when i enable table scripting it take 2 hours? So, i load all table properties using:
serverSQL.SetDefaultInitFields(typeof(SMO.Table), true);
to make SMO more performant but it seems to have no effect. here my code :
public static string dumpTables(SMO.TableCollection tables)
{
int cpt = 0;
SMO.ScriptingOptions scriptingOptions = new SMO.ScriptingOptions();
StringBuilder sb = new StringBuilder();
scriptingOptions.IncludeIfNotExists = true;
scriptingOptions.DriAll = true;
scriptingOptions.ExtendedProperties = true;
foreach (SMO.Table table in tables)
{
sb.Append("-- Table " + table.Name + "\n");
foreach (string scriptline in table.Script(scriptingOptions)) //Script call take a long time
{
sb.Append(scriptline + Environment.NewLine );
}
sb.Append("GO" + Environment.NewLine);
cpt++;
Console.WriteLine(string.Format("Table {0} : {1}", cpt.ToString(), table.ToString()));
}
return sb.ToString();
}
the last issue, as for me is to use sql script and directly create table script but with SSMS, I can make table script in few minutes.(SSMS Script wizard use SMO or sql script?) Thank to give me an issue to fix it.
I find many workaround: After logging all script one by one for each SMO.Table, I discover that some script take less than 1 second, other take about 50 second!
So a first workaround is to work with multithreading.Doing so, make my scripting that took 2 hours to take know only 20 minutes 600% faster. I try with a basic ThreadPool, without optimize it in testing how many thread allow the best performance. I will say you it after. (I will test using different number of thread and logged it in order to see the number of thread in my computer that improve best performance)
Another workaround, is to use DBDiff in codeplex which source allow you to script faster without SMO but not usefull for those who want less code (script manually ;-). You have to use class to create sql script.But the tool is very good to synchronise database;-)