0

I've wrote a script that does work, but it's VERY slow (6 hours). I've been tasked with creating 10000 records, each with anywhere (randomly) up to 50 enditems, each with anywhere (randomly) up to another 50 relevant foreign key items, and then again, and again.

I think it's so very slow because of the iterative way I'm doing it.

Loop 10000
 insert into tble1()
 Loop 50
  insert into tble2 (select top(1) from tble1 desc)
  Loop 50
   insert into tble3 (select top(1) from tble2 desc)
    Loop 50
     insert into tble4 (select top(1) from tble3 desc)
   end
  end
 end 
end

Is kind of how it looks. I know sql works best with sets instead of iteration, but is there a way to do this with relationships without this loop setup I have?

3
  • That's 25 million rows I'd let it run for 6 hours overnight & use it the next day. Commented Dec 6, 2014 at 21:17
  • 1
    Actually it's 1.25 billion rows 6 hrs is pretty good Commented Dec 6, 2014 at 21:18
  • Yeah, I know I'm dealing with a lot of data, but is the method ive done this in way too slow? I'm really not sure how else I could do it but this. I'm pretty new to SQL, and developing in a set mindset instead of the traditional iterative process. Commented Dec 6, 2014 at 21:19

2 Answers 2

1

Which database brand?

If your db supports it, you should use Bulk Copy.

If you can't do Bulk Copy, try

insert into some_table (columns...) values
(some values...),
(some values...),
...
(some values...);

You can also disable auto-commit and only commit the transaction at the very end.

It will also be faster if you can drop/disable indexes, triggers, constraints, keys before the insert and re-enable them after.

You would want to insert into the foreign (referred to) tables first.

MS SQL Server: http://msdn.microsoft.com/en-us/library/ms188365.aspx

PostgreSQL: http://www.postgresql.org/docs/9.3/static/sql-copy.html

Oracle: https://docs.oracle.com/cd/B19306_01/server.102/b14215/ldr_concepts.htm

Sign up to request clarification or add additional context in comments.

2 Comments

I'm looking at bulk insert right now. And unless I'm reading the documentation wrong, I have to already have data created right? I have to randomly generate all this data. And as for your example, that's how I'm currently doing it. But dropping indexs might be an idea, I haven't tried that yet.
What lang is your script in? In JDBC+Postgres for example you can stream to database using CopyManager (which uses COPY internally). Or you could write to temp csv files
0

We'll notwithstanding that I think 6 hours is quite a short time to insert 1.25 billion rows you could try the following

Loop 10000
 Insert into table

   Insert into table (select 50 from temp table where Id like x)
   Loop 50
     Insert into table (select 50 from temptable2 where id > something else)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.