Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sXFnT-002jfw-7r for pgsql-general@arkaria.postgresql.org; Fri, 26 Jul 2024 07:55:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sXFnR-007YkR-NP for pgsql-general@arkaria.postgresql.org; Fri, 26 Jul 2024 07:55:25 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sXFnR-007YkJ-8r for pgsql-general@lists.postgresql.org; Fri, 26 Jul 2024 07:55:25 +0000 Received: from mail-ej1-x633.google.com ([2a00:1450:4864:20::633]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sXFnO-001Vd7-0H for pgsql-general@lists.postgresql.org; Fri, 26 Jul 2024 07:55:23 +0000 Received: by mail-ej1-x633.google.com with SMTP id a640c23a62f3a-a7979c3ffb1so85340566b.2 for ; Fri, 26 Jul 2024 00:55:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=peoplecall-com.20230601.gappssmtp.com; s=20230601; t=1721980520; x=1722585320; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=AtN4lf2StVVG5yJEhCKbDmZLfGLdvbx70g1JI2UHkMk=; b=0uGoVrfJQZ9Ycon4kR/W4qyg6+1XWjtob4Kucey61yCR2j8SLbMbh0Z9M+cwUiNFkl JMhYSYQRg3Ee0QzV14QvD1huFYcnnfx84Ap28qfOjJ6E0tGI31xQVNVpFKV/pfE4Tk3B iMZ/iNNuNwoet5Q/1je+GcFhJB31MpZerTSpSYY1yF08D9y+PNOzN2KOLOWZgLqnIfOY ixPbNzqDpzThTvUOBrSyaF3oTIvCVbiELfWG4wWwYevFpGLhoLrrxruEgTgvDKP/fovM u+RcuNNGHGKpY8F1MDOeMqQwPaVY+FlUFV0pCfnInXD9oyBJO6s057Einlga8xRznXer uVGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721980520; x=1722585320; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=AtN4lf2StVVG5yJEhCKbDmZLfGLdvbx70g1JI2UHkMk=; b=FQSP9SDlFVky54vUu5FNWuevHTRHOCutK3tf3pBRgu6kI5jWRrb0KhKAdxpUjfOSIG P7An1cO2i5/Ww5ceUNhsU0U1mH5a4ImVkALvArDOPvYCM5386kVTIp66AVQLRkWGR5Fh E2oYrSYRbCoR7nCR+XFFcGUhm0GABLgT6RIElAsgnn7y2C7QzV/kG3wt0RQ1y/pZ5jEh zI6dSVaQKIcPEiFZ8vrQZ+08Fh2fhjFOQ+7liR2mdyOWsoDcOEhP0B8zvMhokIZBLiLI dm1Xmibns8SCjlaO2DludOxz7edkC9Zil8EqPPzaUyAyj+qb66splmHv4Ik6IvKI8UW5 oWFg== X-Gm-Message-State: AOJu0YxR478z3p0WR1Nw+wkhJE3aqjigq50PjceQoQejECUvITaEtE/G RmAB+MSV0GYoMfhM2vlsXVzYb8UK26r+5fycBmphrmlQT58Taj/HOBzUIckhMB57TU7y021OJfK cBABIutN58yQHCOhd6XVzjha2g6/t+Rbk90CbgB0lcUrzCJAdNw== X-Google-Smtp-Source: AGHT+IFw3eStUs8DH+Tgn6aM6CsOqhYZEkuFtAyAAxZqCkebWDkBBc1C4fnrCPokRUw7GlGlmVVBgAAUrF7BsHinqms= X-Received: by 2002:a50:d65b:0:b0:5a2:80f:a6dd with SMTP id 4fb4d7f45d1cf-5ac62525b15mr3941746a12.14.1721980519856; Fri, 26 Jul 2024 00:55:19 -0700 (PDT) MIME-Version: 1.0 References: <431223779.107307.1721971823963.ref@mail.yahoo.com> <431223779.107307.1721971823963@mail.yahoo.com> In-Reply-To: <431223779.107307.1721971823963@mail.yahoo.com> From: Francisco Olarte Date: Fri, 26 Jul 2024 09:54:43 +0200 Message-ID: Subject: Re: Slow performance To: "sivapostgres@yahoo.com" Cc: Postgresql General Group Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hello: On Fri, 26 Jul 2024 at 07:31, sivapostgres@yahoo.com wrote: ... > Took backup (pg_dump) of first database (client_db) and restored the database as second database (client_test). ... > The query when run against DB1 takes around 7 min 32 seconds. > The same query when run against DB2 takes around 124 msec. > Same computer, same PG cluster, same query. > Why it takes so much time when run against DB1 (client_db)? Can be bad luck, but the usual suspect would be different databases. I assume db1 is quiescent on the tests ( as it seems the production database, no heavy querying concurrent with your tests ). Bear in mind restoring leaves the database similar to what a vacuum full will do, so it can differ a lot from the original. > Already executed vacuum against client_db database. I think you already have pointed out this, but IIRC you have not told us if you have ANALYZED any of the databases. This is important. Bad stats in any of them could make the planner choose a bad plan ( or, if you are unlucky, make it choose a bad one ). Also, did you vacuum verbose? where your tables well packed? ( bad vacuuming can lead to huge tables with a lot of free space, but I doubt this is your case, but everything has to be checked, we only know what you write us ). And now, not being an expert in tracing explain I see this in plan-db1: " Join Filter: (((b.registrationnumber)::text = (p.registrationnumber)::text) AND ((c.subjectcode)::text = (p.subjectcode)::text) AND (a.semester = p.semester))" " Rows Removed by Join Filter: 13614738" " -> Index Scan using ""cl_student_semester_subject_IX3"" on cl_student_semester_subject p (cost=0.55..8.57 rows=1 width=60) (actual time=0.033..55.702 rows=41764 loops=1)" " Index Cond: (((companycode)::text = '100'::text) AND ((examheaderfk)::text = 'BA80952CFF8F4E1C3F9F44B62ED9BF37'::text))" Not an explain expert, but if i read correctly an index scan expecting 1 row recovers 41674, which hints at bad statistics ( or skewed data distribution and bad luck ) The plans are similar, but in the fast query cl_student_semester_subject is accessed using other index: " -> Index Scan using ""cl_student_semester_subject_IX1"" on cl_student_semester_subject p (cost=0.42..3.09 rows=1 width=60) (actual time=0.010..0.010 rows=1 loops=326)" " Index Cond: (((companycode)::text = '100'::text) AND ((subjectcode)::text = (a.subjectcode)::text) AND ((registrationnumber)::text = (a.registrationnumber)::text) AND (semester = a.semester))" Which seems much more selective and recovers just what it wants. I would start by analyzing ( and, if not too costly, reindexing ) that table. Francisco Olarte.