> User.where(id: User.select(:id)).select(:email).explain_analyze
https://explain.dalibo.com/plan/XXXXXXXXXXXXXX
Created
April 8, 2024 10:28
-
-
Save gmcabrita/35f4555b5560ce904cf9cf6ed4a7f0a2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/initializers/active_record.rb | |
Rails.application.reloader.to_prepare do | |
ActiveRecord::Relation.include(ExplainAnalyze::Relation) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lib/explain_analyze.rb | |
module ExplainAnalyze | |
module Relation | |
extend ActiveSupport::Concern | |
def explain_analyze | |
query = to_sql + ";" | |
explain = "EXPLAIN (ANALYZE, COSTS, VERBOSE, BUFFERS, FORMAT JSON) #{query}" | |
plan = connection.execute(explain) | |
.values | |
.first | |
.first | |
.gsub(/\s\s\s*/, "") # cleanup useless indentation | |
.delete("\n") # cleanup useless newlines | |
response = HTTP | |
.headers(content_type: "application/json") | |
.post( | |
"https://explain.dalibo.com/new", | |
json: { | |
plan: plan, | |
query: query, | |
title: Time.now.to_s | |
} | |
) | |
if response.status == 302 | |
puts "https://explain.dalibo.com" + response.headers["Location"] | |
else | |
raise "Failed to upload explain analyze and query successfully" | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Elixir version:
Usage: