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 1oBLKC-0002Tn-Ed for pgsql-admin@arkaria.postgresql.org; Tue, 12 Jul 2022 19:13:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oBLKA-0004Re-Io for pgsql-admin@arkaria.postgresql.org; Tue, 12 Jul 2022 19:13:34 +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 1oBLKA-0004J6-3S for pgsql-admin@lists.postgresql.org; Tue, 12 Jul 2022 19:13:34 +0000 Received: from mail-wr1-x42c.google.com ([2a00:1450:4864:20::42c]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1oBLK6-00048R-W3 for pgsql-admin@lists.postgresql.org; Tue, 12 Jul 2022 19:13:33 +0000 Received: by mail-wr1-x42c.google.com with SMTP id r10so6348139wrv.4 for ; Tue, 12 Jul 2022 12:13:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20210112.gappssmtp.com; s=20210112; h=message-id:subject:from:to:cc:date:in-reply-to:references :user-agent:mime-version:content-transfer-encoding; bh=NQJLGFE72dHe8v82KKgxJ3SorOULrDOacghECPHpZUo=; b=sNp5xpLSYEKrN2SrnfXQgoNL0VnkqZspdGr0ch5Bdtk1xFRalYymii1oSlLpTF2AmT r05uzaFk3A07mlT99TGqOl3i3+V00D6bWQwoCuOvsNGpt/wlnhbv+42Ac4n9aVReMi6E OfRq08vdTlMSDBRWhsRWVsERC9qSjFhzBLalLXyFRf/JWaOFP7U5h0KNLqjx2a2aVWfV ECj54xb8dm8RZbaZMEhhT+IsM3ySV8hkODfAqikqn5bfIku+3b+UR7fQKYe2AsFmKfXb ePIcDSpr21Xq0VFlM/BD4iAFL8cfKfqYELCqLFpzMyPKU7A6yZ22SeTu9jvqFkoBYCrI VLFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:subject:from:to:cc:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=NQJLGFE72dHe8v82KKgxJ3SorOULrDOacghECPHpZUo=; b=I7E+gwhZklmo2n0ACuLb/sfsvcroz8CM0waYyogIlzFbifxrXNVvrrmV5k/kiu1Vs7 35fzmTz3QgMnzyQi38L5ssdFiwm+3UhIcysZkOwMYAuJ8oDC08rCuRVfd1iH8zbAwNyy 7cdo/0efj96M2bpvLIwAEMM71SimR6ZDeGhti4Ckze2W7faY7qkOeGq/2vDPcLeImQql nFiMWRYHe0Kt+PtM0gB76NA8a735aieueJhceNp9bDtLBxio83QxORWouw7xufly5r7C hn7RDFAaVMljmduSueqz3grGxaNb1iZUsVkmoPnK+0T9zt/AOXYi+iy6IWeopmOFLUr2 hwAQ== X-Gm-Message-State: AJIora/pMuSXRNneSLQqDkzVVuwx3XBeXrZD2oo4m3btQhZw5Q2BKumB k3MGRgbMUn3yFPqGA2HTxj3TTw== X-Google-Smtp-Source: AGRyM1sCQnEQ6KKxnOIyFgM54XhfGAwzIgcuECVgwcUeHRq1mJTKujjRJqlf3aIl22Z2OzBx/ZHP5Q== X-Received: by 2002:a05:6000:1848:b0:21d:abbb:1ec8 with SMTP id c8-20020a056000184800b0021dabbb1ec8mr8836758wri.114.1657653208808; Tue, 12 Jul 2022 12:13:28 -0700 (PDT) Received: from localhost.localdomain (91-115-145-89.adsl.highway.telekom.at. [91.115.145.89]) by smtp.gmail.com with ESMTPSA id n20-20020a05600c4f9400b0039c5ab7167dsm13705782wmq.48.2022.07.12.12.13.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Jul 2022 12:13:28 -0700 (PDT) Message-ID: <05fa08bf8b4e1023eb879bcab0f83d428e4f7a43.camel@cybertec.at> Subject: Re: [EXT] Re: Improve "select count(*)" query - takes more than 30 mins for some large tables From: Laurenz Albe To: MichaelDBA Vitale , "Pierson Patricia L (Contractor)" Cc: "pgsql-admin@lists.postgresql.org" Date: Tue, 12 Jul 2022 21:13:27 +0200 In-Reply-To: <1037158693.49441.1657650345834@email.ionos.com> References: <1B1927BA-F7DE-40E8-8F67-BCC001CD4C6B@gmail.com> <1eb233ff-90f0-529b-f0cc-fa982f668a8f@gmail.com> <1221536526.20277.1657636277921@email.ionos.com> <1037158693.49441.1657650345834@email.ionos.com> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.42.4 (3.42.4-2.fc35) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, 2022-07-12 at 14:25 -0400, MichaelDBA Vitale wrote:  > On 07/12/2022 2:13 PM Pierson Patricia L (Contractor) wrote: > > Do a count on the primary key.  Will force index access and you don’t access the entire row which may be very long. > > LIKE : select count(ID) from my_table; > > That is not true: doing the select on the primary key will still result in a table scan, > not an index scan. The heap always gets accessed for select counts. I'd say that both statements are wrong: - count(id) is *slower* than count(*), because it has to check each "id" if it is NULL or not (NULL values are not counted). count(*) is just the SQL standard's weird way of writing a parameterless aggregate; it has nothing to do with the * in "SELECT * FROM ". - Both "SELECT count(id) FROM tab" and "SELECT count(*) FROM tab" can result in an index-only scan. You just need the table to be recently VACUUMed, you need a table that is wide enough that a sequential scan is actually slower than an index-only scan, and perhaps you need "random_page_cost" to be low enough. Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com