Skip to content

Instantly share code, notes, and snippets.

@TheKidCoder
Created March 10, 2016 18:50
Show Gist options
  • Save TheKidCoder/472a19ddb0d34e48710d to your computer and use it in GitHub Desktop.
Save TheKidCoder/472a19ddb0d34e48710d to your computer and use it in GitHub Desktop.
PostgreSQL load as JSON
require 'benchmark'
result = Benchmark.bm(7) do |x|
x.report("Arrays of JSON") do
ActiveRecord::Base.connection.execute(%{select row_to_json(prompts) as json from prompts order by id asc;}).to_a.each do |prompt|
Oj.load(prompt['json'])
end
end
x.report("Single JSON Object") do
Oj.load(ActiveRecord::Base.connection.execute(%{
select array_to_json(array_agg(row_to_json(t)))
from (
select id, parent_id, name, type, position, properties, rule_books, rule_books
from prompts
order by id asc
) t;
}).to_a.first['array_to_json']);
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment