# 1. Open the Rails console.
rails c
# 2. Retrieve the ActiveRecord connection to the database.
connection = ActiveRecord::Base.connection.raw_connection
# 3. Enable timing of SQL statements.
connection.execute("SET profiling = 1;")
# 4. Run your ActiveRecord queries or any other database operations.
# Example:
# User.all
# Post.where(category: 'Ruby')
# 5. Disable timing and retrieve the profiling results.
results = connection.execute("SHOW PROFILES;")
# 6. Display the timing information for each SQL statement.
results.each do |result|
puts "Query ID: #{result['Query_ID']}, Duration: #{result['Duration']} seconds, Query: #{result['Query']}"
end
# 7. Optionally, you can also reset the profiling data.
connection.execute("SET profiling = 0;")