Skip to content

Instantly share code, notes, and snippets.

@jyurek
Last active July 21, 2023 07:32
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jyurek/c18149980e2e48baa96c072beaa08981 to your computer and use it in GitHub Desktop.
Save jyurek/c18149980e2e48baa96c072beaa08981 to your computer and use it in GitHub Desktop.
Insert lots of rows to test indices
require 'json'
ingredients = %w[
Ham
Water
Sodium\ Bicarbonate
Acetic\ Acid
Salt
Natural\ Flavors
Artificial\ Flavors
Onion
Milk
]
food_adjectives = %w[
Awesome
Party\ Size
Bubbling
Groovy
Extreme
Cheese
New
Red
Mountain
Whole\ Grain
]
food_nouns = %w[
Crisps
Ham
Ghosts
Popplers
Gummies
Dew
Salsa
Cheese
Deliciousness
Experiments
]
f = File.open('out.sql', 'w')
1_000_000.times do |i|
name = [food_adjectives.sample, food_adjectives.sample, food_nouns.sample]
.join(' ')
upc = format('%0.10i', rand(1_000_000_000))
recipe = (0..(rand(5)))
.to_a
.map { { name: ingredients.sample, mg: (rand 1000) + 1 } }
.to_json
f.puts(<<-end_sql)
INSERT INTO foods (name, upc, ingredients)
VALUES ('#{name}', '#{upc}', '#{recipe}');
end_sql
puts i
end
require 'json'
ingredients = %w[
Ham
Water
Sodium\ Bicarbonate
Acetic\ Acid
Salt
Natural\ Flavors
Artificial\ Flavors
Onion
Milk
]
food_adjectives = %w[
Awesome
Party\ Size
Bubbling
Groovy
Extreme
Cheese
New
Red
Mountain
Whole\ Grain
]
food_nouns = %w[
Crisps
Ham
Ghosts
Popplers
Gummies
Dew
Salsa
Cheese
Deliciousness
Experiments
]
f = File.open('out.sql', 'w')
1_000_000.times do |i|
name = [food_adjectives.sample, food_adjectives.sample, food_nouns.sample]
.join(' ')
upc = format('%0.10i', rand(1_000_000_000))
recipe = (0..(rand(5)))
.to_a
.map { { name: ingredients.sample, mg: (rand 1000) + 1 } }
.map(&:to_json)
.map { |s| "'#{s}'" }
.join(',')
f.puts(<<-end_sql)
INSERT INTO foods (name, upc, ingredients)
VALUES ('#{name}', '#{upc}', ARRAY[#{recipe}]::jsonb[]);
end_sql
puts i
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment