QueryReviewer is an advanced SQL query analyzer. It accomplishes the following goals:
- View all EXPLAIN output for all SELECT queries to generate a page
- Rate a page's SQL usage into one of three categories: OK, WARNING, CRITICAL
- Attach meaningful warnings to individual queries, and collections of queries
- Display interactive summary on page
I use this utility for most of my rails projects. Still the best out there in my opinion for analyzing and understanding your generated SQL queries. I forked the original query_reviewer and applied a collection of patches that have been made since the plugin was created. A list of the biggest additions below:
- Snazzed up the README into markdown for better readability
- Full compatibility for Rails 3 (including Railtie)
- Cleanup and move rake task to
lib/tasks
to fix deprecation warnings - Added gemspec for use with Bundler (as a gem)
- Fixed missing tags and additional XHTML escaping
- Fix SQL escaping for better XHTML compatibility
- Fixes for deprecation warnings and for 1.9 compatiblity
- Converts templates to more modern foo.html.erb naming
Last commit to the main repository was on March 30th, 2009. This fork compiles a variety of patches that were made since that time along with additional work to support compatibility with 1.9 and Rails 3. Also: If anyone else creates generally useful enhancements to this utility please start by forking this and then issue me a pull request.
Note: This plugin should work for Rails 2.X and Rails 3. Support for Rails 3 has been confirmed in the latest revision (with fixed deprecation warnings).
All you have to do is install it into your Rails 2 or 3 project.
Right now if you use bundler, simply add this to your Gemfile:
# Gemfile
gem "query_reviewer", :git => "git://github.com/nesquena/query_reviewer.git"
If you are not using bundler, you might want to start using it. You can also install this as a plugin:
script/plugin install git://github.com/nesquena/query_reviewer.git
In Rails 2, if the rake tasks are not loaded automatically (as a gem), you’ll need to add the following to your Rakefile:
# Rakefile
begin
require 'query_reviewer/tasks'
rescue LoadError
STDERR.puts "The query_reviewer gem could not be found!"
end
You can then run:
$ rake query_reviewer:setup
Which will create config/query_reviewer.yml
in your application, see below for what these options mean.
If you don't create a config file, the gem will use the default in vendor/plugins/query_reviewer
.
The configuration file allows you to set configuration parameters shared across all rails environment, as well as overriding those shared parameteres with environment-specific parameters (such as disabling analysis on production!)
enabled
: whether any output or query analysis is performed. Set this false in production!inject_view
: controls whether the output automatically is injected before the </body> in HTML output.profiling
: when enabled, runs the MySQL SET PROFILING=1 for queries longer than thewarn_duration_threshold
/ 2.0production_data
: whether the duration of a query should be taken into accountstack_trace_lines
: number of lines of call stack to include in the "short" version of the stack tracetrace_includes_vendor
: whether the "short" verison of the stack trace should include files in /vendortrace_includes_lib
: whether the "short" verison of the stack trace should include files in /libwarn_severity
: the severity of problem that merits "WARNING" statuscritical_severity
: the severity of problem that merits "CRITICAL" statuswarn_query_count
: the number of queries in a single request that merits "WARNING" statuscritical_query_count
: the number of queries in a single request that merits "CRITICAL" statuswarn_duration_threshold
: how long a query must take in seconds (float) before it's considered "WARNING"critical_duration_threshold
: how long a query must take in seconds (float) before it's considered "CRITICIAL"
If you disable the inject_view option above, you'll need to manually put the analyzer's output into your view:
# view.html.haml
= query_review_output
and that will display the analyzer view!
Random collection of resources that might be interesting related to this utility:
- http://blog.purifyapp.com/2010/06/15/optimise-your-mysql/
- http://www.tatvartha.com/2009/09/rails-optimizing-database-indexes-using-query_analyzer-and-query_reviewer/
- http://www.geekskillz.com/articles/using-indexes-to-improve-rails-performance
- http://www.williambharding.com/blog/rails/rails-mysql-indexes-step-1-in-pitiful-to-prime-performance/
- http://guides.rubyonrails.org/performance_testing.html
Other related gems that prove useful for database optimization:
There have been other alternatives created since this was originally released. A few of the best are listed below. I for one still prefer this utility over the other options:
Know of a better alternative? Let me know!
Created by Kongregate & David Stevenson. Refactorings and compilations of all fixes since was done by Nathan Esquenazi.
Copyright (c) 2007-2008 Kongregate & David Stevenson, released under the MIT license