Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to improve the operation speed of insert

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "how to improve the operation speed of insert". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

We usually use insert statements, especially sometimes we need a large amount of data to do testing, an insert will be very slow, so how can we make our inser faster.

Let's look at an example:

We need to insert test data in the following table, which contains two columns, one is itemid and the other is itemname. If you insert 103680000 records into this table, the normal insertion method may take more than 20 days to complete, but it can be done in 5 hours with the new method described here.

Let's first look at the general data insertion method, assuming that we insert 100000 pieces of data into the table:

The copy code is as follows:

CREATE TABLE # tempTable ([Item ID] [bigint], [Item Name] nvarchar (30))

DECLARE @ counter int

SET @ counter = 1

WHILE (@ counter)

< 100000) BEGIN INSERT INTO #tempTable VALUES (@counter, 'Hammer') SET @counter = @counter + 1 END SELECT * FROM #tempTable DROP TABLE #tempTable 新的插入方法会使用已经插入的数据来进行下一条记录的操作,原理如下:

So look at my new insert code:

The copy code is as follows:

CREATE TABLE # tempTable ([Item ID] [bigint], [Item Name] nvarchar (30))

INSERT INTO # tempTable VALUES (1, 'Hammer')

WHILE ((SELECT COUNT (*) FROM # tempTable) < 100000)

BEGIN

INSERT INTO # tempTable ([Item ID], [Item Name])

(SELECT [Item ID] + (SELECT COUNT (*) FROM # tempTable), 'Hammer' FROM # tempTable)

END

SELECT * FROM # tempTable

DROP TABLE # tempTable

Using the first method may take dozens of minutes to insert 100000 data, but using the second method only takes 4 seconds. With further improvement, it will be completed in 2 seconds:

The copy code is as follows:

CREATE TABLE # tempTable ([Item ID] [bigint], [Item Name] nvarchar (30))

INSERT INTO # tempTable VALUES (1, 'Hammer')

DECLARE @ counter int

SET @ counter = 1

WHILE (@ counter)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report