In Ruby, I have a test that's a subclass of ActiveSupport::TestCase
and access a table through an ActiveRecord
subclass in a Postgres (actually Greenplum) database. For a particular test I need to populate the table with on the order of a million rows, but I don't really care what's in there. I could do something like
for i in 1...1000000 do
MyTable.create(:column1 => 'value', :column2 => 'value')
end
but this will take a long time to run. I could make it a little faster by wrapping it in a transaction so that create
won't create a new one every time, but that will only save so much time.
Is there some nice way to do this so that I don't have to execute a lot of inserts of bogus values into the table?
(Note: stubbing things out to try to pretend that the table contains a million records won't work because I later on need to interact with the actual rows; for this particular test I just don't care what they are)
Run all that in a simple query like:
INSERT INTO mytable (col1, col2)
SELECT 'value1', 'value2'
FROM generate_series(1,1000000);
This should populate database pretty fast. It takes 2 seconds my laptop.