Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

INSERT INTO SELECT - large amount of records

I want to insert records into a TempTable. Something like this:

insert into ##tempT
SELECT * FROM MyTable 

MyTable contains a large amount of records so the "insert into" takes a long time.

If I try to run:

SELECT COUNT(*) FROM ##tempT

it returns always "0" until all records from "MyTable" are inserted by INSERT INTO command.

How can I get a progress count that advise me how many records are in ##tempT?

I need to update a progress bar value while the SQL command is running.

Thank you.

like image 398
epi82 Avatar asked Mar 30 '12 08:03

epi82


People also ask

How can insert large number of records in SQL Server?

BULK INSERT statement BULK INSERT loads data from a data file into a table. This functionality is similar to that provided by the in option of the bcp command; however, the data file is read by the SQL Server process. For a description of the BULK INSERT syntax, see BULK INSERT (Transact-SQL).

Which method is used to insert many records?

INSERT-SELECT-UNION query to insert multiple records Thus, we can use INSERT-SELECT-UNION query to insert data into multiple rows of the table.

Why is insert into slower than SELECT into?

Because the 'INSERT … SELECT' inserts data into an existing table, it is slower and requires more resources due to the higher number of logical reads and greater transaction log usage. However, providing the query hint to lock the entire destination table, the two statements perform exactly the same.


2 Answers

try

set transaction isolation level read uncommitted
SELECT COUNT(*) FROM ##tempT
like image 63
Diego Avatar answered Oct 18 '22 14:10

Diego


You can split your query up.

x = number of records in MyTable / 100
i = 0

do until we're done
    queryString = "insert into ##tempT "
    queryString += "select top " + x " + " * FROM MyTable "
    queryString += "where RecordNumber > " + i

    Execute queryString
    Update Progress Bar
    i = i + x
loop

You'll notice that you'll need some sort of RecordNumber field to make this work though. There are various ways to accomplish that you can search for.

like image 36
Brandon Moore Avatar answered Oct 18 '22 14:10

Brandon Moore