# Performance Issue

**URL:** <https://www.ruby-forum.com/t/performance-issue/188137>\
**Category:** Rails\
**Created:** [May 6, 2010, 1:21pm UTC](https://www.ruby-forum.com/t/performance-issue/188137 "2010-05-06T13:21:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hitesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8e7dd6/32.png) [@hitesh](https://www.ruby-forum.com/u/hitesh)\
**Post date:** [May 6, 2010, 1:21pm UTC](https://www.ruby-forum.com/t/performance-issue/188137/1 "2010-05-06T13:21:38Z")

</div>

Hi,

I am having one performance issue.  
I want to retrieve different kind of data from a single table with  
different conditions.

here is example

```
@licences = Licence.scoped_by_user_id(10)
usedpermlicence = @licences.scoped_by_status_and_licence_type(1, 0)
usedtemplicence = @licences.scoped_by_status_and_licence_type(1, 1)
remainlicence = @licences.scoped_by_status_and_licence_type(0, 0)
remaintemplicence = @licences.scoped_by_status_and_licence_type(0,

```

1. 

Please let me know is there any way to store all the licences into one  
object and can retrieve data from this object without any db query.

Thanks in Advance  
Hitesh R.

---

<div class="post-metadata">

**Author:** ![hitesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8e7dd6/32.png) [@hitesh](https://www.ruby-forum.com/u/hitesh)\
**Post date:** [May 6, 2010, 2:34pm UTC](https://www.ruby-forum.com/t/performance-issue/188137/2 "2010-05-06T14:34:18Z")

</div>

> 

Sure:

# usedpermlicence = @licences.select {|o| o.status == 1 && o.licence\_type

# 0} usedtemplicence = @licences.select {|o| o.status == 1 && o.licence\_type

1}

etc…

You can treat @licences as an array and just select the items you want.  
One  
DB query (which you’re already doing) and a quick bit of filtering.

[http://ruby-doc.org/core/classes/Array.html#M002191](http://ruby-doc.org/core/classes/Array.html#M002191)

Cheers,

Andy

---

<div class="post-metadata">

**Author:** ![hitesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8e7dd6/32.png) [@hitesh](https://www.ruby-forum.com/u/hitesh)\
**Post date:** [May 6, 2010, 3:22pm UTC](https://www.ruby-forum.com/t/performance-issue/188137/3 "2010-05-06T15:22:29Z")

</div>

Thanks Andy,

For ur quick reply and valuable suggestion.  
I followed your suggestion and It improved performance and decrease the  
no. of db queries.

Thanks

Andy J. wrote:

> > 
> 
> Sure:
> 
> # usedpermlicence = @licences.select {|o| o.status == 1 && o.licence\_type
> 
> # 0} usedtemplicence = @licences.select {|o| o.status == 1 && o.licence\_type
> 
> 1}
> 
> etc…
> 
> You can treat @licences as an array and just select the items you want.  
> One  
> DB query (which you’re already doing) and a quick bit of filtering.
> 
> [class Array - RDoc Documentation](http://ruby-doc.org/core/classes/Array.html#M002191)
> 
> Cheers,
> 
> Andy
