[英]Exporting data from SQL Server to PostgreSQL
我正在使用此示例将数据从SQL Server导出到PostgreSQL,当我开始导出时,300,000行需要12分钟,我可以做些什么来加快这个过程,或者你知道另一种方法吗?
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();
如果使用SSIS导出到文本文件并使用COPY
命令导入,则更简单且可能更快。
尝试使用本机NpgsqlConnection和SqlConnection而不是Odbc连接。
http://npgsql.projects.pgfoundry.org/
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.