Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 8316950

Browse files
author
Nick Gauthier
committed
added unique index benchmarks
1 parent ba82807 commit 8316950

File tree

4 files changed

+89
-32
lines changed

4 files changed

+89
-32
lines changed

presentation/shared/goals.md

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,5 @@
22
# Goal
33
## you don't know what you don't know
44

5-
!SLIDE
6-
# Goal
7-
## awareness > comprehension
8-
9-
!SLIDE
10-
# Goal
11-
## fire hydrant hydrant
12-
### you don't have to drink it all, my goal is to soak you
13-
145
!SLIDE
156
# Make your life EASIER

src/chapter-1/basic-index.sql.rb

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
11
#!/usr/bin/env ruby
2-
require 'rubygems'
3-
require 'active_record'
4-
5-
def bench
6-
start = Time.now
7-
yield
8-
finish = Time.now
9-
finish - start
10-
end
11-
12-
def sql(query)
13-
ActiveRecord::Base.connection.execute(query)
14-
end
2+
require File.join(File.dirname(__FILE__), '..', 'helper.rb')
153

164
text_lines = File.read(File.join(File.dirname(__FILE__), '..', '..', 'data', 'romeo_and_juliet.txt')).split("\n")
175

@@ -22,32 +10,23 @@ def sql(query)
2210

2311
total_lines = data.size
2412

25-
ActiveRecord::Base.establish_connection({
26-
:database => 'knowsql_examples', :adapter => 'postgresql'
27-
})
2813

2914
sql %{
3015
drop table if exists romeo_and_juliet_lines;
3116
32-
drop sequence romeo_and_juliet_lines_id_seq;
33-
create sequence romeo_and_juliet_lines_id_seq;
34-
3517
create table romeo_and_juliet_lines (
36-
id integer primary key default nextval('romeo_and_juliet_lines_id_seq'),
3718
line_no integer,
3819
contents varchar(256)
3920
);
4021
}
4122

42-
class RomeoAndJulietLine < ActiveRecord::Base; end
43-
4423
data.each do |row|
4524
sql %{insert into romeo_and_juliet_lines (line_no, contents) values (
4625
#{row[:line]}, #{ActiveRecord::Base.connection.quote(row[:content])}
4726
);}
4827
end
4928

50-
puts "Inserted #{RomeoAndJulietLine.count} lines of Romeo and Juliet"
29+
puts "Inserted Romeo and Juliet"
5130

5231
puts "Performing 1000 finds without an index"
5332
without_index = bench do

src/chapter-1/unique-index.sql.rb

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env ruby
2+
require File.join(File.dirname(__FILE__), '..', 'helper.rb')
3+
4+
sql %{
5+
drop table if exists random_numbers;
6+
drop sequence if exists random_numbers_id_seq;
7+
8+
create sequence random_numbers_id_seq;
9+
create table random_numbers (
10+
id integer primary key default nextval('random_numbers_id_seq'),
11+
number integer not null
12+
);
13+
}
14+
15+
class RandomNumber < ActiveRecord::Base
16+
end
17+
18+
10000.times do |i|
19+
sql %{insert into random_numbers (number) values (#{i.to_s});}
20+
end
21+
22+
pg_duplicates = bench do
23+
1000.times do |i|
24+
begin
25+
sql %{insert into random_numbers values (#{i.to_s});}
26+
rescue ActiveRecord::StatementInvalid
27+
end
28+
end
29+
end
30+
31+
ar_duplicates = bench do
32+
1000.times do |i|
33+
begin
34+
RandomNumber.create(:number => i)
35+
rescue ActiveRecord::StatementInvalid
36+
end
37+
end
38+
end
39+
40+
41+
sql %{
42+
delete from random_numbers;
43+
create unique index random_numbers_number_idx on random_numbers(number);
44+
}
45+
46+
pg_duplicates_unique = bench do
47+
1000.times do |i|
48+
begin
49+
sql %{insert into random_numbers values (#{i.to_s});}
50+
rescue ActiveRecord::StatementInvalid
51+
end
52+
end
53+
end
54+
55+
class RandomNumberUnique < ActiveRecord::Base
56+
set_table_name 'random_numbers'
57+
validates_uniqueness_of :number
58+
end
59+
60+
ar_duplicates_unique = bench do
61+
1000.times do |i|
62+
RandomNumberUnique.create(:number => i)
63+
end
64+
end
65+
66+
puts "PG: #{pg_duplicates}"
67+
puts "PU: #{pg_duplicates_unique}"
68+
puts "AR: #{ar_duplicates}"
69+
puts "AU: #{ar_duplicates_unique}"

src/helper.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
require 'rubygems'
2+
require 'active_record'
3+
4+
def bench
5+
start = Time.now
6+
yield
7+
finish = Time.now
8+
finish - start
9+
end
10+
11+
def sql(query)
12+
ActiveRecord::Base.connection.execute(query)
13+
end
14+
15+
ActiveRecord::Base.establish_connection({
16+
:database => 'knowsql_examples', :adapter => 'postgresql'
17+
})
18+

0 commit comments

Comments
 (0)