Re: [GENERAL] How do I bump a row to the front of sort efficiently

2015-02-07 Thread Paul Jungwirth
> Or maybe instead of a view you could write a > set-returning function, e.g. as described here: I thought I'd see if I could make this work just for fun. Here is a simple proof of concept (on 9.3): -- DROP TABLE IF EXISTS topics; CREATE TABLE topics ( id INTEGER PRIMARY KEY, bumped_at INTEGE

Re: [GENERAL] How do I bump a row to the front of sort efficiently

2015-02-05 Thread RĂ©mi Cura
Hey, I'm not a guru, here is what I understood. You are mixing several problems in the same question : - 1. why the planner isn't more efficient - 2. why the workaround is difficult to use with an ORM. for 1. you can't do much (as said by others, you don't really need a case here anyway). I thin

Re: [GENERAL] How do I bump a row to the front of sort efficiently

2015-02-04 Thread Paul Jungwirth
>> I imagine your original would be at risk of LIMITing out the very row you >> seek to get at the "top", since you don't have an ORDER BY to tell it which >> ones to keep during the outer LIMIT. Here is an old thread about combining ORDER BY with UNION: http://www.postgresql.org/message-id/16814

Re: [GENERAL] How do I bump a row to the front of sort efficiently

2015-02-04 Thread Sam Saffron
The union hack may be able to work, but what I want is slightly more complex I want this to work efficiently, even without the case it chokes: explain select * from testing order by id in (100,2,-1) desc, id limit 30; QUERY PLAN

Re: [GENERAL] How do I bump a row to the front of sort efficiently

2015-02-04 Thread BladeOfLight16
On Tue, Feb 3, 2015 at 11:28 PM, Sam Saffron wrote: > Note: I still consider this a bug/missing feature of sorts since the > planner could do better here, and there is no real clean way of > structuring a query to perform efficiently here, which is why I > erroneously cross posted this to hacker

Re: [GENERAL] How do I bump a row to the front of sort efficiently

2015-02-03 Thread Sam Saffron
Note: I still consider this a bug/missing feature of sorts since the planner could do better here, and there is no real clean way of structuring a query to perform efficiently here, which is why I erroneously cross posted this to hacker initially: # create table testing(id serial primary key, dat

Re: [GENERAL] How do I bump a row to the front of sort efficiently

2015-02-03 Thread BladeOfLight16
On Tue, Feb 3, 2015 at 9:33 PM, BladeOfLight16 wrote: > This is why ORMs are bad. They make hard problems *much* harder, and the > only benefit is that they maybe make easy problems a little quicker. The > cost/savings is *heavily* skewed toward the cost, since there's no upper > bound on the cos

Re: [GENERAL] How do I bump a row to the front of sort efficiently

2015-02-03 Thread BladeOfLight16
On Mon, Feb 2, 2015 at 1:16 AM, Sam Saffron wrote: > However, the contortions on the above query make it very un-ORM > friendly as I would need to define a view for it but would have no > clean way to pass limits and offsets in. > This is why ORMs are bad. They make hard problems *much* harder,

Re: [GENERAL] How do I bump a row to the front of sort efficiently

2015-02-02 Thread Tim Clarke
On 02/02/15 08:40, hubert depesz lubaczewski wrote: > On Mon, Feb 02, 2015 at 05:16:40PM +1100, Sam Saffron wrote: >> Even this is fast, and logically equiv as id is primary key unique >> select * from topic >> where id = 1000 >> union all >> select * from ( >> select * from topics >> where id

Re: [GENERAL] How do I bump a row to the front of sort efficiently

2015-02-02 Thread hubert depesz lubaczewski
On Mon, Feb 02, 2015 at 05:16:40PM +1100, Sam Saffron wrote: > Even this is fast, and logically equiv as id is primary key unique > select * from topic > where id = 1000 > union all > select * from ( > select * from topics > where id <> 1000 > order by bumped_at desc > limit 30 > ) as x > l

[GENERAL] How do I bump a row to the front of sort efficiently

2015-02-01 Thread Sam Saffron
I have this query: select * from topics order by case when id=1 then 0 else 1 end, bumped_at desc limit 30 It works fine, bumps id 1 to the front of the sort fine but is terribly inefficient and scans OTH "select * from topics where id = 1" is super fast "select * from topics order by bumped_a