ActiveRecord - find MySQL connection/thread ID for query

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

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,

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.

Michaël 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,

> 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