B
B
Bulat Kutliev2017-02-10 14:16:31
SQL
Bulat Kutliev, 2017-02-10 14:16:31

How to replace the cursor when inserting data into a table?

Good afternoon.
On MS SQL Server there is a plate, in it the data of the form

ID Parent Value
1 1 2.7
2 1 2.8
3 2 3.2
4 2 3.2
5 2 3.3
6 3 4.1

There should be three children of each parent, that is, it is necessary to determine where how much is missing, and so many to score with average values ​​​​(add one entry with Parent=1, Parent=2 is all right, add two entries with Parent=3).
How it is done now:
Parents are selected and shortages
Parent Lost
1 1
2 0
3 2

The records are traversed by the cursor, inside which WHILE is executed the necessary number of times INSERT
The problem is that in the original table there are ~ 20 million records and thousands of missing ones must be inserted, which results in indecent operation execution time.
And, in fact, the question is: how to replace the CURSOR + WHILE construction?
Or replace MSSQL with something else?
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
d-stream, 2017-02-10
@d-stream

Well, in some cases, it can be useful to replace the cursor with while, and while with something like insert from select or update "batch"
when there is a problem with volumes and there are prerequisites - then you can bulk insert and / or openrowset
, moreover, 10 inserts in 1 row slower than one insert 10 lines, and bulk is many times faster ... (recently played on a beech - something like a price list - almost a million lines ~ 5-7 sec). That is, in a particularly difficult situation, it may even be acceptable to chain sql-> csv-> bulk

B
Bulat Kutliev, 2017-02-15
@SlowRider

If suddenly anyone is interested:
the cursors are replaced by the following bunch - MSSQL select calculates the shortage table, then the c# application builds csv on this table (repeating the rows the required number of times), and then bulk insert drags it into the database (thanks, d-stream ).
The performance of a particular block has not yet been calculated, but the total processing time has dropped from six and a half hours to an hour and forty only due to the cursor.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question