I'm a React, Javascript & Rails full-stack Software Engineer. I have been working remotely for the past two years in a remote village. Before joining as a Software Engineer I founded kwiq - a hyperlocal delivery startup to deliver things in remote villages.
2 minute read
We can run ActiveRecord::Relation#explain on relation. It runs EXPLAIN on the query or queries triggered by the relation
and returns the result as a string. The string is formatted imitating the ones printed by the database shell.
It used to analyze and display the SQL query execution plan generated by ActiveRecord for a particular query.
This method helps us to understand how the database will execute the query
and identify potential performance bottlenecks.
To use the explain method, we can append it to an ActiveRecord query chain.
The output will vary based on the database being used (e.g., MySQL, PostgreSQL, SQLite) since each database might have a different format for displaying query plans.
These options give us deeper insight into query performance
and enable us to customize the output
and obtain a more detailed query plan analysis.
Analyze
It provides detailed information about how the database executed the query, including actual runtime statistics. This includes details such as the number of rows processed, execution time,
and resource utilization.
Verbose
It provides a more detailed and verbose output compared to the regular “EXPLAIN” command. It includes extra information about each step in the query plan, statistics, cost estimates, making it especially useful for in-depth analysis
and understanding of query execution.
Summary
These analyze and verbose options serve as crucial tools for us, offering various levels of insight into query execution, aiding in performance optimizations,
and enabling informed decisions for improving database efficiency and speed.