# SQL performance

**URL:** <https://discuss.rubyonrails.org/t/sql-performance/4801>\
**Category:** rubyonrails-talk\
**Created:** [November 20, 2006, 10:20pm UTC](https://discuss.rubyonrails.org/t/sql-performance/4801 "2006-11-20T22:20:29Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![krunk1](https://avatars.discourse-cdn.com/v4/letter/k/a8b319/32.png) [@krunk1](https://discuss.rubyonrails.org/u/krunk1)\
**Post date:** [November 20, 2006, 10:20pm UTC](https://discuss.rubyonrails.org/t/sql-performance/4801/1 "2006-11-20T22:20:29Z")

</div>

I have a query that involves a couple of left joins and some subqueries. Searching around on how to do this, I came across a method that paginated from an arbitrary query:

&nbsp;&nbsp;&nbsp;&nbsp;def paginate\_from\_sql(model, sql, count\_sql, per\_page) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;plural\_model\_name = "@#{model.name.underscore.pluralize}".to\_sym &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paginator\_name = "@#{model.name.underscore}\_pages".to\_sym &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.instance\_variable\_set(paginator\_name, Paginator.new(self, model.count\_by\_sql(count\_sql), per\_page, @params['page'])) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.instance\_variable\_set(plural\_model\_name, model.find\_by\_sql(sql + " LIMIT #{per\_page}" + " OFFSET #{self.instance\_variable\_get(paginator\_name).current.to\_sql[1]}")) &nbsp;&nbsp;&nbsp;&nbsp;end

I then applied my query:

Plain english: "Select all subjects with \<subject conditions\> who do not have any calls with a call\_back date scheduled and have not received 6 calls or more".

query = "SELECT subjects.\* FROM (" + &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"SELECT subjects.\*,count(subjects.id) AS callsum FROM (" + &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"SELECT subjects.\* FROM subjects LEFT JOIN calls ON calls.subject\_id=subjects.id AND calls.call\_back\_date IS NOT NULL WHERE calls.id IS NULL AND subjects.agreed\_to\_participate IS NULL AND subjects.valid\_number = 'T') AS subjects " + &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"LEFT JOIN calls ON calls.subject\_id=subjects.id GROUP BY subjects.id) as subjects"+ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" WHERE callsum \< 6 AND date\_format(subjects.updated\_on,'%Y-%m-%d') \< DATE\_SUB(CURDATE(), INTERVAL 1 DAY)"

count\_by\_sql = "SELECT COUNT(\*) FROM (query)"

paginate\_from\_sql(Subject, query, count\_by\_sql, 15)

This all works fine and ouputs the correct data paginated and all.

But (there's always a but), the performance is horrendous compared to the previous php implementation of the same functionality. There's noticeable lag on each pagination or page load. Just doing a loose count, it takes about 12s to fetch and display the data compared to about 1 or 2s for php.

Questions: 1) Is there a more friendly way to fetch and paginate queries like this? 2) Am I missing something here that could dramatically increase performance as far as page rendering?
