Re: Query take a long time and use no index

From: basti <mailinglist(at)unix-solution(dot)de>
To: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: Query take a long time and use no index
Date: 2023-07-17 18:19:08
Message-ID: 4b927a7f-ff01-0df3-27b6-30b0fee17969@unix-solution.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Thanks a lot tomas, i will try it.

I have find out that there is a 'aggregation' function in the frontend.
But this is MySQL specific and I have no idea the transform it to postgres.

It looks like:
'REPLACE INTO aggregate (channel_id, type, timestamp, value, count)

SELECT channel_id, ? AS type, MAX(agg.timestamp) AS timest
amp, COALESCE( SUM(agg.val_by_time) / (MAX(agg.timestamp) -
MIN(agg.prev_timestamp)), AVG(agg.value)) AS value, COUNT(agg.value) AS
count FROM ( SELECT channel_id,
timestamp, value, value * (timestamp - @prev_timestamp) AS
val_by_time, COALESCE(@prev_timestamp, 0) AS prev_timestamp,
@prev_timestamp := timestamp FROM data CROSS
JOIN (SELECT @prev_timestamp := NULL) AS vars WHERE channel_id = ?
AND timestamp < UNIX_TIMESTAMP(DATE_FORMAT(NOW(), "%Y-%m-%d")) * 1000 )
AS agg GROUP BY channel_
id, DATE_TRUNC('day', TIMESTAMP 'epoch' + timestamp * INTERVAL '1
millisecond')' with params [3, 5]:


SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near
"REPLACE"

LINE 1: REPLACE INTO aggregate (channel_id, type, timestamp, value,
...

Am 17.07.23 um 13:21 schrieb Tomas Vondra:
> On 7/17/23 13:20, Tomas Vondra wrote:
>> ...
>>
>> It's always going to be slow with the COUNT(DISTINCT), I'm afraid.
>>
>> Not sure how much you can modify the query / database, and how accurate
>> results you need. If you're OK with estimates, you can try postgres-hll
>> extension [2] which estimates count(distinct). For exact reaults, I
>> wrote count_distinct extension [2] that uses hashtable. Might be worth a
>> try, I guess.
>>
>
> Damn, I forgot to add the links:
>
> [1] https://github.com/citusdata/postgresql-hll
>
> [2] https://github.com/tvondra/count_distinct
>
> regards
>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Nathan Bossart 2023-07-17 18:50:32 Re: Is anyone using db_user_namespace?
Previous Message Carl Erik Eriksson 2023-07-17 16:34:16 Re: Installation Issue