Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6841533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:58:43+00:00 2026-05-26T23:58:43+00:00

I am developing a Rails app. I would like to use an array to

  • 0

I am developing a Rails app. I would like to use an array to hold 2,000,000 data, then insert the data into database like following:

large_data = Get_data_Method() #get 2,000,000 raw data

all_values = Array.new

large_data.each{ |data|

    all_values << data[1] #e.g. data[1] has the format "(2,'john','2002-09-12')"

}

sql="INSERT INTO cars (id,name,date) VALUES "+all_values.join(',')

ActiveRecord::Base.connection.execute(sql)

When I run the code, it takes a long long time at the point of large_data.each{...} . Actually I am now still waiting for it to finish(it has been running for 1 hour already still not finish the large_data.each{...} part).

Is it because of the number of elements is too large for the ruby array that the array can not hold 2,000,000 elements ? or ruby array can hold that much elements and it is reasonable to wait this long?

Since I would like to use bulk insertion in SQL to speed up the large data insertion time in mysql database, so I would like to use only one INSERT INTO statement, that’s why I did the above thing. If this is a bad design, can you recommand me a better way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:58:44+00:00Added an answer on May 26, 2026 at 11:58 pm

    Some notes:

    Don’t use the pattern “empty array + each + push”, use Enumerable#map.

    all_values = large_data.map { |data| data[1] }
    

    Is it possible to write get_data to return items lazily? if the answer is yes, check enumerators and use them to do batched inserts into the database instead of puting all objects at once. Something like this:

    def get_data
      Enumerator.new do |yielder|
        yielder.yield some_item
        yielder.yield another_item
        # yield all items.
      end
    end
    
    get_data.each_slice(1000) do |data|
      # insert those 1000 elements into the database
    end
    

    That said, there’re projects for doing efficient bulk insertions, check ar-extensions and activerecord-import for Rails >= 3.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing a Rails application and would like to understand when to use
I am developing a Rails app. after I execute a sql command like following:
I am developing in Rails an app where I would like to rank a
I'm developing a Rails 3 app deployed on Heroku which would like to optimize.
I'm trying to seed some data into a Rails 3 app that I'm developing,
I am developing an app using Rails and jQuery. I would like to submit
I'm developing a Rails app and I've run into an issue that I can't
I am developing a Rails app which should rely on existing database. There are
I use tail -f to display the log file when developing my Rails app.
I've been trying to fix a bug in the Rails app I'm developing, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.