Reputation: 629
I'm using the Sequel gem with a postgres database which has a 300k+ entries. I'm trying to get the earliest and latest entry by time, however I migrated the db and some entries got out of order time-wise.
If I use mydb[:items].first
and mydb[:items].last
it takes a fraction of a second for the query. However, like I said, they aren't fully in order of time, so that can get messed up. If I run mydb[:items].order(:time).first
and mydb[:items].order(:time).last
, it works but it takes FAR longer (roughly six seconds) to complete each query.
Is there a way to reorder the database so it's naturally in order of time, so I don't have to run the order command? Or am I just doing something wrong?
Upvotes: 0
Views: 55
Reputation: 3005
If you want to get record in a specific order you must use an order clause. Most databases do not guarantee the order if no sort clause is provided.
You should index your table by the fields you will use to sort.
Upvotes: 1