search
HomeDatabaseMysql Tutorial [转]不同版本的SQL Server之间数据导出导入的方法及性能比较

工作中有段时间常常涉及到不同版本的数据库间导出导入数据的问题,索性整理一下,并简单比较下性能,有所遗漏的方法也欢迎讨论、补充。 00.建立测试环境 01.使用SQL Server Import and Export Tool 02.使用Generate Scripts 03.使用BCP 04.使用SqlBulkCopy 0

工作中有段时间常常涉及到不同版本的数据库间导出导入数据的问题,索性整理一下,并简单比较下性能,有所遗漏的方法也欢迎讨论、补充。

00.建立测试环境

01.使用SQL Server Import and Export Tool

02.使用Generate Scripts

03.使用BCP

04.使用SqlBulkCopy

05.使用Linked Server进行数据迁移

06.使用RedGate的SQL Data Compare

07.结果对比

可以先看下测试的结果

 

 

 

00.建立测试环境

建立一个测试的环境,一个数据源数据库,香港服务器,版本为SQL Server 2008,一个目标数据库,版本为SQL Server 2000。

实验环境如下图所示,源数据库使用语句生成了100万的测试数据。

 

建立测试表并生成100万的测试数据

IF OBJECT_ID() IS NOT NULL DROP TABLE DEMOTABLE GO CREATE TABLE DEMOTABLE ( COL1 VARCHAR(50) , COL2 VARCHAR(50) , COL3 VARCHAR(50) ) INSERT INTO DEMOTABLE SELECT TOP 1000000 NEWID() , NEWID() , NEWID() FROM MASTER..SPT_VALUES T1 INNER JOIN MASTER..SPT_VALUES T2 ON 1 = 1 INNER JOIN MASTER..SPT_VALUES T3 ON 1 = 1

 

 01.使用SQL Server Import and Export Tool

使用SQL Server Import and Export Tool进行数据的导出,也可以在目标数据库端使用Import进行导入,这部分套件也是SSIS的一部分。

在源数据库上右键,选择Task -> Export Data

分别填写源数据库和目标数据库的连接信息。

 

 

选择“copy data from one or more tables or views”

选择需要导数据的表,并且可以编辑列的Mapping关系。

可以选择立即执行或者存储为SSIS的包,用于执行计划等其他用途。

这里我们选择立即执行。

注意导入的时候如果遇到如下的错误

Error 0xc02020f4: Data Flow Task: The column "Tel" cannot be processed because more than one code page (936 and 1252) are specified for it.
(SQL Server Import and Export Wizard)

是因为两边的数据库的Collation设置不一样造成的,需要设置同样的Collation。

02.使用Generate Scripts生成脚本

在源数据库上右键,选择Task -> Geneate Scripts...

配置相关信息,注意选择数据库的版本并将Script Data设置成True。

这里需要注意,美国空间,因为有100万的数据,香港空间,所以导出的SQL文件就有400多M,所以用SQL Server Management Studio是打不开的。

所以只能使用sqlcmd执行。

sqlcmd语句 

C:\>sqlcmd -i export.sql -d ExportDataDemo_Destination -s 192.168.21.165 -U sa -P 1234567890

用时约28分钟

 

 03.使用BCP进行导出导入

在尝试了前面两个效率低下的工具之后,我们终于开始尝试下SQL Server中专门用于导数据的工具:BCP。

关于BCP的详细用法可以参见MSDN的帮助文档。

我们先使用BCP导出数据。

-U和-P后面分别为数据库的用户名和密码。

我们可以看到100万的数据导出仅用了1.8秒。

现在我们再使用BCP进行导入。

执行后发现,导入数据使用了20.8秒,还是很快的。

  • 用时1.872秒+20.810秒=22.682秒
  •  

  •  04.使用SqlBulkCopy

    .NET Framework 2.0中增加的SqlBulkCopy类可以进行高效的数据迁移动作,这也为代码实现数据迁移提供了接口。

    并且SqlBulkCopy类提供了修改字段Mapping关系的方法ColumnMappings。

     使用SqlBulkCopy类进行数据迁移

    Statement
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    What Are the Limitations of Using Views in MySQL?What Are the Limitations of Using Views in MySQL?May 14, 2025 am 12:10 AM

    MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

    Securing Your MySQL Database: Adding Users and Granting PrivilegesSecuring Your MySQL Database: Adding Users and Granting PrivilegesMay 14, 2025 am 12:09 AM

    ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

    What Factors Influence the Number of Triggers I Can Use in MySQL?What Factors Influence the Number of Triggers I Can Use in MySQL?May 14, 2025 am 12:08 AM

    MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

    MySQL: Is it safe to store BLOB?MySQL: Is it safe to store BLOB?May 14, 2025 am 12:07 AM

    Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

    MySQL: Adding a user through a PHP web interfaceMySQL: Adding a user through a PHP web interfaceMay 14, 2025 am 12:04 AM

    Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

    MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

    MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

    MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

    ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

    MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

    ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Article

    Hot Tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Safe Exam Browser

    Safe Exam Browser

    Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

    SublimeText3 English version

    SublimeText3 English version

    Recommended: Win version, supports code prompts!

    PhpStorm Mac version

    PhpStorm Mac version

    The latest (2018.2.1) professional PHP integrated development tool