Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f0tHu-00042d-Fo for pgsql-general@arkaria.postgresql.org; Tue, 27 Mar 2018 18:25:38 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1f0tHt-0001SF-4i for pgsql-general@arkaria.postgresql.org; Tue, 27 Mar 2018 18:25:37 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f0tHs-0001S5-Sp for pgsql-general@lists.postgresql.org; Tue, 27 Mar 2018 18:25:36 +0000 Received: from mail-pl0-x236.google.com ([2607:f8b0:400e:c01::236]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1f0tHq-0003aH-M2 for pgsql-general@postgresql.org; Tue, 27 Mar 2018 18:25:36 +0000 Received: by mail-pl0-x236.google.com with SMTP id x4-v6so14615041pln.7 for ; Tue, 27 Mar 2018 11:25:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=illuminatedcomputing-com.20150623.gappssmtp.com; s=20150623; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=RUjU0qf6Z7C7abHTgjQzthsKsdQ9Nt9s4xawkQLy5ks=; b=R+3TGgj2w+lTIkKLXRaem23m9+gsYHH6OavHkblcT0/idUumPY+n2YV44eSrKKwIe8 QX4wr37OrXM4QpIOMNDmthArKhFyRoT7X+YxqxGTFdyRqWgzODxt/Ez7LZKIg7fdKugQ mMtYbxxiKnHzKgCr37h3c7yVyE7CXNnx/yCY/gOAq4L1wePjQo0JasUsv/WHWKggFfdi kbG6C/GgwCZUhBREI1SrveL90ljzoBBnB9H0orh3FJI7TkIdEmgGsdcp4vATEMY7tTZO qmZ7mDkGcnSTDjjr6YzF2sbruGRXAXeT2M4YSTC71Z0m+wCrpauNkbXjwUsXDpQMXVr5 5NfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=RUjU0qf6Z7C7abHTgjQzthsKsdQ9Nt9s4xawkQLy5ks=; b=SG3JfUlwmi1jrlADl2Gv+1WYzJuSaM2sUuXGh/KojszoIuxp+hJv38px+Nh6Qnlil9 N5E9iCjWNHRVkp4Juta23AjHnQVK/8FkEvjb8IGdO73K3N7bV9nLrjp2NFKaqb1BY12E Avf8N6YdYRSpR4e0le27v3CGp/cq+pTEc6itDcF7kTpcUVIMqNi15UL0cmjAl3DwIYB2 MePpQ0j0rr6qjGcpHidBnb/FjF7hwtqgE0KbyuXDviPTaVbFbTiglzoRLzhSMRNv2PzB ER7lJdhxLqeNfaaoFjLptzPD4qGznxHSrQfFaQIY/xLD8+zoeuueSy0ZNU5ajMz47nY+ ctuA== X-Gm-Message-State: AElRT7Fo2On0RUi86QdhL/j3f7axnUW1v8bRiiVxqnS6outQt8q5sK6I 2xCp3ZxvVYSPwo8ggTi+VbTlpSOh X-Google-Smtp-Source: AIpwx4/vIAU7hBgui0mAl+OsmMftTc5X8SYhFrY065QLYdf8A72z4HvUTtoK3EfHWN+GEh1BgYtnLg== X-Received: by 2002:a17:902:68c2:: with SMTP id x2-v6mr379477plm.129.1522175131489; Tue, 27 Mar 2018 11:25:31 -0700 (PDT) Received: from [192.168.2.201] (static-50-53-109-254.bvtn.or.frontiernet.net. [50.53.109.254]) by smtp.googlemail.com with ESMTPSA id a9sm4959905pfe.147.2018.03.27.11.25.30 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 27 Mar 2018 11:25:30 -0700 (PDT) Subject: Re: Proposition for better performance To: hmidi slim , "pgsql-general@postgresql.org" References: <2e436152-eda0-d67d-4b20-90da0db4a049@illuminatedcomputing.com> <834d8a70-386d-7101-f846-ae572140af92@illuminatedcomputing.com> From: Paul Jungwirth Message-ID: Date: Tue, 27 Mar 2018 11:25:30 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk (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