From: Tomas Vondra <tomas.vondra@2ndquadrant.com>
To: Bill Moran <wmoran@potentialtech.com>
To: Alexander Farber <alexander.farber@gmail.com>
Cc: pgsql-general <pgsql-general@postgresql.org>
Subject: Re: Not sure if I should CREATE INDEX for text columns on which I plan to filter later
Date: Thu, 22 Feb 2018 03:57:34 +0100
Message-ID: <4f6e0a35-b4e7-1062-a7c0-4881d489fc33@2ndquadrant.com> (raw)
In-Reply-To: <20180221110043.ea9a7d8ad7ff5e522fde79d9@potentialtech.com>
References: <CAADeyWi2b_nO+SquOz7sSF2iaAyM2WhJjKX8bQdFYuDhewJqKw@mail.gmail.com>
<C727B340-40F8-4E18-846A-C1E6F14181D8@avbrief.com>
<CAADeyWiEQvKv5oA2q+F6auRDzk3svBL1=KK_wKSd+tNmrC5R9w@mail.gmail.com>
<20180221110043.ea9a7d8ad7ff5e522fde79d9@potentialtech.com>
On 02/21/2018 05:00 PM, Bill Moran wrote:
> On Wed, 21 Feb 2018 13:33:18 +0100
> Alexander Farber <alexander.farber@gmail.com> wrote:
>
>> Hi Martin -
>>
>> On Wed, Feb 21, 2018 at 1:26 PM, Martin Moore <martin.moore@avbrief.com>
>> wrote:
>>
>>> I’m no expert but I’d think it unlikely an index would be considered for a
>>> table with only 100 rows in. Also I’m pretty sure only one index per table
>>> is used, so you’d want to put state1 and state2 in one index.
>>
>> I hope to have more records in the words_games table later when my game is
>> launched (currently in soft launch/beta).....
>
> To elaborate+clarify Martin's comments.
>
> The index will not be used while the table is small because it's actually slower
> to process an index than it is to just read the entire table. However, as the
> table gets more rows, these timings will reverse and Postgres will start using
> the indexes. It's probably best to just create them even though the table is
> small. The performance improvement you'll get when the table grows will be
> well worth it, and it avoids the problem of trying to remember to create it later.
>
> However, Martin's other comment about only using a single index is incorrect.
> Postgres can use multiple indexes per query, so it's often good practace to
> put indexes on every column that might ever be used in a WHERE clause.
>
I call this practice "shotgun" and generally discourage people from
using it. It seems attractive, but not every where condition can be
evaluated using an index, and every index has maintenance overhead.
There are cases when it's somewhat reasonable (e.g. when you don't know
which columns will be referenced by WHERE conditions, and data ingestion
has lower priority than queries). But that doesn't seem to be the case
here - you know the WHERE conditions, and people are likely sending a
lot of inserts (and expecting low latency responses).
regards
--
Tomas Vondra http://www.2ndQuadrant.com
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: tomas.vondra@2ndquadrant.com, wmoran@potentialtech.com, alexander.farber@gmail.com
Subject: Re: Not sure if I should CREATE INDEX for text columns on which I plan to filter later
In-Reply-To: <4f6e0a35-b4e7-1062-a7c0-4881d489fc33@2ndquadrant.com>
References: <CAADeyWi2b_nO+SquOz7sSF2iaAyM2WhJjKX8bQdFYuDhewJqKw@mail.gmail.com> <C727B340-40F8-4E18-846A-C1E6F14181D8@avbrief.com> <CAADeyWiEQvKv5oA2q+F6auRDzk3svBL1=KK_wKSd+tNmrC5R9w@mail.gmail.com> <20180221110043.ea9a7d8ad7ff5e522fde79d9@potentialtech.com> <4f6e0a35-b4e7-1062-a7c0-4881d489fc33@2ndquadrant.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox