Skip to content

Instantly share code, notes, and snippets.

@norman
Created July 8, 2010 15:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save norman/468126 to your computer and use it in GitHub Desktop.
Save norman/468126 to your computer and use it in GitHub Desktop.
require "rubygems"
require "pg"
require "rbench"
query = "SELECT * FROM people WHERE id = $1 LIMIT 1"
conn = PGconn.open(:dbname => "random_test")
stmt = conn.prepare "orm_load", query
RBench.run(100000) do
column :times
column :time
report 'Prepared statements' do
time {
id = rand 1_000_000
result = conn.exec_prepared("orm_load", [id])
# p result.field_values("name")[0]
}
end
report 'Direct SQL' do
time {
id = rand 1_000_000
result = conn.exec(query, [id])
# p result.field_values("name")[0]
}
end
end
# My results, using pg 0.9.0, Postgres 8.4.2, and Ruby 1.9.2-head,
# on a table with 2 fields and one million rows:
#
# | TIME |
# -----------------------------------------------
# Prepared statements x100000 | 11.099 |
# Direct SQL x100000 | 17.639 |
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment