Re: [PERFORM] Should the optimiser convert a CASE into a WHERE if it can?

2010-01-27 Thread Віталій Тимчишин
27 січня 2010 р. 19:01 Matthew Wakeling написав: > On Wed, 27 Jan 2010, Віталій Тимчишин wrote: > >> How about SELECT SUM (case when id > 120 and id < 121 then 1 end) >> from tbl_tracker; >> > > That is very interesting. > > > * All the functions should be noop for null input >> > > Ala

Re: [PERFORM] Should the optimiser convert a CASE into a WHERE if it can?

2010-01-27 Thread Matthew Wakeling
On Wed, 27 Jan 2010, Віталій Тимчишин wrote: How about SELECT SUM (case when id > 120 and id < 121 then 1 end) from tbl_tracker; That is very interesting. * All the functions should be noop for null input Alas, not true for COUNT(*), AVG(), etc. Matthew -- An optimist sees the gl

Re: [PERFORM] Should the optimiser convert a CASE into a WHERE if it can?

2010-01-27 Thread Віталій Тимчишин
2010/1/26 Matthew Wakeling > On Tue, 26 Jan 2010, Richard Neill wrote: > >> SELECT SUM (case when id > 120 and id < 121 then 1 else 0 end) >> from tbl_tracker; >> >> Explain shows that this does a sequential scan. >> > > I'd defer to Tom on this one, but really, for Postgres to work this

Re: [PERFORM] Should the optimiser convert a CASE into a WHERE if it can?

2010-01-26 Thread Scott Carey
On Jan 26, 2010, at 9:41 AM, Richard Neill wrote: > Thanks for your answers. > > > David Wilson wrote: > >> Why not simply add the where clause to the original query? >> >> SELECT >> SUM (case when id > 120 and id < 121 then 1 else 0 end) AS c1, >> SUM (case when id > 121 and id <

Re: [PERFORM] Should the optimiser convert a CASE into a WHERE if it can?

2010-01-26 Thread Richard Neill
Thanks for your answers. David Wilson wrote: > Why not simply add the where clause to the original query? > > SELECT > SUM (case when id > 120 and id < 121 then 1 else 0 end) AS c1, > SUM (case when id > 121 and id < 122 then 1 else 0 end) AS c2, > SUM (case when id > 122 an

Re: [PERFORM] Should the optimiser convert a CASE into a WHERE if it can?

2010-01-26 Thread Matthew Wakeling
On Tue, 26 Jan 2010, Richard Neill wrote: SELECT SUM (case when id > 120 and id < 121 then 1 else 0 end) from tbl_tracker; Explain shows that this does a sequential scan. I'd defer to Tom on this one, but really, for Postgres to work this out, it would have to peer deep into the myst

Re: [PERFORM] Should the optimiser convert a CASE into a WHERE if it can?

2010-01-26 Thread Tom Lane
Richard Neill writes: > SELECT >SUM (case when id > 120 and id < 121 then 1 else 0 end) AS c1, >SUM (case when id > 121 and id < 122 then 1 else 0 end) AS c2, >... > FROM tbl_tracker; > This can be manually optimised into a far uglier (but much much faster) > query:

[PERFORM] Should the optimiser convert a CASE into a WHERE if it can?

2010-01-26 Thread Richard Neill
Dear All, Just wondering whether there is a missing scope for the query planner (on 8.4.2) to be cleverer than it currently is. Specifically, I wonder whether the optimiser should know that by converting a CASE condition into a WHERE condition, it can use an index. Have I found a possible e