我使用這個例子將數據從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();
您可能會考慮PostgreSql的批處理:http://stackoverflow.com/questions/758945/whats-the-fastest-way-to-do-a-bulk-insert-into-postgres – NotMe 2013-03-21 23:07:02