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 1eoh4s-00026K-Df for pgsql-general@arkaria.postgresql.org; Thu, 22 Feb 2018 02:57:46 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eoh4r-00046f-4z for pgsql-general@arkaria.postgresql.org; Thu, 22 Feb 2018 02:57:45 +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.84_2) (envelope-from ) id 1eoh4q-00046U-TD for pgsql-general@lists.postgresql.org; Thu, 22 Feb 2018 02:57:44 +0000 Received: from mail-wr0-x22d.google.com ([2a00:1450:400c:c0c::22d]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1eoh4m-00047i-Uq for pgsql-general@postgresql.org; Thu, 22 Feb 2018 02:57:43 +0000 Received: by mail-wr0-x22d.google.com with SMTP id l43so9078851wrc.2 for ; Wed, 21 Feb 2018 18:57:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=20MPay/F+9z/i+rCurhwhawPBqinC+FPv2V1WyyELec=; b=yxavLEUi/cbv0bpRuYIwsU1hxJaluES0F7gq8yeu2G5fxZjiibqzxM7b33ln8Z52ju 0T+a/CjXBiQ8t4DChJYKcUaSuEMh8Nd8sdp4tPIqAfT7UbKOCX4FuP0neGvXH2UWmgrL hKoNTzvpPp9+BIznVSRPjxOMt7uIrDOEfElhruTeZpsMW6nqb9QAMr0e6D2EDJD5XPcZ sVTQ4/W5mj7MpgYlXjQy6PvKtuIpzIrbSdfw2Y0o5c8592eFBULYP2j7qn5XGJD96j4O io+Y840M/AoML3O5W2JIBeUAPRvTVDBjd/0BRtfYVraoFRSw36Y/4vbk+DJWmZh3rkDb MiMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=20MPay/F+9z/i+rCurhwhawPBqinC+FPv2V1WyyELec=; b=cYRzVMlzmWkay7wt+0Q/KBfNrZt0lI/L2hXhsGZw5dwXezlQrnLmaMAhVUZFwTF/AK YRlPoiYVSa5uht2PNE5rbVBYJyKISbR95oB/CXH27yPoFPuSMOfLuGD1gvzoHGW28O89 bydaF9/3Wjm4Udju48sbBSaThtvX69GQ3Mx2mniZZ0eIymukb3Jqp9ojj90+njMsikm9 qADB2alNHieG8oZlxqVvKLbAhVFeWy0yONYOU9pM78C2hvHG8xD5vbE5OmLdqU5hewDX /nUtjOCNK+/3DX7WNBeCjb2Xlo5L5CyTuOQDeTCY8PziHlyzTXk7RRy8611t6DnQSF7Y /gxA== X-Gm-Message-State: APf1xPDrnWDTVmwMSxkv+dLaK9g8wZlV8SQVl7ub0fgmFnIkPwkhPVpq 5L0BmMwRlLPe958GupLokyUdJ+ziOr3uojTlcx7CY10dc3vfBY87G0ZbBXNBO5bYwFfF4PAUg0d MQRq/eXzTogzPL4jJ0Gw9lPvVkb9QQOW84KM7IZqBZMMvamJsyYs2DHPMuGHAU2eatntIwviscp ZYpbesgGnI X-Google-Smtp-Source: AH8x225qw1egnIQSiYYW4u8s3VlKf1akinCM6Fl4cG4HHRclmFU2La/IDeYqy3RjSg38gfaQF1muZg== X-Received: by 10.223.164.158 with SMTP id g30mr4461842wrb.62.1519268259464; Wed, 21 Feb 2018 18:57:39 -0800 (PST) Received: from [10.137.2.19] (ip-78-102-97-226.net.upcbroadband.cz. [78.102.97.226]) by smtp.gmail.com with ESMTPSA id 188sm1261045wmx.12.2018.02.21.18.57.38 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 21 Feb 2018 18:57:38 -0800 (PST) Subject: Re: Not sure if I should CREATE INDEX for text columns on which I plan to filter later To: Bill Moran , Alexander Farber Cc: pgsql-general References: <20180221110043.ea9a7d8ad7ff5e522fde79d9@potentialtech.com> From: Tomas Vondra Message-ID: <4f6e0a35-b4e7-1062-a7c0-4881d489fc33@2ndquadrant.com> Date: Thu, 22 Feb 2018 03:57:34 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0 MIME-Version: 1.0 In-Reply-To: <20180221110043.ea9a7d8ad7ff5e522fde79d9@potentialtech.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 02/21/2018 05:00 PM, Bill Moran wrote: > On Wed, 21 Feb 2018 13:33:18 +0100 > Alexander Farber wrote: > >> Hi Martin - >> >> On Wed, Feb 21, 2018 at 1:26 PM, Martin Moore >> 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