Skip to content

Commit 1207128

Browse files
committed
Added indexes to allow for User.with_username queries to avoid seq scans
1 parent cb44b1a commit 1207128

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class CreateCaseInsensitiveIndexesOnUser < ActiveRecord::Migration
2+
3+
# User.with_username looks up on following fields almost
4+
# constantly but with a UPPER(fieldname) = UPPER(val)
5+
# which is nasty and slow, add upcase and downcase indexes
6+
# to avoid the problem
7+
8+
def up
9+
execute 'create index ix_users_github_lower on users (lower(github) varchar_pattern_ops)'
10+
execute 'create index ix_users_github_upper on users (upper(github) varchar_pattern_ops)'
11+
execute 'create index ix_users_linkedin_lower on users (lower(linkedin) varchar_pattern_ops)'
12+
execute 'create index ix_users_linkedin_upper on users (upper(linkedin) varchar_pattern_ops)'
13+
execute 'create index ix_users_twitter_lower on users (lower(twitter) varchar_pattern_ops)'
14+
execute 'create index ix_users_twitter_upper on users (upper(twitter) varchar_pattern_ops)'
15+
execute 'create index ix_users_username_lower on users (lower(username) varchar_pattern_ops)'
16+
execute 'create index ix_users_username_upper on users (upper(username) varchar_pattern_ops)'
17+
end
18+
19+
def down
20+
execute 'drop index if exists ix_users_github_lower'
21+
execute 'drop index if exists ix_users_github_upper'
22+
execute 'drop index if exists ix_users_linkedin_lower'
23+
execute 'drop index if exists ix_users_linkedin_upper'
24+
execute 'drop index if exists ix_users_twitter_lower'
25+
execute 'drop index if exists ix_users_twitter_upper'
26+
execute 'drop index if exists ix_users_username_lower'
27+
execute 'drop index if exists ix_users_username_upper'
28+
end
29+
end

db/schema.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#
1212
# It's strongly recommended to check this file into your version control system.
1313

14-
ActiveRecord::Schema.define(:version => 20140701170008) do
14+
ActiveRecord::Schema.define(:version => 20140703223632) do
1515

1616
create_table "alias_tags", :id => false, :force => true do |t|
1717
t.integer "tag_id"

0 commit comments

Comments
 (0)