# \[ANN\] inspect\_sql

**URL:** <https://www.ruby-forum.com/t/ann-inspect-sql/147921>\
**Category:** Rails\
**Created:** [September 7, 2008, 3:07am UTC](https://www.ruby-forum.com/t/ann-inspect-sql/147921 "2008-09-07T03:07:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [September 7, 2008, 3:07am UTC](https://www.ruby-forum.com/t/ann-inspect-sql/147921/1 "2008-09-07T03:07:52Z")

</div>

Railsters:

When you use ActiveRecord, it conglomerates your has\_many directives  
together to  
produce elaborate queries.

Sometimes these might be a little too elaborate.

I wrote a test helper function that lets you peek under the hood, like  
this:

```
 sql = inspect_sql do
   Post.find(:all, :include => { :user => :user_level },
                :conditions => { :'user_levels.name' => 'Moderator' 

```

})  
end

pp sql.statements

The method inspect\_sql{} returns an array object full of data about your  
queries. It comes with accessors (.statements, .keys, .tables, [2],  
etc.) that  
let you drill down to specifics. The trace statement ‘pp sql.statements’  
will  
emit all the SQL SELECT statements that inspect\_sql collected.

To optimize those statements, you can use assert\_no\_match (or assert{  
statement  
!~ /something/ } ) to catch and forbid inefficient or incorrect  
statements.

This helps you tune your database without writing brute-force tests that  
simply  
load thousands of records, query them, and time the results.

Get inspect\_sql with the assert\_efficient\_sql gem, and read more about  
it here:

[O'Reilly Media - Technology and Business Training](http://www.oreillynet.com/ruby/blog/2008/09/inspect_sql.html)

–  
Phlip

---

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [September 22, 2008, 6:25pm UTC](https://www.ruby-forum.com/t/ann-inspect-sql/147921/2 "2008-09-22T18:25:22Z")

</div>

> The method inspect\_sql{} returns an array object full of data about your  
> queries. It comes with accessors (.statements, .keys, .tables, [2],  
> etc.) that  
> let you drill down to specifics. The trace statement ‘pp sql.statements’  
> will  
> emit all the SQL SELECT statements that inspect\_sql collected.

adding sql explain might be nice, too 😛  
-=R

---

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [September 22, 2008, 8:21pm UTC](https://www.ruby-forum.com/t/ann-inspect-sql/147921/3 "2008-09-22T20:21:00Z")

</div>

On Sep 7, 9:07 am, Phlip [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Get inspect\_sql with the assert\_efficient\_sql gem, and read more about it here:
> 
> [O'Reilly Media - Technology and Business Training](http://www.oreillynet.com/ruby/blog/2008/09/inspect_sql.html)

Will this work with db’s other than MySQL?
