2013-03-21 120 views
3

我使用這個例子將數據從SQL Server導出到PostgreSQL,當我開始導出時,由於300,000行需要12分鐘,我可以做些什麼來加速這個過程,或者你知道另一種方式去做吧?將數據從SQL Server導出到PostgreSQL

string SourceDriver = "Driver={SQL Server Native Client 10.0}"; 
OdbcConnection SourceConnection = new OdbcConnection(SourceDriver+ ";Server=10.10.10.10;Database=sourceMSSQL;Uid=sa;Pwd=12345;"); 

string DestDriver = "Driver={PostgreSQL}"; 
OdbcConnection DestConnection = new OdbcConnection(DestDriver+ ";Server=10.10.10.11;Port=5432;Database=destPostgreSQL;Uid=postgres;Pwd=12345;"); 

string SourceSql = "SELECT Code, Label, Model, List, Size, Quantity, City, Family, ExportDate FROM MovPedidosP0"; 
string DestSql = "INSERT INTO tmp_MovPedidosP0_t (Code, Label, Model, List, Size, Quantity, City, Family, ExportDate) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)"; 

using(OdbcCommand SourceCommand = new OdbcCommand(SourceSql, SourceConnection)) 
{ 
    SourceConnection.Open(); 
    using(OdbcDataReader SourceReader = SourceCommand.ExecuteReader()) 
    { 
     Console.WriteLine("Exporting..."); 

     DestConnection.Open(); 

     while(SourceReader.Read()) 
     { 
      using(OdbcCommand DestCommand = new OdbcCommand(DestSql, DestConnection)) 
      { 
       DestCommand.Prepare(); 
       DestCommand.Parameters.Clear(); 

       for(int i=0; i<SourceReader.FieldCount; i++) 
       { 
        DestCommand.Parameters.AddWithValue("?ID" + (i+1).ToString(), SourceReader[i]); 
       } 

       DestCommand.ExecuteNonQuery(); 
       TotalRows++; 
      } 
     } 

     DestConnection.Close(); 
    } 
} 

SourceConnection.Close(); 
+0

您可能會考慮PostgreSql的批處理:http://stackoverflow.com/questions/758945/whats-the-fastest-way-to-do-a-bulk-insert-into-postgres – NotMe 2013-03-21 23:07:02

回答

2

更簡單,如果您導出到使用SSIS和導入與COPY命令的文本文件可能更快。

+0

甚至使用SSIS直接複製到PostgresSQL數據庫。 – 2013-03-21 23:27:38

+0

我會使用SSIS和COPY,但我需要通過應用程序導出信息,並且是將使用它的最終用戶。 – 2013-03-21 23:53:08

相關問題