# Huge performance issue with ActiveRecord::Base exists?

**URL:** <https://www.ruby-forum.com/t/huge-performance-issue-with-activerecord-base-exists/150720>\
**Category:** Rails\
**Created:** [October 12, 2008, 1:19am UTC](https://www.ruby-forum.com/t/huge-performance-issue-with-activerecord-base-exists/150720 "2008-10-12T01:19:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [October 12, 2008, 1:19am UTC](https://www.ruby-forum.com/t/huge-performance-issue-with-activerecord-base-exists/150720/1 "2008-10-12T01:19:44Z")

</div>

I was just running a relative simple exists? query:

Page.exists?([“id \> ?”,i])

which equates to

SELECT COUNT(\*) FROM `pages` WHERE (id \> 1400000) LIMIT 1

You would think with a COUNT and a LIMIT 1, that things would be really  
fast, but its SUPER slow, its trying to count all the records greater  
than 1400000.

This is N times faster:

SELECT \* FROM `pages` WHERE (id \> 1400000) LIMIT 1

Do you guys think this needs to be changed, or not worth changing  
COUNT(\*) to just \* even though its a “LIMIT 1” ?

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [October 12, 2008, 1:22am UTC](https://www.ruby-forum.com/t/huge-performance-issue-with-activerecord-base-exists/150720/2 "2008-10-12T01:22:12Z")

</div>

COMPLETELY IGNORE this “issue”, this was caused by an old overwrite of  
the exists? function that I had from years ago.

MY BAD!

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [October 12, 2008, 1:26am UTC](https://www.ruby-forum.com/t/huge-performance-issue-with-activerecord-base-exists/150720/3 "2008-10-12T01:26:56Z")

</div>

On Oct 12, 12:19 am, Aryk G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> than 1400000.
> 
> This is N times faster:
> 
> SELECT \* FROM `pages` WHERE (id \> 1400000) LIMIT 1
> 
> Do you guys think this needs to be changed, or not worth changing  
> COUNT(\*) to just \* even though its a “LIMIT 1” ?

Which version of rails are you running? I’ve looked at the source of  
edge, 2-1-stable and 2-0-stable and for all those versions exists does  
not appear to use count. Are you sure the query is coming from there?

Fred

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [October 12, 2008, 1:28am UTC](https://www.ruby-forum.com/t/huge-performance-issue-with-activerecord-base-exists/150720/4 "2008-10-12T01:28:13Z")

</div>

Fred, this was my mistake read, the post above yours.
