Гость
Целевая тема:
Создать новую тему:
Автор:
Форумы / PostgreSQL [игнор отключен] [закрыт для гостей] / мастерам сиквела просвящается / 8 сообщений из 8, страница 1 из 1
04.05.2005, 17:15
    #33048730
pavel_programmer
Гость
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
мастерам сиквела просвящается
естьтаблица
TABLE dev_data (
"dev_id" INTEGER,
"x" REAL,
"y" REAL,
"devtime" TIMESTAMP(0) WITHOUT TIME ZONE,
"group_id" integer
) WITH OIDS;

и при изменении параметров(интервал времени не постоянен) происходит вставка в базу данных
если между двумя записями одного прибора т.е. dev_id=конст
интервал меньше заданного то считать что в этот этот интервал приботр работал и добавить это время в общему времени работып рибора иначе не добавлять за день прибор мог много раз отключаться

вопрос господа как получить выборку за опред промежуток времени(дней) где будет время работы каждого прибора (как сумма нтервалов)
приветствуются варианты как через запрос так и с исп процедур)
...
Рейтинг: 0 / 0
05.05.2005, 10:33
    #33049750
LeXa NalBat
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
мастерам сиквела просвящается
Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
create table test (
 id integer,
 time timestamp,
 primary key (id,time)
);

insert into test values (  1 , '2000-01-01 00:00:05' );
insert into test values (  2 , '2000-01-01 00:00:05' );
insert into test values (  1 , '2000-01-01 00:00:07' );
insert into test values (  1 , '2000-01-01 00:00:08' );
insert into test values (  2 , '2000-01-01 00:00:10' );
insert into test values (  1 , '2000-01-01 00:00:15' );
insert into test values (  1 , '2000-01-01 00:00:16' );
insert into test values (  2 , '2000-01-01 00:00:16' );
insert into test values (  2 , '2000-01-01 00:00:26' );

select
 id,
 sum( time2-time1 )
from
 (
  select
   id,
   time as time1,
   (
    select
     time
    from
     test as test2
    where
     test2.id=test1.id and
     test2.time>test1.time
    limit  1 
   ) as time2
  from
   test as test1
 ) as A
where
 time2-time1<='00:00:06'
group by
 id
;

drop table test;
...
Рейтинг: 0 / 0
13.05.2005, 15:54
    #33062786
pavel_programmer
Гость
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
мастерам сиквела просвящается
а еще идеи есть?
я сам писал нечто подобное но в виде функции....
почемум лезу в этот форум мне интересно может есть что-нить на основе базовых функций что позволяем делать эти операции быстро....
потому что на 500000 записях этот запро висит очень долго (но он работает)
...
Рейтинг: 0 / 0
13.05.2005, 16:30
    #33062913
XM
XM
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
мастерам сиквела просвящается
pavel_programmer а еще идеи есть?
Конечно, и даже очень извратные
На больших данных не пробовал :(
Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
create type time_accum as (
        the_time timestamptz,
        the_interval interval
);
create function add_time(_state time_accum, _data_time time_accum) returns time_accum as $$
DECLARE _temp interval;
BEGIN
  _temp := _data_time.the_time - _state.the_time;
   if  _temp < '5 min'::interval THEN
        _state.the_interval := _state.the_interval + _temp;
   END IF;
   _state.the_time := _data_time.the_time;
   return _state;
END
$$ language 'plpgsql';
CREATE AGGREGATE work_time (
    BASETYPE = time_accum,
    SFUNC = add_time,
    STYPE = time_accum,
    INITCOND = '("1970-01-01",0)'
);

create function time_accum(_time timestamptz) returns time_accum as $$
declare _data time_accum;
BEGIN
        _data.the_time := _time;
        _data.the_interval := '0'::interval;
        return _data;

END
$$ language 'plpgsql';

select dev_id, work_time(time_accum(devtime)) from
                (select dev_id, devtime from dev_data  order by devtime) as ttt
group by dev_id;
...
Рейтинг: 0 / 0
14.05.2005, 13:30
    #33064597
LeXa NalBat
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
мастерам сиквела просвящается
select version();
PostgreSQL 7.4.7 on i686-pc-linux-gnu, compiled by GCC gcc (GCC) 3.3.4 (pre 3.3.5 20040809)

create table test (
id integer,
time timestamp,
primary key (id,time)
);

time perl -e "printf \"begin;\n\"; for ( my \$i=1; \$i<=1_000_000; \$i++ ) { printf \"insert into test values ( %d, '%04d-%02d-%02d %02d:%02d:%02d' );\n\", 1+rand(5), 2000+rand(5), 1+rand(12), 1+rand(28), rand(24), rand(60), rand(60); unless ( \$i % 1_000 ) { printf \"end;\n\"; printf \"begin;\n\"; } } printf \"end;\n\";" | psql 1>/dev/null 2>/dev/null

select count(*) from test;
count
--------
608000
(1 row)

vacuum analyze test;

set enable_seqscan to off;
set enable_mergejoin to off;
set enable_hashjoin to off;

-- ПЕРВЫЙ ЗАПРОС

explain
analyze
select
id,
sum( time2-time1 )
from
(
select
id,
time as time1,
(
select
time
from
test as test2
where
test2.id=test1.id and
test2.time>test1.time
order by
id, time
limit 1
) as time2
from
test as test1
) as A
where
time2<=time1+' 10:00:00 '
group by
id
order by
id
;
GroupAggregate (cost=0.00..3723865.62 rows=2 width=12) (actual time=30615.110..154604.042 rows=5 loops=1)
-> Index Scan using test_pkey on test test1 (cost=0.00..3722846.14 rows=202667 width=12) (actual time=0.760..97825.603 rows= 607715 loops=1)
Filter: ((subplan) <= ("time" + '10:00:00'::interval))
SubPlan
-> Limit (cost=0.00..3.07 rows=1 width=12) (actual time=0.129..0.131 rows=1 loops=608000)
-> Index Scan using test_pkey on test test2 (cost=0.00..124342.47 rows=40534 width=12) (actual time=0.117..0.117 rows=1 loops=608000)
Index Cond: ((id = $0) AND ("time" > $1))
SubPlan
-> Limit (cost=0.00..3.07 rows=1 width=12) (actual time=0.068..0.070 rows=1 loops=607715)
-> Index Scan using test_pkey on test test2 (cost=0.00..124342.47 rows=40534 width=12) (actual time=0.056..0.056 rows=1 loops=607715)
Index Cond: ((id = $0) AND ("time" > $1))
Total runtime: 154 605.676 ms

explain
analyze
select
id,
sum( time2-time1 )
from
(
select
id,
time as time1,
(
select
time
from
test as test2
where
test2.id=test1.id and
test2.time>test1.time
order by
id, time
limit 1
) as time2
from
test as test1
) as A
where
time2<=time1+' 00:01:00 '
group by
id
order by
id
;
GroupAggregate (cost=0.00..3723865.62 rows=2 width=12) (actual time=20272.304..101391.404 rows=5 loops=1)
-> Index Scan using test_pkey on test test1 (cost=0.00..3722846.14 rows=202667 width=12) (actual time=4.825..98375.592 rows= 29956 loops=1)
Filter: ((subplan) <= ("time" + '00:01:00'::interval))
SubPlan
-> Limit (cost=0.00..3.07 rows=1 width=12) (actual time=0.130..0.131 rows=1 loops=608000)
-> Index Scan using test_pkey on test test2 (cost=0.00..124342.47 rows=40534 width=12) (actual time=0.118..0.118 rows=1 loops=608000)
Index Cond: ((id = $0) AND ("time" > $1))
SubPlan
-> Limit (cost=0.00..3.07 rows=1 width=12) (actual time=0.072..0.073 rows=1 loops=29956)
-> Index Scan using test_pkey on test test2 (cost=0.00..124342.47 rows=40534 width=12) (actual time=0.058..0.058 rows=1 loops=29956)
Index Cond: ((id = $0) AND ("time" > $1))
Total runtime: 101 391.911 ms

-- ВТОРОЙ ЗАПРОС

explain
analyze
select
id,
sum( time2-time1 )
from
(
select
test1.id,
test1.time as time1,
min(test2.time) as time2
from
test as test1
join
test as test2
on
test2.id=test1.id and
test2.time>test1.time and
test2.time<=test1.time+' 10:00:00 '
group by
test1.id, test1.time
) as A
group by
id
order by
id
;
GroupAggregate (cost=0.00..25683107233.67 rows=200 width=20) (actual time=105348.725..529269.218 rows=5 loops=1)
-> Subquery Scan a (cost=0.00..25683104192.67 rows=608000 width=20) (actual time=5.952..524120.843 rows=607715 loops=1)
-> GroupAggregate (cost=0.00..25683098112.67 rows=608000 width=20) (actual time=5.925..519606.949 rows=607715 loops=1)
-> Nested Loop (cost=0.00..25621411652.70 rows=8224658663 width=20) (actual time=0.765..411766.216 rows= 18207105 loops=1)
-> Index Scan using test_pkey on test test1 (cost=0.00..1854699.74 rows=608000 width=12) (actual time=0.511..5737.652 rows=608000 loops=1)
-> Index Scan using test_pkey on test test2 (cost=0.00..41867.21 rows=13511 width=12) (actual time=0.049..0.425 rows=30 loops=608000)
Index Cond: ((test2.id = "outer".id) AND (test2."time" > "outer"."time") AND (test2."time" <= ("outer"."time" + '10:00:00'::interval)))
Total runtime: 529 269.720 ms

explain
analyze
select
id,
sum( time2-time1 )
from
(
select
test1.id,
test1.time as time1,
min(test2.time) as time2
from
test as test1
join
test as test2
on
test2.id=test1.id and
test2.time>test1.time and
test2.time<=test1.time+' 00:01:00 '
group by
test1.id, test1.time
) as A
group by
id
order by
id
;
GroupAggregate (cost=0.00..25683107233.67 rows=200 width=20) (actual time=16781.895..84480.085 rows=5 loops=1)
-> Subquery Scan a (cost=0.00..25683104192.67 rows=608000 width=20) (actual time=12.730..84152.745 rows=29956 loops=1)
-> GroupAggregate (cost=0.00..25683098112.67 rows=608000 width=20) (actual time=12.713..83876.243 rows=29956 loops=1)
-> Nested Loop (cost=0.00..25621411652.70 rows=8224658663 width=20) (actual time=4.139..83243.735 rows= 30713 loops=1)
-> Index Scan using test_pkey on test test1 (cost=0.00..1854699.74 rows=608000 width=12) (actual time=0.403..42454.685 rows=608000 loops=1)
-> Index Scan using test_pkey on test test2 (cost=0.00..41867.21 rows=13511 width=12) (actual time=0.049..0.050 rows=0 loops=608000)
Index Cond: ((test2.id = "outer".id) AND (test2."time" > "outer"."time") AND (test2."time" <= ("outer"."time" + '00:01:00'::interval)))
Total runtime: 84 480.563 ms

-- ТЕСТОВЫЙ ЗАПРОС

explain
analyze
select
id,
min(time)
from
test
group by
id
order by
id
;
GroupAggregate (cost=0.00..1857739.76 rows=5 width=12) (actual time=9679.683..48503.539 rows=5 loops=1)
-> Index Scan using test_pkey on test (cost=0.00..1854699.74 rows=608000 width=12) (actual time=0.549..43460.582 rows=608000 loops=1)
Total runtime: 48 503.782 ms

drop table test;

-- РЕЗЮМЕ

Второй запрос может оказаться как быстрее первого, так и медленнее, в зависимости от того, каково среднее кол-во временных отметок, попадающих в ограничивающий интервал. Мне кажется, что скорости быстрее 48 секунд, показанной тестовым запросом с full index scan, достичь для вашей задачи нельзя. Вам нужны более быстрые запросы, чем работающие 84-154 секунды? (Вместо второго запроса, работающего 529 секунд в первом примере, нужно использовать первый запрос.)

P.S.: Пример ХМ не попробовал, потому что на 7.4 он не заработал. :-(

P.P.S.: В моем посте от 5-ого мая ошибка: в самом внутруннем селекте должен быть еще order by id, time.
...
Рейтинг: 0 / 0
14.05.2005, 15:37
    #33064962
XM
XM
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
мастерам сиквела просвящается
Celeron 1,4GHz, 256M RAM
Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
select version();
                                                         version
--------------------------------------------------------------------------------------------------------------------------
 PostgreSQL 8.0.0 on i386-redhat-linux-gnu, compiled by GCC i386-redhat-linux-gcc (GCC) 3.4.3 20050113 (Red Hat 3.4.3-15)

select count(*) from test;
  count
---------
 1000000
Код: plaintext
1.
2.
3.
-- мое решение
explain analyze select id, work_time(time_accum(time)) from
   (select id, time from test  order by time) as ttt group by id;
Код: plaintext
1.
2.
3.
4.
5.
6.
7.
                                                             QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------
 HashAggregate  (cost=178870.84..178871.84 rows=200 width=12) (actual time=85159.345..85161.039 rows=500 loops=1)
   ->  Subquery Scan ttt  (cost=161370.84..173870.84 rows=1000000 width=12) (actual time=40287.278..48047.890 rows=1000000 loops=1)
         ->  Sort  (cost=161370.84..163870.84 rows=1000000 width=12) (actual time=40245.176..43269.437 rows=1000000 loops=1)
               Sort Key: "time"
               ->  Seq Scan on test  (cost=0.00..15883.00 rows=1000000 width=12) (actual time=0.148..5608.893 rows=1000000 loops=1)
Total runtime: 85276.470 ms

Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
-- LeXa NalBat
explain analyze select id, sum( time2-time1 ) from
  (select id, time as time1,
     (select time from test as test2
        where test2.id=test1.id and test2.time>test1.time
        order by id, time limit  1 )  as time2
   from test as test1 ) as A
   where  time2<time1+'00:00:05'
   group by id order by id;
Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
Sort  (cost=3830492.19..3830492.61 rows=168 width=12) (actual time=314546.990..314547.058 rows=68 loops=1)
   Sort Key: test1.id
   ->  HashAggregate  (cost=3829845.52..3830485.98 rows=168 width=12) (actual time=314546.035..314546.824 rows=68 loops=1)
         ->  Seq Scan on test test1  (cost=0.00..3828178.85 rows=333334 width=12) (actual time=69111.297..314537.307 rows=75 loops=1)
               Filter: ((subplan) <= ("time" + '00:00:05'::interval))
               SubPlan
                 ->  Limit  (cost=0.00..3.81 rows=1 width=12) (actual time=0.290..0.292 rows=1 loops=1000000)
                       ->  Index Scan using test_id_time_idx on test test2  (cost=0.00..2535.66 rows=666 width=12) (actual time=0.276..0.276 rows=1 loops=1000000)
                             Index Cond: ((id = $0) AND ("time" > $1))
         SubPlan
           ->  Limit  (cost=0.00..3.81 rows=1 width=12) (actual time=0.066..0.067 rows=1 loops=75)
                 ->  Index Scan using test_id_time_idx on test test2  (cost=0.00..2535.66 rows=666 width=12) (actual time=0.050..0.050 rows=1 loops=75)
                       Index Cond: ((id = $0) AND ("time" > $1))
Total runtime: 314547.659 ms
...
Рейтинг: 0 / 0
15.05.2005, 08:41
    #33065694
pavel_programmer
Гость
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
мастерам сиквела просвящается
Всем респект и пасибо. Щас наполню и буду тестить)
...
Рейтинг: 0 / 0
16.05.2005, 21:06
    #33068321
LeXa NalBat
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
мастерам сиквела просвящается
select version();
PostgreSQL 8.0.1 on x86_64-unknown-linux-gnu, compiled by GCC gcc (GCC) 3.3.5 20050117 (prerelease) (SUSE Linux)

create table test (
id integer,
time timestamp,
primary key (id,time)
);

time perl -e "printf \"begin;\n\"; for ( my \$i=1; \$i<=1_000_000; \$i++ ) { printf \"insert into test values ( %d, '%04d-%02d-%02d %02d:%02d:%02d' );\n\", 1+rand(5), 2000+rand(5), 1+rand(12), 1+rand(28), rand(24), rand(60), rand(60); unless ( \$i % 1_000 ) { printf \"end;\n\"; printf \"begin;\n\"; } } printf \"end;\n\";" | psql 1>/dev/null 2>/dev/null

select count(*) from test;
count
--------
637000
(1 row)

vacuum analyze test;

-- ПЕРВЫЙ ЗАПРОС

-- БЕЗ ТЮНИНГА

explain
analyze
select
id,
sum( time2-time1 )
from
(
select
id,
time as time1,
(
select
time
from
test as test2
where
test2.id=test1.id and
test2.time>test1.time
order by
id, time
limit 1
) as time2
from
test as test1
) as A
where
time2<=time1+'00:01:00'
group by
id
order by
id
;
Sort (cost=2314124.86..2314124.86 rows=2 width=12) (actual time=17523.181..17523.182 rows=5 loops=1)
Sort Key: test1.id
-> HashAggregate (cost=2314118.48..2314124.85 rows=2 width=12) (actual time=17523.130..17523.135 rows=5 loops=1)
-> Seq Scan on test test1 (cost=0.00..2312914.99 rows=240697 width=12) (actual time=0.163..17105.726 rows=32868 loops=1)
Filter: ((subplan) <= ("time" + '00:01:00'::interval))
SubPlan
-> Limit (cost=0.00..3.18 rows=1 width=12) (actual time=0.025..0.025 rows=1 loops=637000)
-> Index Scan using test_pkey on test test2 (cost=0.00..153131.78 rows=48140 width=12) (actual time=0.023..0.023 rows=1 loops=637
000)
Index Cond: ((id = $0) AND ("time" > $1))
SubPlan
-> Limit (cost=0.00..3.18 rows=1 width=12) (actual time=0.008..0.008 rows=1 loops=32868)
-> Index Scan using test_pkey on test test2 (cost=0.00..153131.78 rows=48140 width=12) (actual time=0.006..0.006 rows=1 loops=32868)
Index Cond: ((id = $0) AND ("time" > $1))
Total runtime: 17523.506 ms

-- С ТЮНИНГОМ

set enable_seqscan to off;
set enable_mergejoin to off;
set enable_hashjoin to off;

explain
analyze
select
id,
sum( time2-time1 )
from
(
select
id,
time as time1,
(
select
time
from
test as test2
where
test2.id=test1.id and
test2.time>test1.time
order by
id, time
limit 1
) as time2
from
test as test1
) as A
where
time2<=time1+'00:01:00'
group by
id
order by
id
;
GroupAggregate (cost=0.00..4589167.57 rows=2 width=12) (actual time=2121.001..10593.881 rows=5 loops=1)
-> Index Scan using test_pkey on test test1 (cost=0.00..4587957.71 rows=240697 width=12) (actual time=0.595..10275.768 rows=32868 loops=1)
Filter: ((subplan) <= ("time" + '00:01:00'::interval))
SubPlan
-> Limit (cost=0.00..3.18 rows=1 width=12) (actual time=0.014..0.014 rows=1 loops=637000)
-> Index Scan using test_pkey on test test2 (cost=0.00..153131.78 rows=48140 width=12) (actual time=0.013..0.013 rows=1 loops=637000)
Index Cond: ((id = $0) AND ("time" > $1))
SubPlan
-> Limit (cost=0.00..3.18 rows=1 width=12) (actual time=0.007..0.007 rows=1 loops=32868)
-> Index Scan using test_pkey on test test2 (cost=0.00..153131.78 rows=48140 width=12) (actual time=0.005..0.005 rows=1 loops=32868)
Index Cond: ((id = $0) AND ("time" > $1))
Total runtime: 10593.973 ms

set enable_seqscan to on;
set enable_mergejoin to on;
set enable_hashjoin to on;

-- ВТОРОЙ ЗАПРОС

-- БЕЗ ТЮНИНГА

explain
analyze
select
id,
sum( time2-time1 )
from
(
select
test1.id,
test1.time as time1,
min(test2.time) as time2
from
test as test1
join
test as test2
on
test2.id=test1.id and
test2.time>test1.time and
test2.time<=test1.time+'00:01:00'
group by
test1.id, test1.time
) as A
group by
id
order by
id
;
Cancel request sent
ERROR: производится отмена запроса по запросу пользователя
-- не дождался! :(

-- С ТЮНИНГОМ

set enable_seqscan to off;
set enable_mergejoin to off;
set enable_hashjoin to off;

explain
analyze
select
id,
sum( time2-time1 )
from
(
select
test1.id,
test1.time as time1,
min(test2.time) as time2
from
test as test1
join
test as test2
on
test2.id=test1.id and
test2.time>test1.time and
test2.time<=test1.time+'00:01:00'
group by
test1.id, test1.time
) as A
group by
id
order by
id
;
GroupAggregate (cost=0.00..37412563346.64 rows=200 width=20) (actual time=1833.255..9164.470 rows=5 loops=1)
-> Subquery Scan a (cost=0.00..37412562984.59 rows=72209 width=20) (actual time=1.051..9122.915 rows=32868 loops=1)
-> GroupAggregate (cost=0.00..37412562262.50 rows=72209 width=20) (actual time=1.048..9078.568 rows=32868 loops=1)
-> Nested Loop (cost=0.00..37325550208.17 rows=11601583174 width=20) (actual time=0.375..9006.674 rows=33692 loops=1)
-> Index Scan using test_pkey on test test1 (cost=0.00..2287405.61 rows=722089 width=12) (actual time=0.085..5150.053 rows=637000 l
oops=1)
-> Index Scan using test_pkey on test test2 (cost=0.00..51366.96 rows=16047 width=12) (actual time=0.005..0.005 rows=0 loops=637000
)
Index Cond: ((test2.id = "outer".id) AND (test2."time" > "outer"."time") AND (test2."time" <= ("outer"."time" + '00:01:00'::int
erval)))
Total runtime: 9164.557 ms

set enable_seqscan to on;
set enable_mergejoin to on;
set enable_hashjoin to on;

-- ЧЕРЕЗ ФУНКЦИЮ ГРУППИРОВКИ (by XM)

create type time_accum as (
the_time timestamp,
the_interval interval
);
create function add_time(_state time_accum, _data_time time_accum) returns time_accum as $$
declare _temp interval;
begin
_temp := _data_time.the_time - _state.the_time;
if _temp <= '1 min'::interval then
_state.the_interval := _state.the_interval + _temp;
end if;
_state.the_time := _data_time.the_time;
return _state;
end
$$ language 'plpgsql';
create aggregate work_time (
basetype = time_accum,
sfunc = add_time,
stype = time_accum,
initcond = '("1970-01-01",0)'
);

create function time_accum(_time timestamp) returns time_accum as $$
declare _data time_accum;
begin
_data.the_time := _time;
_data.the_interval := '0'::interval;
return _data;

end
$$ language 'plpgsql';

explain
analyze
select id, work_time(time_accum(time)) from
(select id, time from test order by time) as ttt
group by id;
HashAggregate (cost=134575.27..134576.27 rows=200 width=12) (actual time=10980.302..10980.306 rows=5 loops=1)
-> Subquery Scan ttt (cost=121938.71..130964.82 rows=722089 width=12) (actual time=3237.633..3990.972 rows=637000 loops=1)
-> Sort (cost=121938.71..123743.93 rows=722089 width=12) (actual time=3237.624..3517.254 rows=637000 loops=1)
Sort Key: "time"
-> Seq Scan on test (cost=0.00..12362.89 rows=722089 width=12) (actual time=0.039..263.376 rows=637000 loops=1)
Total runtime: 11013.862 ms

-- ТЕСТОВЫЙ ЗАПРОС

explain
analyze
select
id,
min(time)
from
test
group by
id
order by
id
;
GroupAggregate (cost=0.00..2291016.06 rows=5 width=12) (actual time=1166.705..5853.179 rows=5 loops=1)
-> Index Scan using test_pkey on test (cost=0.00..2287405.61 rows=722089 width=12) (actual time=0.077..5507.108 rows=637000 loops=1)
Total runtime: 5853.241 ms

--

drop function time_accum(timestamp);
drop aggregate work_time(time_accum);
drop function add_time(time_accum,time_accum);
drop type time_accum;

drop table test;

--

2 XM:

В вашем тесте ограничение в 5 секунд наверное выбрано не очень правильно, так как ему соответствует всего лишь 75 строк из миллиона.

С таким ограничением лучше использовать не мой первый незатюненый запрос, а второй затюненый запрос.

Пробовал пинать планы в вашем запросе (через group aggregate, index scan), более быстрого выполнения добиться не получилось. :(
...
Рейтинг: 0 / 0
Форумы / PostgreSQL [игнор отключен] [закрыт для гостей] / мастерам сиквела просвящается / 8 сообщений из 8, страница 1 из 1
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]