Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cerQD-0003Xl-RE for pgsql-performance@arkaria.postgresql.org; Fri, 17 Feb 2017 22:54:37 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1cerQD-0005FH-E5 for pgsql-performance@arkaria.postgresql.org; Fri, 17 Feb 2017 22:54:37 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1cerOW-0002aS-GV for pgsql-performance@postgresql.org; Fri, 17 Feb 2017 22:52:52 +0000 Received: from mail-wm0-x236.google.com ([2a00:1450:400c:c09::236]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1cerOT-0006wt-AP for pgsql-performance@postgresql.org; Fri, 17 Feb 2017 22:52:51 +0000 Received: by mail-wm0-x236.google.com with SMTP id v77so19682692wmv.0 for ; Fri, 17 Feb 2017 14:52:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding; bh=5nTRzbP2EmfYnSU84ZCwRcH1hlsiS6mJ/Khs6yLTXg4=; b=OtU7puoRBLTs9E8/R5bN8U41aVpu5IvYFEQxZ+IVDJT/45GX/bMDj05No9ATcjIose mz45PubDfkTBIqH4Nf3nfH+FD2ujQVwFRyEUvytOHyuvJXsRt66Gsc93BHFMovIX3ZXQ CcmPPOucwSZgzGsQlBAvnlOWVm4UxiyKqucfn3SA8D8qx+kp8jJTGNpeDzGm4SFegYrY SadmXurI2Z80ssRCv1ODaoFOAYo34xGV4o2FgxmwlankHVBZOCFUHTVrGHHMgco3v/be bdunFA4k8LfAEhuv64NqtzmA95OQ9jG67oLH58BX1GhJrCztWOp3LY6L1X5I82CokEaK RGkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding; bh=5nTRzbP2EmfYnSU84ZCwRcH1hlsiS6mJ/Khs6yLTXg4=; b=BWJm9SESTtfZIzucER5nlXl5KQrENKuQLLnYqsgA+zn8nkx7rsZJmSNNRrOCooKRMM zvP9O5KHMtknvWjd+vdNM7g3+Y+0X6nQg8RWIgWbLNZO+h13iWGf9Btcagns86nMUhmp AlNnMGM2Mvhds6ZuELgWi0RgwVqJPyjDP6LYvbaVpM6Bg4HRDvmG4x5I7fZkIrisrt4y Rz/313Tpdoh91vgrK3c1EYKjtrCunKyTzH4iVVQz3qnS/XewgC6EVEaNgIOL5AT7DYPP wOJHOhDOe79VzmLCBIo40tUEEXhMtmYpnaGgIMjENZBZNoCkXxaZoZbJzNTD6N96d0F7 F7YQ== X-Gm-Message-State: AMke39lvvW7P7BBzRccqM21LWkxJK9M+TrO+Ks0Xp+M810A8EFFxaEjf1fcBERiKJ3Uod0kO60b8w8DWtzafqYt62Ugorsr44BDq2PF1z33hLom3/soWtKnw83mOCBZBdgPS44gXzeUZySh1dro20ewWD+ETMq3pyHxaZB2PlUw28ZIs7d1ojIiUMxL+KkQ376LaJ4g/v8tObEeA2A== X-Received: by 10.28.194.198 with SMTP id s189mr5820881wmf.114.1487371967151; Fri, 17 Feb 2017 14:52:47 -0800 (PST) Received: from [10.137.2.17] (ip-89-177-46-65.net.upcbroadband.cz. [89.177.46.65]) by smtp.gmail.com with ESMTPSA id m84sm3321091wmf.10.2017.02.17.14.52.46 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 17 Feb 2017 14:52:46 -0800 (PST) Subject: Re: Number of characters in column preventing index usage To: pgsql-performance@postgresql.org References: From: Tomas Vondra Message-ID: <195020ec-053f-6387-25f8-3f354da6cc38@2ndquadrant.com> Date: Fri, 17 Feb 2017 23:52:43 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org On 02/17/2017 11:42 PM, David G. Johnston wrote: > On Fri, Feb 17, 2017 at 3:19 PM, Hustler DBA >wrote: > > > my_db=# create index tab_idx1 on tab(ID); > > CREATE INDEX > my_db=# explain (analyze, buffers) select count(*) from tab where ID > = '01625cfa-2bf8-45cf' ; > QUERY > PLAN > --------------------------------------------------------------------------------------------------------------------------------------- > Aggregate (cost=8.29..8.30 rows=1 width=0) (actual > time=0.048..0.048 rows=1 loops=1) > Buffers: shared read=2 > -> Index Only Scan using tab_idx1 on tab (cost=0.27..8.29 > rows=1 width=0) (actual time=0.043..0.043 rows=0 loops=1) > Index Cond: (ID = '01625cfa-2bf8-45cf'::text) > > > > -> Seq Scan on tab (cost=0.00..14.79 rows=5 width=0) (actual > time=0.031..0.108 rows=5 loops=1) > Filter: ((ID)::text = > '01625cfa-2bf8-45cf-bf4c-aa5f3c6fa8ea'::text) > Rows Removed by Filter: 218 > Buffers: shared hit=12 > Planning time: 0.122 ms > Execution time: 0.180 ms > (8 rows) > > > ​IIRC the only reason the first query cares to use the index is because > it can perform an Index Only Scan and thus avoid touching the heap at > all. If it cannot avoid touching the heap the planner is going to just > use a sequential scan to retrieve the records directly from the heap and > save the index lookup step. > I don't follow - the queries are exactly the same in both cases, except the parameter value. So both cases are eligible for index only scan. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance