This project is archived and is in readonly mode.
[Arel] Offset + Count != good
Reported by Tobias Lütke | January 8th, 2011 @ 08:18 PM | in 3.x
Bug in query planner, produces poor SQL:
ree-1.8.7-2010.02 > Order.offset(1).count #=> 0 # (200 in
table)
SQL (0.3ms) SELECT COUNT(*) FROM orders
LIMIT
18446744073709551615 OFFSET 1
Comments and changes to this ticket
-
Aaron Patterson January 14th, 2011 @ 06:40 PM
Hi Tobias, what database are you using? From the SQL generated, I assume mysql?
-
Tobias Lütke January 14th, 2011 @ 08:03 PM
yes this is mysql. Frankly i think the right fix is to always clean out any offset if it's a .count query. I found quite a few current_orders.except(:offset).count like queries in the Shopify codebase already.
-
Aaron Patterson January 18th, 2011 @ 10:39 PM
Is AR setting the offset, or is that coming from your application code?
I tend to agree with you about removing the offset on count(*) queries. I just wonder if anyone is relying on this functionality.
-
Tobias Lütke January 18th, 2011 @ 11:04 PM
It comes from our application code, we have methods such as
shop.orders.filter_by_params(params) which looks at things like :page,
:order_status etc.Because it looks at .page it has to add a offset, however we still need to
query the .count from the arel object after the filters have been applied,
hence .except(:offset).count.I don't think anyone relies on this functionality. Offset + count(*) leads
to undefined behavior in most SQL servers.- tobi
-
Repository February 25th, 2011 @ 11:40 PM
- State changed from new to resolved
(from [54a2bf66019d2694ff53f666765faf5bca927c09]) removing limits and offsets from COUNT queries unless both are specified. [#6268 state:resolved] https://github.com/rails/rails/commit/54a2bf66019d2694ff53f666765fa...
-
John Mileham March 9th, 2011 @ 08:14 PM
Hi,
Just wanted to note here that I submitted a patch for #5060 that would change the semantics of #count to apply the :limit and :offset first. If that approach were accepted, it would change the behavior associated with this ticket as well. It would also make #count consistent in behavior with ActiveRecord's other aggregate calculations (like sum). Tobias, it seems that such behavior would probably be what your app was expecting as well when passing in scopes with limits and offsets? Any feedback would be appreciated.
https://rails.lighthouseapp.com/projects/8994/tickets/5060-activere...
Thanks,
-john
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Tags
Referenced by
- 6268 [Arel] Offset + Count != good (from [54a2bf66019d2694ff53f666765faf5bca927c09]) removin...
- 5060 ActiveRelation "offset().empty?" produces unexpected result Also pertains to #6268.