jeremyevans / sequel

Sequel: The Database Toolkit for Ruby
http://sequel.jeremyevans.net
Other
4.99k stars 1.07k forks source link

limit fires an extra query when offset is greater than 1 #1891

Closed ashwintastic closed 2 years ago

ashwintastic commented 2 years ago

https://github.com/jeremyevans/sequel/blob/c6bcc6b5abf95ad1b6b38abaa1051959a28cb945/lib/sequel/dataset/query.rb#L660

jeremyevans commented 2 years ago

First, clone doesn't cause a query. Second, if you have questions about Sequel, create a discussion post. Only create an issue to report a bug in Sequel.

ashwintastic commented 2 years ago

@jeremyevans Limit fires an extra query when the offset is more than 1. I created a temp table and executed the query DB[:temp].limit(3,3).all

_First it executed this query_ START SELECT * FROM (SELECT * FROM "TEMP") "T1" WHERE (ROWNUM <= 1) - END ( 0.0060s) SELECT * FROM (SELECT * FROM "TEMP") "T1" WHERE (ROWNUM <= 1)

and then this

- START SELECT "ID", "NAME" FROM (SELECT "T1".*, ROWNUM "X_SEQUEL_ROW_NUMBER_X" FROM (SELECT * FROM "TEMP") "T1") "T1" WHERE (("X_SEQUEL_ROW_NUMBER_X" > 3) AND ("X_SEQUEL_ROW_NUMBER_X" <= (3 + 3)))

- END ( 0.0033s) SELECT "ID", "NAME" FROM (SELECT "T1".*, ROWNUM "X_SEQUEL_ROW_NUMBER_X" FROM (SELECT * FROM "TEMP") "T1") "T1" WHERE (("X_SEQUEL_ROW_NUMBER_X" > 3) AND ("X_SEQUEL_ROW_NUMBER_X" <= (3 + 3))) => [{:id=>0.4e1, :name=>"d"}, {:id=>0.5e1, :name=>"e"}]

jeremyevans commented 2 years ago

Not sure what database you are using, but I'm guessing an old version of Oracle? In order to run the query with a limit and offset on an old version of Oracle, the limit and offset need to be emulated using ROWNUM, and that requires getting the columns. The first query is run to get the columns, and the second query to get the data (you can see the second query uses the columns returned by the first query).

In the future, please ask your question in only a single place. You don't need to ask both on GitHub and on Google Groups.