Skip to content

Instantly share code, notes, and snippets.

@lucasrenan
Last active August 23, 2018 17:53
Show Gist options
  • Select an option

  • Save lucasrenan/ed11b3c80080ac3892c60c6a727a3ad2 to your computer and use it in GitHub Desktop.

Select an option

Save lucasrenan/ed11b3c80080ac3892c60c6a727a3ad2 to your computer and use it in GitHub Desktop.
Dumb postgres query comparison between int4/varchar

This is a complete naive comparison, don't take it for granted!

Docker for MAC: Version 18.06.0-ce-mac70 (26399)

Image: postgres:9.4.10-alpine

Results

Postgres did a sequence scan for both queries with and without indexes.

namespace :lucas do
task :run => :environment do |_, args|
100_000.times { |i|
puts Product.create!(name: SecureRandom.hex, status: 0, status2: 'active')
puts Product.create!(name: SecureRandom.hex, status: 1, status2: 'archived')
}
end
end
class Product < ApplicationRecord
enum status: { active: 0, archived: 1 }
#status2: [ :active, :archived ]
end
# == Schema Information
#
# Table name: products
#
# id :integer not null, primary key
# name :string
# status :integer
# status2 :string
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_products_on_status (status)
# index_products_on_status2 (status2)
#
EXPLAIN (ANALYZE, COSTS, VERBOSE, BUFFERS, FORMAT JSON) SELECT * FROM products WHERE status = 1;
EXPLAIN (ANALYZE, COSTS, VERBOSE, BUFFERS, FORMAT JSON) SELECT * FROM products WHERE status2 = 'active';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment