Re: Proposition for better performance - Mailing list pgsql-general

From Paul Jungwirth
Subject Re: Proposition for better performance
Date
Msg-id d617ebc2-3e63-ba2a-660f-318dfb08b2a6@illuminatedcomputing.com
Whole thread Raw
In response to Proposition for better performance  (hmidi slim <hmidi.slim2@gmail.com>)
List pgsql-general
(Including the list....)

On 03/27/2018 10:49 AM, hmidi slim wrote:
> Sorry I didn't copy it very well:
> create index idx on availability using gist(during);
> 
> and during = daterange(start_date,end_date)
> 
> And the query plan used was seq scan not index scan.

It sounds like there must be some important information missing. Here is 
my attempt to mock up some fake data:

     insert into availability
     select p.id, concat('prod ', p.id::text), daterange((now() + 
concat(r.v, ' days')::interval)::date, (now() + concat((r.v + 1 + 
random() * 21)::int, ' days')::interval)::date)
     from (select * from generate_series(1, 1000000)) p(id)
     cross join (select * from generate_series(1, 15)) n(i)
     join lateral (
       select p2.id, n2.i, (random() * 600 - 300)::int AS v
       from generate_series(1, 1000000) p2(id),
       generate_series(1, 15) n2(i)
     ) r
     on r.id = p.id and r.i = n.i
     ;

Then

     explain select * from availability where during @> 
daterange('2018-03-27', '2018-03-31');
                                      QUERY PLAN 

 
-----------------------------------------------------------------------------
      Index Scan using idx on availability  (cost=0.39..1644.41 rows=1 
width=552)
        Index Cond: (during @> '[2018-03-27,2018-03-31)'::daterange)
     (2 rows)

Running that query for real I get:

     ...
     (215044 rows)

     Time: 1450.099 ms (00:01.450)

So if the index isn't being used you'll have to do some digging to find 
out why.    
    
-- 
Paul              ~{:-)
pj@illuminatedcomputing.com


pgsql-general by date:

Previous
From: Adrian Klaver
Date:
Subject: Re: Postgresql 9.3 Server will not start after Ubuntu Upgrade
Next
From: Paul Jungwirth
Date:
Subject: Re: Proposition for better performance