Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lDRqr-0005r2-Ky for pgsql-general@arkaria.postgresql.org; Sat, 20 Feb 2021 12:59:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lDRqp-0000t2-Si for pgsql-general@arkaria.postgresql.org; Sat, 20 Feb 2021 12:59:11 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lDRqp-0000sv-Ld for pgsql-general@lists.postgresql.org; Sat, 20 Feb 2021 12:59:11 +0000 Received: from mail.hjp.at ([212.17.106.138] helo=rorschach.hjp.at) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lDRqm-0003lj-3w for pgsql-general@lists.postgresql.org; Sat, 20 Feb 2021 12:59:10 +0000 Received: by rorschach.hjp.at (Postfix, from userid 1000) id D40284507; Sat, 20 Feb 2021 13:59:05 +0100 (CET) Date: Sat, 20 Feb 2021 13:59:05 +0100 From: "Peter J. Holzer" To: pgsql-general@lists.postgresql.org Subject: Re: Please help to improve SELECT JOIN performance, EXPLAIN ANALYZE attached Message-ID: <20210220125905.GA30604@hjp.at> Mail-Followup-To: pgsql-general@lists.postgresql.org References: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="82I3+IH0IqGh5yIs" 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 --82I3+IH0IqGh5yIs Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On 2021-02-19 21:25:36 +0100, Alexander Farber wrote: > In the PostgreSQL log I have noticed that the duration for 2 particular q= ueries > have increased, especially in the evenings: [...] > One of the queries is actually a simple SELECT ... LIMIT 15 to find the 15 > longest words played by a user (they are displayed at the player profile = page). >=20 > I have run the EXPLAIN ANALYZE here:=A0https://explain.depesz.com/s/IcqN Well, I wouldn't call a query with 8 joins "simple".=20 I notice two things: 1) It does a lot more than the description ("Select 15 longest words played by the user id 5") suggests. It not only selects those words, but also the games in which they were played and some information about both players. 2) The query seems to spend most of the time in https://explain.depesz.com/s/IcqN#l11, retrieving all the words ever played by that user via a parallel bitmap heap scan, only to sort them and discard all but the 15 longest. I think using an index should make that much more efficient. Therefore, three suggestions: 1) Add an index on words_scores(uid, length(word) desc). 2) If postgresql still isn't using that, isolate the core query by putting it into a CTE: with s as (select * from words_scores where uid =3D 5 order by length(w= ord)) select ... from s join words_moves m ... 3) If that shifts the bottleneck towards that extra per-user info (location, photo, ...) try to cache that in the application. That probably doesn't change very often and doesn't have to be retrieved from the database every time. hp --=20 _ | Peter J. Holzer | Story must make more sense than reality. |_|_) | | | | | hjp@hjp.at | -- Charles Stross, "Creative writing __/ | http://www.hjp.at/ | challenge!" --82I3+IH0IqGh5yIs Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAmAxB5UACgkQ8g5IURL+ KF08NA/+PWgPeodbwQ/+ZFT0OpLkVwfZmqzirlWpinWKcri0RcrkixSMc7Utd03z 067InMoogXS7MaH3eLTkgaVTqUPkaJpKryOTWY4yKvqzkzwQTvVkNgSElEZ6avt0 HKY3EqLaN92N9L4qM+0LXahXg04+gDHItjyu/rdO21EjJTYk9zOIfPPMIQCpEpYx g63yLKeDdkrwEy6K3JjYnYeh/Zb3hikJHbBrGZSg2x3ZEhtSQ/dxzoxyzBw3MLcH eGH5rr4DeunxtUMa5NgP+RZA8IqXZ6k+5j5zlXtX0E1tewl0rCxwmVQnTD5a92dI tF+U2DBfd8QrVsg4DUScQuxW8TY1DSCGiJ9wODilKDG9OkpjigM+85Fk8U4nbj7o qdbE1qMXNX4QYuOKhDkhMqJPpDmniOgl6DNeMX0fiGl8bje4YTbADZBV5ZFkhTSK z9W61Al7QcF+7RV79rWs5Bwqm0qr2aoWLz08qHOCNFnFRVsGJwRyOD6mQUAVD7NU cuhHO2kcDy0yodI2XB5CfGK4bIpyIi0PxO2ImS1fBO2BW/9E5UkKqH+rU4QA65um Yo8T7m25ULbQdGeNrvHyNE6q6G+bZNTn5UoET4iw9toWTA2menE32TK820jN6kd6 0tLy6DlklYaoV5mab5kpu2RVj2fAL6jSKkZ7mvSIUIgmFaycCoc= =T9+S -----END PGP SIGNATURE----- --82I3+IH0IqGh5yIs--