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 1f0qr5-00025t-O3 for pgsql-general@arkaria.postgresql.org; Tue, 27 Mar 2018 15:49:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1f0qr4-0002Gv-Lh for pgsql-general@arkaria.postgresql.org; Tue, 27 Mar 2018 15:49:46 +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 1f0qr4-0002Gm-BH for pgsql-general@lists.postgresql.org; Tue, 27 Mar 2018 15:49:46 +0000 Received: from mail-pl0-x22a.google.com ([2607:f8b0:400e:c01::22a]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1f0qqx-00087Q-PB for pgsql-general@lists.postgresql.org; Tue, 27 Mar 2018 15:49:45 +0000 Received: by mail-pl0-x22a.google.com with SMTP id b7-v6so14343050plr.8 for ; Tue, 27 Mar 2018 08:49:39 -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=HU/GX0m54OmD0vThVYPgcBdfc8LauhKgb4x/0RqsUL0=; b=HMDVo7gYQD8ltHpl/sAjfebBiWeqKGAotsLyv21uUfZOcYcQ8kxufR4UsEYkQjXxpD MuNL8eymlfU68O4vTHsQjmVk0/h9PiyOkKPrvtmHV8Ts38HIIPyN8ocezvcA5iD7jWsB KdlT/sFsXhaPbRo1prB+VncbMYxVUXVHFtiD0tD4Z93rN+mLvv/8Ub0iGZhJNTbFCU6R pP9Arz+Fh3inMuAhxsYaRQe17YAlwxMyfmUUTsKuBQBc8HFK32OBQ+O8WGmFGgj1JM9+ zyAnkwDp9x1rcOu6NqOFc480z6bdRuqEETjIjuPi580HHUbXf/Mx14QtEAsm6PACnw3F A4CQ== 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=HU/GX0m54OmD0vThVYPgcBdfc8LauhKgb4x/0RqsUL0=; b=ujuV8oJ9EXaU08PhMKepf6eXDh3jOGinEXgopilET296gSrvV48XDF2tr086VM2itW yZqXER9Qyg6Km+tw24Xq9YFQRp7jBSKuijlWPfrf+PcnzNz66SaH5u72nLFbPQWJCy9R FSgz9LSxOVM4A/zl2UK1uJq3qCOKuBKgyRj5Lax7vagaGftHJYaX3mVJooS4Arem6ftg 1NcyV895eLJTNssREo2gDBcn2mCxTVy7C1hEg2j5wKuuvobv+Uql4Kb1t/frNNG6dhEP ikAubaPwlDZj+Kkafu60nfe/X0Xa8G5iA6eCJdhac0A7OWkhdMnMl5ygnP4vnOuDkQik v8zg== X-Gm-Message-State: AElRT7EnLkZb3lUoOcUCcB/MphzI6L2Krkz36J2ldYVIB9+pYPFi1urN VAaBGrH1fwgC2za65ur3I74r8jHO X-Google-Smtp-Source: AIpwx4/IHybqqe8krrNH7h5T8Wmhnv2yeprmNPKX30IDCy34U9vxSLhzx5VV9lhHMLZz6t5VJgsaow== X-Received: by 2002:a17:902:7793:: with SMTP id o19-v6mr9059336pll.151.1522165776582; Tue, 27 Mar 2018 08:49:36 -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 b65sm4679205pfl.145.2018.03.27.08.49.35 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 27 Mar 2018 08:49:35 -0700 (PDT) Subject: Re: Proposition for better performance To: pgsql-general@lists.postgresql.org References: From: Paul Jungwirth Message-ID: <2e436152-eda0-d67d-4b20-90da0db4a049@illuminatedcomputing.com> Date: Tue, 27 Mar 2018 08:49:35 -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 On 03/27/2018 07:42 AM, hmidi slim wrote: > Hi, > I'm trying to create an availability table for existing products. I'm > fetching the suitable schema to design in order to get good performance > when I fetch products in a table contains millions of rows. > I think to make a schema like this: > *create table availability (product_id integer, product_name > varchar(255), start_date date, end_date date)*. I would use a tstzrange (or daterange) instead of separate start_date and end_date columns. Then you can create an exclusion constraint that has `EXCLUDE USING gist (id WITH =, available_during WITH &&)`. That will automatically add a GiST index on those columns that should help with faster lookups. (It will also prevent contradictions where a product has two overlapping rows.) You didn't mention the queries you want to be fast, but that index should cover the reasonable ones I think. > Is there any solution to use such as range types or anything which > mentions the unavailable dates. For any product, there should be the same number of unavailable periods as available, right---or often one more? So I don't see any performance benefit in doing it that way, and it certainly seems less intuitive to store when something is *not* available. -- Paul ~{:-) pj@illuminatedcomputing.com