Re: [HACKERS] An optimisation question
От | Tom Lane |
---|---|
Тема | Re: [HACKERS] An optimisation question |
Дата | |
Msg-id | 5561.936023612@sss.pgh.pa.us обсуждение исходный текст |
Ответ на | An optimisation question (Constantin Teodorescu <teo@flex.ro>) |
Список | pgsql-hackers |
Constantin Teodorescu <teo@flex.ro> writes: > select data from valori order by desc limit 1 > NOTICE: QUERY PLAN: > Sort (cost=3216.01 rows=72970 width=8) -> Seq Scan on valori (cost=3216.01 rows=72970 width=8) > I thought that if the 'order by' implies an column which have a btree > index, the sort would not be actually executed and the index will be > used instead. But it seems that it won't. That's fixed for 6.6. A workaround that partially solves the problem for 6.5 is to add a dummy WHERE clause referencing the ORDER-BY item:select data from valori where data > '1/1/1800'orderby data limit 1; The WHERE is needed to get the 6.5 optimizer to consider the index at all. In a quick test it seems this works for normal order but not DESC order... you could try applying the backwards-index patch that someone (Hiroshi or Tatsuo, I think) posted recently. > Also, the following query : > select max(data) from valori where data<'2-3-1999' > is not using optimally the index, it just limit the records for the > aggregate function instead of picking the first value from the left of > the index tree lower than '2-3-1999'. There's no prospect of that happening anytime soon, I fear; there is no connection between aggregate functions and indexes in the system, and no easy way of making one. This workaround works in current sources: explain select data from valori where data<'2-3-1999' order by data desc limit 1; NOTICE: QUERY PLAN: Index Scan Backward using valori_i on valori (cost=21.67 rows=334 width=8) regards, tom lane
В списке pgsql-hackers по дате отправления: