Re: limit order by performance issue

From: Pedro Jiménez Pérez <p(dot)jimenez(at)ismsolar(dot)com>
To: pgsql-performance(at)postgresql(dot)org
Subject: Re: limit order by performance issue
Date: 2012-10-17 09:14:05
Message-ID: 507E76DD.9020802@ismsolar.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

For this query:

select var_value from ism_floatvalues where id_signal = 29660 order by
time_stamp desc limit 1;

This is what EXPLAIN returns (can't make EXPLAIN ANALYZE because it
"never" ends):

"Limit (cost=0.00..258.58 rows=1 width=16)"
" -> Index Scan Backward using ism_floatvalues_index_time_stamp on
ism_floatvalues (cost=0.00..8912076.82 rows=34466 width=16)"
" Filter: (id_signal = 29660)"

This is EXPLAIN ANALYZE without "limit 1":

"Sort (cost=93683.39..93769.56 rows=34466 width=16) (actual
time=188.643..188.650 rows=1 loops=1)"
" Sort Key: time_stamp"
" Sort Method: quicksort Memory: 17kB"
" -> Index Scan using ism_floatvalues_index on ism_floatvalues
(cost=0.00..90494.38 rows=34466 width=16) (actual time=188.019..188.030
rows=1 loops=1)"
" Index Cond: (id_signal = 29660)"
"Total runtime: 189.033 ms"

Note that I have created two indexes, the first on id_signal and the
second on time_stamp.
Regards.

El 16/10/2012 21:23, Pavel Stehule escribió:
> 2012/10/15 Pedro Jiménez <p(dot)jimenez(at)ismsolar(dot)com>:
>> Hello,
>> I'm trying to do a simple SQL query over Postgresl 9.0 running on Ubuntu.
>>
>> I have a large table (over 100 million records) with three fields, id_signal
>> (bigint), time_stamp (timestamp) and var_value (float).
>>
>> My query looks like this:
>>
>> select var_value from ism_floatvalues where id_signal = 29660 order by
>> time_stamp desc limit 1;
>>
>> So I want to select the last value from a determinated ID (is_signal).
>>
>> This query runs FOREVER, while if I delete "limit 1" it runs instantly....
> did you ANALYZE your tables?
>
> Can you send EXPLAIN ANALYZE result of both queries?
>
> Regards
>
> Pavel Stehule
>
>
>> Any help?
>>
>> Regards.
>>
>>
>>
>>
>> --
>> Sent via pgsql-performance mailing list (pgsql-performance(at)postgresql(dot)org)
>> To make changes to your subscription:
>> http://www.postgresql.org/mailpref/pgsql-performance

--
Documento sin título

**Pedro Jiménez Pérez
**p(dot)jimenez(at)ismsolar(dot)com

****

**Innovación en Sistemas de Monitorización, S.L.**
Edificio Hevimar
C/ Iván Pavlov 2 y 4 - Parcela 4 2ª Planta Local 9
Parque Tecnológico de Andalucía
29590 Campanillas (Málaga)
Tlfno. 952 02 07 13
contacto(at)ismsolar(dot)com

firma_gpt.jpg, 1 kB

Antes de imprimir, piensa en tu responsabilidad y compromiso con el
MEDIO AMBIENTE!

Before printing, think about your responsibility and commitment with the
ENVIRONMENT!

CLÁUSULA DE CONFIDENCIALIDAD.- Este mensaje, y en su caso, cualquier
fichero anexo al mismo, puede contener información confidencial o
legalmente protegida (LOPD 15/1999 de 13 de Diciembre), siendo para uso
exclusivo del destinatario. No hay renuncia a la confidencialidad o
secreto profesional por cualquier transmisión defectuosa o errónea, y
queda expresamente prohibida su divulgación, copia o distribución a
terceros sin la autorización expresa del remitente. Si ha recibido este
mensaje por error, se ruega lo notifique al remitente enviando un
mensaje al correo electrónico contacto(at)ismsolar(dot)com y proceda
inmediatamente al borrado del mensaje original y de todas sus copias.
Gracias por su colaboración.

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Martin French 2012-10-17 09:28:55 Re: Out of shared mem on new box with more mem, 9.1.5 -> 9.1.6
Previous Message Maciek Sakrejda 2012-10-17 09:13:43 Re: Out of shared mem on new box with more mem, 9.1.5 -> 9.1.6