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 1l7FQx-000220-Se for pgsql-general@arkaria.postgresql.org; Wed, 03 Feb 2021 10:30:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1l7FQw-0000NR-CP for pgsql-general@arkaria.postgresql.org; Wed, 03 Feb 2021 10:30:50 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1l7FQw-0000MM-4d for pgsql-general@lists.postgresql.org; Wed, 03 Feb 2021 10:30:50 +0000 Received: from mail-ej1-x636.google.com ([2a00:1450:4864:20::636]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1l7FQt-00008d-Nr for pgsql-general@lists.postgresql.org; Wed, 03 Feb 2021 10:30:48 +0000 Received: by mail-ej1-x636.google.com with SMTP id bl23so34818088ejb.5 for ; Wed, 03 Feb 2021 02:30:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20150623.gappssmtp.com; s=20150623; h=message-id:subject:from:to:date:in-reply-to:references:user-agent :mime-version:content-transfer-encoding; bh=0avF+eUCedd3kLUlo8caMm+ZhifYSuksAtf2nAdxfEw=; b=aC/bg0zOEV1NQu2OrO4VAgfP/WBdWsXF822kioVvjbM2X0VyNrWIKruwCVJuFBzlhF 7JML/yFuJRBJjc+VWaF/exlBWosX5UcYc2j6MbR9kePBqyVMyhvz8brRypl/ySxfTomp bEG31KRuBo3SFxTgeh+JqiZfyA4lu/4l6m/XfOsFblQaNuoeMnaXmG9u+6doMQECBOGE E6Rq6niLx2n9CxnYnAJTZRlH4k1IBVh+eI/Hn8z2468bCp78m6XMLhJGKFabvkvhlE6h TlFQfWVYCUTTIr2GZ+7Pmyyet1BRkWexPnv5dzD+Ei2k3sWYV0TBxOZgR52IwvJWKnud gkmA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=0avF+eUCedd3kLUlo8caMm+ZhifYSuksAtf2nAdxfEw=; b=euWXOjbc4Fm/j84SvyntoUTCkSIV41yDOmYkrS8IapB+NFF2Sjgxt5DM0RDt1HzhzW 4CoGPIPWQGfrtAs8Nfjfcw/pNskQK450WriLNds1pOwu403CXSmSnQe1knmcsDHm6m3F cPaBKpYPJUFyxVbLCWFDaj+RNXhZPERhKQkEW5MuIdpY8j5mggu5Tmth9BYVTKynbxZj 1Stn3cXZODqquTaHvnnIN1k+vo9kvqmycOGnzr8Q47TNM9/BxBHPhCRVVyNgEcFSn4sG KfGHbqImcvcFlzCcn+lMUsVELjpiJbX+4Z6VclemAux5Zx7FiDDnPLfsmKZhZ/IpceDZ jpPg== X-Gm-Message-State: AOAM532JPpDWi5EEWuS2dXRbX/YoszW1xfFOdSxMGM7qhU4XDFC0XUM5 W6GYoOY1AHxmAnOTH0DUYenTMs8GG/kttw== X-Google-Smtp-Source: ABdhPJwp6nIhPmhLISS6gG/f/GpAgyWp1Ge/sUIl20EX5w2mfkVFa9a35QPoyu8CUx2KaPMncyn3cw== X-Received: by 2002:a17:906:b84d:: with SMTP id ga13mr2484880ejb.112.1612348246488; Wed, 03 Feb 2021 02:30:46 -0800 (PST) Received: from localhost.localdomain (217-149-170-253.nat.highway.telekom.at. [217.149.170.253]) by smtp.gmail.com with ESMTPSA id l13sm803893eji.49.2021.02.03.02.30.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Feb 2021 02:30:46 -0800 (PST) Message-ID: Subject: Re: count(*) vs count(id) From: Laurenz Albe To: Karsten Hilbert , pgsql-general@lists.postgresql.org Date: Wed, 03 Feb 2021 11:30:45 +0100 In-Reply-To: References: <3502043.1612226098@sss.pgh.pa.us> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.36.5 (3.36.5-2.fc32) MIME-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Wed, 2021-02-03 at 10:54 +0100, Karsten Hilbert wrote: > > I just ran a few practical tests on large (~14mil rows) tables that have > > multiple indexes. > > SELECT COUNT(id) forces PostgreSQL to use the primary key index. > > SELECT COUNT(*) allows PostgreSQL to chose an index to use and it seems to > > be choosing one of smaller size which leads to less IO and hence returns > > the result faster. > > Would you mind throwing in a test for > > select count(1) ... No need to test. That will be slightly slower because it repeatedly checks if 1 is NULL or not. Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com