Skip to content

Instantly share code, notes, and snippets.

@muneebaahmad
Created January 21, 2019 21:59
Show Gist options
  • Save muneebaahmad/f66e33808027058e585688f8262a986b to your computer and use it in GitHub Desktop.
Save muneebaahmad/f66e33808027058e585688f8262a986b to your computer and use it in GitHub Desktop.
# INSERT RECORDS INTO A DB
# Use case: To insert millions of records into a local db to test scripts.
# Testing scripts that affect millions of records on a remote db is very time consuming.
require('mysql2')
class Table
# @params cur_index [The id to start the insertion at]
# @params num_records [Number of records to insert starting at the id above]
def insert_records(cur_index, num_records)
end_index = cur_index + num_records
while cur_index < end_index do
client.query("INSERT into emails VALUES('#{cur_index}', 'address #{cur_index}')")
cur_index += 1
if cur_index % 100000 == 0
puts "At index: #{cur_index}"
end
end
end
def client
@client ||= Mysql2::Client.new(:host => "localhost", :username => "root", :database => "test_emails")
end
end
Table.new.insert_records(1,1000000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment