Created
March 10, 2016 18:50
-
-
Save TheKidCoder/472a19ddb0d34e48710d to your computer and use it in GitHub Desktop.
PostgreSQL load as JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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