Skip to content

Instantly share code, notes, and snippets.

@djquan
Created March 14, 2015 04:30
Show Gist options
  • Save djquan/4cafa59ff11b4318d1ac to your computer and use it in GitHub Desktop.
Save djquan/4cafa59ff11b4318d1ac to your computer and use it in GitHub Desktop.
Cassandra Prepared Statements
require 'cassandra'
require 'benchmark/ips'
cluster = Cassandra.cluster(hosts: ["192.168.59.103"])
session = cluster.connect
session.execute("CREATE KEYSPACE benchmark WITH REPLICATION = { 'class': 'SimpleStrategy', 'replication_factor': 2 }")
session.execute("USE benchmark")
session.execute("CREATE table reads (test_one text PRIMARY KEY, test_two text)")
5_000.times do |i|
session.execute("INSERT INTO reads (test_one, test_two) VALUES ('#{i}', 'benchmark')")
end
Benchmark.ips do |x|
x.time = 60
statement = session.prepare("SELECT * FROM reads where test_one = ?")
x.report("Without a prepared statement") do
100.times { |i| session.execute("SELECT * FROM reads WHERE test_one = '#{i}'") }
end
x.report("With a prepared statement") do
100.times { |i| session.execute(statement, arguments: ["'#{i}'"])}
end
x.compare!
end
session.execute("DROP KEYSPACE benchmark")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment