# Simple system for asynchronous SQL queries

**URL:** <https://www.ruby-forum.com/t/simple-system-for-asynchronous-sql-queries/177551>\
**Category:** Rails\
**Created:** [October 20, 2009, 4:23am UTC](https://www.ruby-forum.com/t/simple-system-for-asynchronous-sql-queries/177551 "2009-10-20T04:23:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![newbee-robert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dbc845/32.png) [@newbee-robert](https://www.ruby-forum.com/u/newbee-robert)\
**Post date:** [October 20, 2009, 4:23am UTC](https://www.ruby-forum.com/t/simple-system-for-asynchronous-sql-queries/177551/1 "2009-10-20T04:23:11Z")

</div>

There has to be something like this… I have some tracking related SQL  
queries that are slow and don’t need to happen in the request cycle. Is  
it possible to dump them to some text file and then read them in a  
daemon and send them to MySQL?

What would be the simplest way of implementing this?

---

<div class="post-metadata">

**Author:** ![newbee-robert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dbc845/32.png) [@newbee-robert](https://www.ruby-forum.com/u/newbee-robert)\
**Post date:** [October 20, 2009, 4:26am UTC](https://www.ruby-forum.com/t/simple-system-for-asynchronous-sql-queries/177551/2 "2009-10-20T04:26:32Z")

</div>

Should note that I already have delayed\_job running, but it seems a bit  
silly to create a job (in the database) that just executes a SQL query,  
even if the SQL query is running take significantly longer than creating  
the job.

Robert M. wrote:

> There has to be something like this… I have some tracking related SQL  
> queries that are slow and don’t need to happen in the request cycle. Is  
> it possible to dump them to some text file and then read them in a  
> daemon and send them to MySQL?
> 
> What would be the simplest way of implementing this?

---

<div class="post-metadata">

**Author:** ![newbee-robert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dbc845/32.png) [@newbee-robert](https://www.ruby-forum.com/u/newbee-robert)\
**Post date:** [October 20, 2009, 6:37am UTC](https://www.ruby-forum.com/t/simple-system-for-asynchronous-sql-queries/177551/3 "2009-10-20T06:37:18Z")

</div>

Hi Robert M.

Can’t you use a rake task and make a cron to execute this task at a  
specified time or time interval ?

Sijo

---

<div class="post-metadata">

**Author:** ![newbee-robert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dbc845/32.png) [@newbee-robert](https://www.ruby-forum.com/u/newbee-robert)\
**Post date:** [October 20, 2009, 10:55pm UTC](https://www.ruby-forum.com/t/simple-system-for-asynchronous-sql-queries/177551/4 "2009-10-20T22:55:13Z")

</div>

I’d put them in a model and call them via cron, e.g.

# in app/models:

class SlowQuery \< ActiveRecord::Base  
def self.some\_query  
self.connection.execute “my big slow SQL here”  
end  
end

cron can call:

```
ruby script/runner "SlowQuery.some_query"

```

On Oct 19, 10:26 pm, Robert M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![newbee-robert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dbc845/32.png) [@newbee-robert](https://www.ruby-forum.com/u/newbee-robert)\
**Post date:** [October 20, 2009, 7:20am UTC](https://www.ruby-forum.com/t/simple-system-for-asynchronous-sql-queries/177551/5 "2009-10-20T07:20:11Z")

</div>

On Mon, Oct 19, 2009 at 7:26 PM, Robert M. wrote:

> Should note that I already have delayed\_job running, but it seems a bit  
> silly to create a job (in the database) that just executes a SQL query,  
> even if the SQL query is running take significantly longer than creating  
> the job.

And it would be somehow less “silly” to do file IO to "dump them to some  
text file and then read them in a daemon and " open a DB connection to  
" send them to MySQL?

> > What would be the simplest way of implementing this?

I’d say you just identified the “simplest”, since it’s already in place.  
YMMV 🙂

–  
Hassan S. ------------------------ removed\_email\_address@domain.invalid  
twitter: @hassan
