从SQL Server导出数据到PostgreSQL

3

我正在使用这个例子将数据从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();

你可以查看PostgreSql的批处理功能:https://dev59.com/EXRA5IYBdhLWcg3w_DAw - NotMe
2个回答

2

如果您使用SSIS导出到文本文件并使用COPY命令进行导入,这样做会更加简单且可能更快。


甚至可以使用SSIS直接复制到PostgresSQL数据库。 - John Saunders
我会使用SSIS和COPY,但我需要通过一个应用程序导出信息,并且是最终用户将使用它。 - Sergio Flores

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接