2013-05-07 133 views
2

我有一個Windows腳本宿主腳本,它在我們的數據庫中執行存儲過程,並返回大約225列的大記錄集(列數取決於查詢會有所不同未來可能會變得更大)。處理大型記錄集時出現「內存不足」錯誤

var adUseServer = 2; 
var adUseClient = 3; 
var adOpenForwardOnly = 0; 
var adLockReadOnly = 1; 


var dbc = new ActiveXObject("ADODB.connection") 

dbc.connectionString = "Driver={SQL Server};Server=MYDBSERVER;Database=MYDB;uid=USER;Pwd=PASSWORD;" 
dbc.CursorLocation = adUseClient 
dbc.connectionTimeout = 3600 
dbc.commandTimeout = 3600 
dbc.open 

// Setup test table 
WScript.echo("Setting table..."); 
dbc.execute("IF OBJECT_ID(N'export_test', N'U') IS NOT NULL DROP TABLE export_test;;"); 
dbc.execute("create table export_test (channelID int)"); 

// Changing to 130 works (for me) 
for(var n = 1; n < 131; n++) 
{ 
    dbc.execute("alter table export_test ADD test_" + n + " NVARCHAR(4000) DEFAULT '0' not null"); 
} 



WScript.echo("Starting query..."); 

var rsData = serverQuery("Select * from export_test") 

WScript.echo("Starting loop..."); 

var count = 0; 
while(rsData.eof == 0) 
{ 
    WScript.echo(count++); 
    rsData.moveNext(); 
} 

WScript.echo("All done"); 



function serverQuery(sql) 
{ 
    var rsData = new ActiveXObject("ADODB.recordset"); 
    rsData.CursorLocation = adUseServer; 
    rsData.CursorType = adOpenForwardOnly; 
    rsData.LockType = adLockReadOnly; 
    rsData.MaxRecords = 1; // This makes no difference 
    rsData.open(sql, dbc); 

    return rsData; 
} 

當我使用客戶端遊標執行此查詢時,出現「內存不足」錯誤;如果我使用服務器遊標執行,則會收到「沒有足夠的存儲空間可用於完成此操作」錯誤。

我已經嘗試減少數據,所以沒有多少行正在返回,問題依然存在。

我打算嘗試'分頁'的結果,但我害怕做了這個工作後,它不會幫助,因爲我試圖減少返回的行,並沒有幫助,所以我認爲它可能是大量的列(這是動態的,可能會隨着時間的推移而越來越多)成爲這個問題的根本原因。我已經嘗試將數據庫日誌文件大小更改爲「不受限制的增長」,但它不會讓我繼續恢復爲「最大2GB大小」,但這是一個不同的問題(我認爲),因爲它目前無處可及。

有沒有人有任何進一步的想法或洞察到這一點。

謝謝。

回答

0

對,我基本上已經認定它是一個限制,所以我做的是創建一個COM對象來卸載這個工作並返回一個JSON或XML版本的結果數據,我可以在腳本中使用它。

對於任何人在乎這個,下面是C#代碼(道歉,如果它有點蹩腳,我也沒有一個代碼審查,作爲了我現在唯一DEV):

using System; 
using System.Runtime.InteropServices; 
using System.Text; 
using System.Data.SqlClient; 
using System.Xml; 

/* 
* To create strong name key for signing: "C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\sn" -k SQLInterchange_Key.snk" 
*/ 


namespace SQLInterchange 
{ 
    [Guid("4E97C259-80EC-40dc-8F7D-DB56BE9F123E")] 
    public interface ISQLInterchange 
    { 
    [DispId(1)] 
    bool Open(string databaseServer, string databaseName, string userID, string userPassword); 

    [DispId(2)] 
    void Close(); 

    [DispId(3)] 
    bool ExecuteRecordset(string selCommand); 

    [DispId(4)] 
    void CloseRecordset(); 

    [DispId(5)] 
    bool Execute(string selCommand); 

    [DispId(6)] 
    string GetJSONData(); 

    [DispId(7)] 
    string GetXMLData(string recordElementName, bool encoded); 
    } 


    // Events interface Database_COMObjectEvents 
    [Guid("31A125AA-81D5-495b-86E6-7A4B24B08BAA"), 
    InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] 
    public interface SQLInterchange_Events 
    { 
    } 


    [Guid("6B0B6A04-3BAF-4e14-9770-A0C10425E2CE"), 
    ClassInterface(ClassInterfaceType.None), 
    ComSourceInterfaces(typeof(SQLInterchange_Events))] 
    public class Connection : ISQLInterchange 
    { 
    private SqlConnection _connection = null; 
    private SqlDataReader _reader = null; 

    public Connection() 
    { 
    } 

    public bool Open(string databaseServer, string databaseName, string userID, string userPassword ) 
    { 
     // no need to throw as it throws a com compatible exception automatically 

     string myConnectString = "user id=" + userID + ";password=" + userPassword +";Database=" + databaseName + ";Server=" + databaseServer + ";Connect Timeout=30"; 
     _connection = new SqlConnection(myConnectString); 
     _connection.Open(); 

     return true; 
    } 

    public bool ExecuteRecordset(string selCommand) 
    { 
     if(_reader != null) 
     _reader.Close(); 

     SqlCommand myCommand = new SqlCommand(selCommand); 
     myCommand.Connection = _connection; 
     myCommand.CommandTimeout = 3600; 
     myCommand.ExecuteNonQuery(); 
     _reader = myCommand.ExecuteReader(); 
     return true; 
    } 


    public bool Execute(string selCommand) 
    { 
     if(_reader != null) 
     _reader.Close(); 

     SqlCommand myCommand = new SqlCommand(selCommand, _connection); 
     myCommand.CommandTimeout = 3600; 
     int rows = myCommand.ExecuteNonQuery(); 
     return true; 
    } 


    public void Close() 
    { 
     if(_connection != null) 
     _connection.Close(); 
    } 


    public void CloseRecordset() 
    { 
     if(_reader != null) 
     _reader.Close(); 
    } 


    public string GetJSONData() 
    { 
     StringBuilder sb = new StringBuilder(); 
     sb.Append("["); 

     if(_reader != null) 
     { 
     int count = _reader.FieldCount; 

     StringBuilder sbRecord = new StringBuilder(); 
     while(_reader.Read()) 
     { 

      if(sbRecord.Length > 0) 
      { 
      sbRecord.Append(","); 
      } 

      sbRecord.Append("{"); 

      // get the results of each column 
      for(int n = 0; n < count; n++) 
      { 
      string name = _reader.GetName(n); 
      string data = Convert.ToString(_reader[ n ]); 

      sbRecord.Append("\"" + _safeJSONElementName(name) + "\":\""); 

      sbRecord.Append(_safeJSON(data)); 

      sbRecord.Append("\""); 

      if(n + 1 < count) 
      { 
       sbRecord.Append(","); 
      } 
      } 

      sbRecord.Append("}"); 
     } 

     sb.Append(sbRecord.ToString()); 
     } 

     sb.Append("]"); 

     return sb.ToString(); 
    } 


    public string GetXMLData(string recordElementName, bool encoded) 
    { 
     _lt = "<"; 
     _gt = ">"; 

     if(encoded) 
     { 
     _lt = "&lt;"; 
     _gt = "&gt;"; 
     } 

     StringBuilder sb = new StringBuilder(); 

     if(_reader != null) 
     { 
     int count = _reader.FieldCount; 

     while(_reader.Read()) 
     { 
      _addXMLElement(sb, recordElementName, 1, true); 

      // get the results of each column 
      for(int n = 0; n < count; n++) 
      { 
      string name = _reader.GetName(n); 
      string data = Convert.ToString(_reader[ n ]); 

      _addXMLElement(sb, name, 2, false); 

      sb.Append(_escapeXML(data)); 

      _addXMLElement(sb, "/" + name, 0, true); 
      } 

      _addXMLElement(sb, "/" + recordElementName, 1, true); 
     } 
     } 

     return sb.ToString(); 
    } 


    private string _safeJSON(string s) 
    { 
     s = s.Replace("\n", "\\n"); 
     s = s.Replace("\r", "\\r"); 
     s = s.Replace("\t", "\\t"); 
     s = s.Replace("\"", "\\\""); 
     return s; 
    } 

    private string _safeJSONElementName(string s) 
    { 
     s = s.Replace(".", "_"); 
     s = s.Replace(" ", "_"); 
     return s; 
    } 


    private string _lt = "<"; 
    private string _gt = ">"; 

    private void _addXMLElement(StringBuilder sb, string s, int tabs, bool last) 
    { 
     for(int n = 0; n < tabs; n++) 
     { 
     sb.Append("\t"); 
     } 
     sb.Append(_lt); 
     sb.Append(s); 
     sb.Append(_gt); 
     if(last) sb.Append("\n"); 
    } 

    private string _escapeXML(string unescaped) 
    { 
     XmlDocument doc = new XmlDocument(); 
     var node = doc.CreateElement("root"); 
     node.InnerText = unescaped; 
     return node.InnerXml; 
    } 
    } 

} 
相關問題