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 1eoWpI-0008CC-2E for pgsql-general@arkaria.postgresql.org; Wed, 21 Feb 2018 16:01:00 +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 1eoWpG-0003bl-Cf for pgsql-general@arkaria.postgresql.org; Wed, 21 Feb 2018 16:00:58 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1eoWpG-0003bZ-1q for pgsql-general@lists.postgresql.org; Wed, 21 Feb 2018 16:00:58 +0000 Received: from virtual1.potentialtech.com ([69.25.178.43] helo=mail.potentialtech.com) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eoWp9-0005aI-6j for pgsql-general@postgresql.org; Wed, 21 Feb 2018 16:00:56 +0000 Received: from wmoran1-M4800 (unknown [216.99.208.252]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.potentialtech.com (Postfix) with ESMTPSA id F386E24BEB7; Wed, 21 Feb 2018 11:00:49 -0500 (EST) Date: Wed, 21 Feb 2018 11:00:43 -0500 From: Bill Moran To: Alexander Farber Cc: pgsql-general Subject: Re: Not sure if I should CREATE INDEX for text columns on which I plan to filter later Message-Id: <20180221110043.ea9a7d8ad7ff5e522fde79d9@potentialtech.com> In-Reply-To: References: X-Mailer: Sylpheed 3.4.1 (GTK+ 2.24.23; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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. That being said, if you'll always use a WHERE on both state1 and state2, it will be even more performant to create a multicolumn index on those two columns instead of two single-column indexes. Even though Postgres _can_ use multiple indexes, using a single index is faster if it's available. -- Bill Moran