PostgreSQL Query Analyzer for Rails

Provides a PostgreSQL query analysis in your development logs.

Installation

gem install pg_query_analyzer

# config/enviroments/development.rb

config.gem "pg_query_analyzer"

Example

Analyzing Car Load Execution Time:

 Hash Join  (cost=8.01..21.90 rows=6 width=2699)
 Hash Cond: (cars.group_id = group.id)
 ->  Seq Scan on cars  (cost=0.00..12.80 rows=280 width=2699)
 ->  Hash  (cost=8.00..8.00 rows=1 width=8)
     ->  Hash Join  (cost=3.90..8.00 rows=1 width=8)
           Hash Cond: (groups.id = contracts.group_id)
           ->  Seq Scan on groups  (cost=0.00..3.79 rows=79 width=4)
           ->  Hash  (cost=3.89..3.89 rows=1 width=4)
                 ->  Seq Scan on contracts  (cost=0.00..3.89 rows=1 width=4)
                       Filter: (user_id = 8)

Options

ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.explain_analyze = true

Use ANALYZE to carry out the command and show the actual run times.

ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.explain_verbose = true

Use VERBOSE to show the full internal representation of the plan tree, rather than a summary.

Credits

MODIFIED by John Eberly originally take from svn.nfectio.us PostgreSQL and Oracle Adapter from Luca Mearelli spazidigitali.com Cooked and gemified by Marcos Piccinini github.com/nofxx/query-analyzer PostgreSQL Only Version from Trevor Turk github.com/trevorturk/pg_query_analyzer

MIT License

Spatial Adapter Copyright © 2006 Guilhem Vellut <[email protected]> PostGis Adapter Functions © 2008 Marcos Piccinini <nofxx> PostgreSQL Only Version © 2010 Trevor Turk <trevorturk>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.