2012-08-26 47 views
0

使用SMO,我遇到了一個很大的性能問題:我腳本中的所有數據庫對象(函數,用戶,角色,表...)以及腳本除了表以外的所有數據庫對象少於2分鐘,但是當我啓用表腳本時需要2個小時? 所以,我使用加載所有表格屬性:使用SMO編寫腳本表,解決性能問題

serverSQL.SetDefaultInitFields(typeof(SMO.Table), true); 

使SMO更高性能 但它似乎沒有任何效果。 這裏我的代碼:

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(); 
     } 

最後一個問題,因爲對我來說是使用SQL腳本,並直接創建表腳本但SSMS,我可以表腳本在幾分鐘內(SSMS腳本嚮導使用SMO或SQL。腳本?) 謝謝給我一個問題來解決它。

+0

Does this [this](http://www.codeproject.com/Questions/186062/smo-table -script-generation-performance-problem)對你來說意味着什麼? –

回答

0

我發現很多解決方法: 爲每個SMO.Table記錄所有腳本之後,我發現一些腳本需要少於1秒,其他需要約50秒!

  1. 所以第一解決方法是用multithreading.Doing工作,所以,讓我的腳本是用了2小時才能知道只有20分鐘的600%的速度。我嘗試使用基本的ThreadPool,而不是在測試多少個線程允許最佳性能時進行優化。我會在這之後對你說。 (我將使用不同數量的線程進行測試並將其記錄下來,以便查看我的計算機中提高最佳性能的線程數)

  2. 另一個解決方法是在codeplex中使用DBDiff,該源允許您更快地編寫腳本SMO,但對於那些想要更少代碼的人來說不是有用的(手動腳本;-)。您必須使用class來創建sql腳本。但該工具非常適合同步數據庫;-)

0

Riffing關閉格特·阿諾德的評論以上,這應該爲你做它:

public static string dumpTables(SMO.TableCollection tables) 
     { 
      SMO.ScriptingOptions scriptingOptions = new SMO.ScriptingOptions(); 
      scriptingOptions.IncludeIfNotExists = true; 
      scriptingOptions.DriAll = true; 
      scriptingOptions.ExtendedProperties = true; 

      SMO.Scripter scripter = new SMO.Scripter(); 
      scripter.Options = scriptingOptions; 

      return scripter.Script(tables); 
     } 

警告:我不是一個C#程序員(只是一個DBA),但Scripter.Script()方法採取一個SqlSmoObject [],這基本上是上面評論中的鏈接來提高性能。

+0

謝謝Ben Thul,但它是相同的結果,我在過去嘗試這個,但通過表集合,或枚舉所有骨灰甕前腳本具有相同的效果我找到一個解決方法 –