Skip to content

Instantly share code, notes, and snippets.

@tameem92
Created December 26, 2019 18:11
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 tameem92/08579a3d2ac690fce395c21bedab4f11 to your computer and use it in GitHub Desktop.
Save tameem92/08579a3d2ac690fce395c21bedab4f11 to your computer and use it in GitHub Desktop.
# enable slow query logs in Rails
ActiveRecord::Base.logger = Logger.new(STDOUT)
# Get rails to show you the sql that will run
User.where(id: 1).to_sql
=> "SELECT \"users\".* FROM \"users\" WHERE \"users\".\"id\" = 1"
# Get rails to explain the sql query for more detail
User.where(id: 1).explain
# The explain will tell you which index is being used, and what work happens in the back.
=> EXPLAIN for: SELECT "users".* FROM "users" WHERE "users"."id" = $1 [["id", 1]]
QUERY PLAN
--------------------------------------------------------------------------
Index Scan using users_pkey on users (cost=0.14..8.16 rows=1 width=962)
Index Cond: (id = '1'::bigint)
(2 rows)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment