# ActiveRecord - find MySQL connection/thread ID for query

**URL:** <https://www.ruby-forum.com/t/activerecord-find-mysql-connection-thread-id-for-query/192638>\
**Category:** Rails\
**Created:** [August 3, 2010, 2:18pm UTC](https://www.ruby-forum.com/t/activerecord-find-mysql-connection-thread-id-for-query/192638 "2010-08-03T14:18:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![MichaSSSSl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de0a6/32.png) [@MichaSSSSl](https://www.ruby-forum.com/u/MichaSSSSl)\
**Post date:** [August 3, 2010, 2:18pm UTC](https://www.ruby-forum.com/t/activerecord-find-mysql-connection-thread-id-for-query/192638/1 "2010-08-03T14:18:21Z")

</div>

Hi,

Is it possible, when using a find method with a Model, to record the  
MySQL thread ID for the subsequent database query? Does ActiveRecord  
expose this?

Many thanks,  
Michaël

---

<div class="post-metadata">

**Author:** ![MichaSSSSl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de0a6/32.png) [@MichaSSSSl](https://www.ruby-forum.com/u/MichaSSSSl)\
**Post date:** [August 3, 2010, 2:24pm UTC](https://www.ruby-forum.com/t/activerecord-find-mysql-connection-thread-id-for-query/192638/2 "2010-08-03T14:24:58Z")

</div>

MichaÃ«l wrote:

> Hi,
> 
> Is it possible, when using a find method with a Model, to record the  
> MySQL thread ID for the subsequent database query? Does ActiveRecord  
> expose this?

Why on earth would you need this? What are you trying to achieve?

> Many thanks,  
> Michaï¿½l

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

Sent from my iPhone

---

<div class="post-metadata">

**Author:** ![MichaSSSSl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de0a6/32.png) [@MichaSSSSl](https://www.ruby-forum.com/u/MichaSSSSl)\
**Post date:** [August 3, 2010, 3:09pm UTC](https://www.ruby-forum.com/t/activerecord-find-mysql-connection-thread-id-for-query/192638/3 "2010-08-03T15:09:17Z")

</div>

On Aug 3, 1:24 pm, Marnen Laibow-Koser [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Many thanks,  
> > Micha l

Because the application I’m maintaining uses Model find() methods to  
run large queries on the database to produce reports. These queries  
can take upwards of 10 minutes due to some complex table joins. While  
I’m altering the way these are working so they’re not taking a large  
amount of time, I need an interim solution where I monitor the  
database for any of these queries that are taking more than a set  
maximum time, killing the thread of those queries that are going over  
the threshold and recording which query was killed so it can be re-run  
at a quieter time. It would be really useful if, when one of these  
large finds is run, that I could record the thread ID it creates so if  
I need to kill a thread, I can match it back up to the originating  
find() call.

---

<div class="post-metadata">

**Author:** ![MichaSSSSl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de0a6/32.png) [@MichaSSSSl](https://www.ruby-forum.com/u/MichaSSSSl)\
**Post date:** [August 3, 2010, 5:23pm UTC](https://www.ruby-forum.com/t/activerecord-find-mysql-connection-thread-id-for-query/192638/4 "2010-08-03T17:23:41Z")

</div>

MichaÃ«l wrote:

> On Aug 3, 1:24ï¿½pm, Marnen Laibow-Koser [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > Many thanks,  
> > > Micha l

[Please quote what you’re replying to. It will make the discussion  
easier to follow.]

> Because the application I’m maintaining uses Model find() methods to  
> run large queries on the database to produce reports. These queries  
> can take upwards of 10 minutes due to some complex table joins. While  
> I’m altering the way these are working so they’re not taking a large  
> amount of time, I need an interim solution where I monitor the  
> database for any of these queries that are taking more than a set  
> maximum time, killing the thread of those queries that are going over  
> the threshold and recording which query was killed so it can be re-run  
> at a quieter time.

Your database should already do this. You don’t need to do it from  
Rails.

> It would be really useful if, when one of these  
> large finds is run, that I could record the thread ID it creates so if  
> I need to kill a thread, I can match it back up to the originating  
> find() call.

If the database kills a thread, can’t you just rescue the appropriate  
exception?

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![MichaSSSSl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de0a6/32.png) [@MichaSSSSl](https://www.ruby-forum.com/u/MichaSSSSl)\
**Post date:** [August 3, 2010, 5:35pm UTC](https://www.ruby-forum.com/t/activerecord-find-mysql-connection-thread-id-for-query/192638/5 "2010-08-03T17:35:03Z")

</div>

> > It would be really useful if, when one of these  
> > large finds is run, that I could record the thread ID it creates so if  
> > I need to kill a thread, I can match it back up to the originating  
> > find() call.
> 
> If the database kills a thread, can’t you just rescue the appropriate  
> exception?

That’s a good point. I’ll try catching the exception and storing the  
find parameters so I can run that query at time when I can afford to.

Thanks for the help,

Michaël
