MSSQL uses linked servers for rapid data migration

Source: Internet
Author: User

In the work, encountered a task, need to migrate a database data to B database, two database data structure is the same. b database is already in a considerable amount of data, and therefore cannot be achieved by replicating the database.

Old method

When migrating data, the following methods are used at the outset:

(1) Use SSMs's "Generate and publish Scripts" feature to export data from a database to SQL scripts.

The script looks like this:

INSERT [dbo].[ Region]([ID],[region_id],[Region_name],[Region_code])VALUES(10649,0N'other'N'0000000')INSERT [dbo].[ Region]([ID],[region_id],[Region_name],[Region_code])VALUES(1,2N'Chengdu'N'1398098')INSERT [dbo].[ Region]([ID],[region_id],[Region_name],[Region_code])VALUES(2,2N'Chengdu'N'1360804')INSERT [dbo].[ Region]([ID],[region_id],[Region_name],[Region_code])VALUES(3,2N'Chengdu'N'1398198')INSERT [dbo].[ Region]([ID],[region_id],[Region_name],[Region_code])VALUES(4,2N'Chengdu'N'1398195')INSERT [dbo].[ Region]([ID],[region_id],[Region_name],[Region_code])VALUES(5,2N'Chengdu'N'1355114')INSERT [dbo].[ Region]([ID],[region_id],[Region_name],[Region_code])VALUES(6,2N'Chengdu'N'1390807')INSERT [dbo].[ Region]([ID],[region_id],[Region_name],[Region_code])VALUES(7,2N'Chengdu'N'1370804')INSERT [dbo].[ Region]([ID],[region_id],[Region_name],[Region_code])VALUES(8,2N'Chengdu'N'1398071')INSERT [dbo].[ Region]([ID],[region_id],[Region_name],[Region_code])VALUES(9,2N'Chengdu'N'1365808')

(2) Execute the generated SQL script on the B database.

Problems and difficulties

There is no problem in dealing with thousands of data in this way, but when the data reaches 100,000, the speed is really slow.

New method

Through exploration, a new method has been created.

(1) using a linked server, set up a linked server on the database server where the B database resides, pointing to a database. In this way, a database can be directly manipulated in the B database.

(2) Use the INSERT INTO () Select form statement to directly insert the data found in the a database into the B database.

Change the script above to resemble the following:

1 INSERT   into [dbo].[ Region]2([ID] ,3           [region_id] ,4           [Region_name] ,5           [Region_code]6         )7         SELECT  [ID] ,8                 [bundling] ,9                 [region_id] ,Ten                 [Region_name] , One                 [Region_code] A          from    [Xianjie].[A].[dbo].[ Region]

The speed of migration has been increased by more than a hundredfold. Ha ha

MSSQL uses linked servers for rapid data migration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.