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 1nAbjX-0004zp-Mk for pgsql-general@arkaria.postgresql.org; Thu, 20 Jan 2022 18:00:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nAbjW-0008G0-C6 for pgsql-general@arkaria.postgresql.org; Thu, 20 Jan 2022 18:00:26 +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 1nAbjW-0008Fr-1g for pgsql-general@lists.postgresql.org; Thu, 20 Jan 2022 18:00:26 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nAbjS-0006eV-Ri for pgsql-general@lists.postgresql.org; Thu, 20 Jan 2022 18:00:25 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 20KI0HU3891560; Thu, 20 Jan 2022 13:00:18 -0500 From: Tom Lane To: Ekaterina Amez cc: pgsql-general@lists.postgresql.org Subject: Re: Query much slower from php than psql or dbeaver In-reply-to: References: Comments: In-reply-to Ekaterina Amez message dated "Thu, 20 Jan 2022 15:31:06 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <891558.1642701617.1@sss.pgh.pa.us> Date: Thu, 20 Jan 2022 13:00:17 -0500 Message-ID: <891559.1642701617@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Ekaterina Amez writes: > I've tested the query with psql and DBeaver and it takes only milliseconds: > it returns 39 records and now there's only 16000 records on the table but > I've tested it with <100K. When I've tested my php script the same query > takes 14 minutes to return (more or less). A plausible theory is that it's not really the same query, but differs in having some values presented as parameters not literal constants. Depending on exactly how you do it, that can confuse the planner leading to a poor execution plan and a long runtime. I'd first try enabling log_statement on the server to see if the query is really being presented exactly the same way. Another thing worth trying is auto_explain, to capture the plans actually being used. regards, tom lane