Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1idvAD-0003Du-Pt for pgsql-general@arkaria.postgresql.org; Sun, 08 Dec 2019 11:55:49 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1idvAC-0001mm-9F for pgsql-general@arkaria.postgresql.org; Sun, 08 Dec 2019 11:55:48 +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_SHA1:256) (Exim 4.89) (envelope-from ) id 1idvAB-0001mf-V4 for pgsql-general@lists.postgresql.org; Sun, 08 Dec 2019 11:55:47 +0000 Received: from mail.hjp.at ([212.17.106.138] helo=rorschach.hjp.at) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1idvA7-0007GD-8d for pgsql-general@lists.postgresql.org; Sun, 08 Dec 2019 11:55:46 +0000 Received: by rorschach.hjp.at (Postfix, from userid 1000) id 80F07186E; Sun, 8 Dec 2019 12:55:41 +0100 (CET) Date: Sun, 8 Dec 2019 12:55:41 +0100 From: "Peter J. Holzer" To: pgsql-general@lists.postgresql.org Subject: Re: CREATE INDEX ON words_moves(length(letters), score) WHERE action = 'play' Message-ID: <20191208115541.GA12402@hjp.at> References: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="YZ5djTAD1cGYuMQK" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --YZ5djTAD1cGYuMQK Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On 2019-12-07 16:20:44 +0100, Alexander Farber wrote: > in PostgreSQL 11 I have a table holding player moves (could be: 'play', '= swap', > 'skip', ...) in a word game: >=20 > # \d words_moves; > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 Table "public.words_moves" > =A0Column =A0| =A0 =A0 =A0 =A0 =A0 Type =A0 =A0 =A0 =A0 =A0 | Collation |= Nullable | =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 > Default =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 > ---------+--------------------------+-----------+----------+-------------= ----------------------------- > =A0mid =A0 =A0 | bigint =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 | =A0 =A0 =A0= =A0 =A0 | not null | nextval > ('words_moves_mid_seq'::regclass) > =A0action =A0| text =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 | =A0 =A0 =A0= =A0 =A0 | not null | > =A0gid =A0 =A0 | integer =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0| =A0 =A0 =A0= =A0 =A0 | not null | > =A0uid =A0 =A0 | integer =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0| =A0 =A0 =A0= =A0 =A0 | not null | > =A0played =A0| timestamp with time zone | =A0 =A0 =A0 =A0 =A0 | not null | > =A0tiles =A0 | jsonb =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0| =A0 =A0 =A0= =A0 =A0 | =A0 =A0 =A0 =A0 =A0| > =A0score =A0 | integer =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0| =A0 =A0 =A0 = =A0 =A0 | =A0 =A0 =A0 =A0 =A0| > =A0letters | text =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 | =A0 =A0 =A0 = =A0 =A0 | =A0 =A0 =A0 =A0 =A0| > =A0hand =A0 =A0| text =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 | =A0 =A0 = =A0 =A0 =A0 | =A0 =A0 =A0 =A0 =A0| > =A0puzzle =A0| boolean =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0| =A0 =A0 =A0 = =A0 =A0 | not null | false [...] > When I search for "interesting moves" with score higher than 90 or all 7 = tiles > played, then the query takes a bit longer: >=20 > EXPLAIN ANALYZE=A0 > SELECT=A0 [...] > FROM words_moves=A0 > =A0WHERE action =3D 'play' =A0 > AND LENGTH(hand) =3D 7=A0 > AND (LENGTH(letters) =3D 7 OR score > 90)=A0 > AND played > CURRENT_TIMESTAMP - interval '1 year'=A0 > AND played < CURRENT_TIMESTAMP - interval '3 day' =A0 > =A0ORDER BY played DESC; > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0= =A0 =A0 =A0 [...] > Then I drop that index and create another one: >=20 > CREATE INDEX ON words_moves(length(letters), score) WHERE action =3D 'pla= y'; >=20 > And it seems to have a better performance on the query: >=20 > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0= =A0 =A0 > QUERY PLAN =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 > =A0 =A0 =A0 =A0 =A0 =A0 > -------------------------------------------------------------------------= ---------------------------------------------------------------------------= --------------------- > =A0Sort =A0(cost=3D97687.61..97687.72 rows=3D44 width=3D97) (actual time= =3D175.832..175.927 > rows=3D1036 loops=3D1) > =A0 =A0Sort Key: played DESC > =A0 =A0Sort Method: quicksort =A0Memory: 194kB > =A0 =A0-> =A0WindowAgg =A0(cost=3D97684.98..97686.41 rows=3D44 width=3D97= ) (actual time=3D > 172.443..174.937 rows=3D1036 loops=3D1) > =A0 =A0 =A0 =A0 =A0-> =A0Sort =A0(cost=3D97684.98..97685.09 rows=3D44 wid= th=3D57) (actual time=3D > 172.390..172.490 rows=3D1036 loops=3D1) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Sort Key: (to_char(played, 'Mon YYYY'::tex= t)) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Sort Method: quicksort =A0Memory: 129kB > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0-> =A0Bitmap Heap Scan on words_moves =A0(= cost=3D55092.96..97683.78 > rows=3D44 width=3D57) (actual time=3D165.420..1= 71.164 rows=3D1036 loops=3D1) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Recheck Cond: (((length(letter= s) =3D 7) AND (action =3D > 'play'::text)) OR ((score > 90) AND (action =3D= 'play'::text))) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Filter: ((length(hand) =3D 7) = AND (played > > (CURRENT_TIMESTAMP - '1 year'::interval)) AND (played < (CU= RRENT_TIMESTAMP - '3 > days'::interval))) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Rows Removed by Filter: 468 > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Heap Blocks: exact=3D1495 > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0-> =A0BitmapOr =A0(cost=3D5509= 2.96..55092.96 rows=3D15280 width=3D0) > (actual time=3D165.036..165.036 ro= ws=3D0 loops=3D1) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0-> =A0Bitmap Index= Scan on > words_moves_length_score_idx =A0(cost=3D0.00..275.71 rows=3D1483= 8 width=3D0) (actual > time=3D0.620..0.620 rows=3D912 loops=3D1) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Index = Cond: (length(letters) =3D 7) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0-> =A0Bitmap Index= Scan on > words_moves_length_score_idx =A0(cost=3D0.00..54817.23 rows=3D44= 2 width=3D0) (actual > time=3D164.413..164.413 rows=3D608 loops=3D1) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Index = Cond: (score > 90) > =A0Planning Time: 0.948 ms > =A0Execution Time: 177.604 ms > (19 rows) >=20 > Here the resulting link:=A0https://explain.depesz.com/s/pmCw >=20 > I still wonder, what am I missing, what could be improved there? Several ideas: 1. You are only interested in moves from the last year or so. How much history do have stored in your table? If it's much more than one year, then an index on played will help. However, it looks like that filter removes at most 468 of ~ 1500 rows, so that doesn't seem to be case (yet). 2. You spend now most of the time scanning words_moves_length_score_idx for scores > 90. This is basically a full index scan since that index is ordered by length(letters). I'm surprised that PostgreSQL can even do that :-). This is a separate index scan than the one for length(letters) =3D 7, so separating the indexes should be no worse and probably a lot better. Create an index on score (possibly conditional on action =3D 'play'). 3. A lot of the conditions is fixed. So you might want to move them into the condition of a partial index: create index on words_moves(played)=20 where action =3D 'play' and LENGTH(hand) =3D 7 and (LENGTH(lett= ers) =3D 7 OR score > 90); Then the planner should recognize that it can use this index and the index contains only interesting moves - no logical operations needed at query time at all, only an index scan to find recent moves. Be warned though that creating such ultra-specific indexes comes at a cost: You may end up with a lot of them if you have many different queries and maintaining them may make inserts noticably slower. hp --=20 _ | Peter J. Holzer | Story must make more sense than reality. |_|_) | | | | | hjp@hjp.at | -- Charles Stross, "Creative writing __/ | http://www.hjp.at/ | challenge!" --YZ5djTAD1cGYuMQK Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAl3s5LgACgkQ8g5IURL+ KF2VOQ//X/n2jx6d03vGu7v0YR2feDz0alqlbfpG2LmJabMLD6GhLOFzqm42ozgx jWxbZ5RnCLZr8VzB6Mo4FIPuqmpgBBsGvlxQS/rEUM3SZJKEdRaNEE+whPO4CYrc B5nqCkBMzLKceKqzKEmnc00gEiJ27H5jo91GoktQp7J+LYu1MbUI25BCbCwsflBM PJH40W6w0sdD73EbIVfm8JGfpqR2HuUSF1yqBS90losQnGrtJ1z85ZmBjpVoiEwb jxVGvVg69hJrbNB0q5Qs8Lcw6ZesUd8XeRptrP1RYTyAlbXaMPjWc6DGMPGr614Z 77yy2cLshZS5k9d6wlmVwuZGyy4YwGofSoSoOB0ECjyRM05/xMGPoBXlHXnGjRZ3 V5jqNRYmOj0i/+vXgz65oppUkrSkY82FijOyg5bkAVmsBGUN6fKUe4pxAcQBDm+s 3uQMWURZ9qi9KwMCsnqDEYTZUZlR/XNO22WZUpFJdEZumiTKx7YOfJ+sETtf0Z2Z bqM8zfd36E0aP7XA2nGVEOA5fPO18v0gxESL1Tws0tA/0MeeuD5T3eUqhRAVrqFK j7TERib4NCW+2rD15jBZ1bapTXhMFW17Te5gDhHhPP39MLQZzuaCw3vwTJ6gOExf js7DFCRgjDMRR3JuRRN+qE/XZ3oA+H9ID4SfHGKgRDxIBQvy4wA= =aLcD -----END PGP SIGNATURE----- --YZ5djTAD1cGYuMQK--