Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cerNI-0003Al-PT for pgsql-performance@arkaria.postgresql.org; Fri, 17 Feb 2017 22:51:36 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1cerNI-0001Sc-1Z for pgsql-performance@arkaria.postgresql.org; Fri, 17 Feb 2017 22:51:36 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1cerLS-0007os-Mm for pgsql-performance@postgresql.org; Fri, 17 Feb 2017 22:49:43 +0000 Received: from mail-wr0-x233.google.com ([2a00:1450:400c:c0c::233]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1cerLL-00007h-EW for pgsql-performance@postgresql.org; Fri, 17 Feb 2017 22:49:41 +0000 Received: by mail-wr0-x233.google.com with SMTP id 89so32872920wrr.3 for ; Fri, 17 Feb 2017 14:49:35 -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=bvDRSnNWWKAHWIUzEvm61WVJRio46Fo7AsFVv/3vjS4=; b=mZdqVCQR8bY2vQ94Bah5iSwox2VXm2g45r4jm3ZL3EjgWrUh1T5cHgvlvLbYUdHBts d8BmSH3xpE+VCIHsTQRSXk1FwIx7kVVH+4B3py34V7r7i/zLUAPv9AUwzf5QpoQDu0NT djygxMaLpr3W2ZaBfW+gJBMuThqi8/ITitYgT8AYs08xUeh4t8pbD3C9nL0/l8H8lk8t h+UEDKsJhBslVNveLYHnbpHFtlSBdLx0ElYE1iYUWN8lEA0UijuQFOS920iPLehJaP4K ujUCy5+eA93+Wgd+U3CnDXR4DtAIzKZX9ha4pj0M6l7OqrcU9jJrYd879N9/8DpSi/Yo K0Xg== 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=bvDRSnNWWKAHWIUzEvm61WVJRio46Fo7AsFVv/3vjS4=; b=sjDpwRGooAhILbG2RhXpmKiQqQJELvqPdLxzIYBQZ41TFY1TgNSt9tRm9MrfDhgFAX wHxQa3CsROPva5SwefUPaUkziaT2vNfpF99iXEisJ+5dqHUad5F/0jNfr3lUhHr4nwyU WjC0Se70tp/UsIITeI30K3Q/upq71cW9F1QbY+MCCmZdvFwBuyrq1y3k412DeRUGGDUa pIzPEWtVdtS1jsXIemClMhg61VdqAKq2PZBtUggZDxZLnpbUTrdpC5/qPSmcwglFqHWq U+JwHAykIhVrf5j5Oe4IktC0QKJ/oU4gtx++ynjWwFtYdWv33zwXuRk42HAfkz0/vipl wXgw== X-Gm-Message-State: AMke39nNvK+hpWsi1ccxRhxCCx44QhrasNLZ96tD5RpIdxpHtxk237XgASylYsL2ZaCwVP4yKu9y67mD/K/mgtFQmfkaxwCEuJOgWc4fqSYiMMv4N2rpn2SnTxK+9FWALtPOJZUp5gMw9HjH9RvqVzJgFx/oGsFhiHK42A8bgJ98pP7pogq7dAr7M27BY4Ky3Q5l4gWzRYXDjvwK9A== X-Received: by 10.223.144.176 with SMTP id i45mr2928340wri.51.1487371772855; Fri, 17 Feb 2017 14:49:32 -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 m29sm14614310wrm.38.2017.02.17.14.49.32 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 17 Feb 2017 14:49:32 -0800 (PST) Subject: Re: Number of characters in column preventing index usage To: pgsql-performance@postgresql.org References: From: Tomas Vondra Message-ID: Date: Fri, 17 Feb 2017 23:49:29 +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: 7bit X-Pg-Spam-Score: -1.9 (-) 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 Hi, On 02/17/2017 11:19 PM, Hustler DBA wrote: > I am seeing this strange behavior, I don't know if this is by design by > Postgres. > > I have an index on a column which is defined as "character > varying(255)". When the value I am searching for is of a certain length, > the optimizer uses the index but when the value is long, the optimizer > doesn't use the index but does a seq scan on the table. Is this by > design? How can I make the optimizer use the index no matter what the > size/length of the value being searched for? > AFAIK there are no such checks, i.e. the optimizer does not consider the length of the value when deciding between scan types. > > PostgreSQL version: 9.4 > That's good to know, but we also need information about the table involved in your queries. I'd bet the table is tiny (it seems to be just 12 pages, so ~100kB), making the indexes rather useless. > 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) > Heap Fetches: 0 > Buffers: shared read=2 > Planning time: 0.250 ms > Execution time: 0.096 ms > (8 rows) > > my_db=# explain (analyze, buffers) select count(*) from tab where ID = > '01625cfa-2bf8-45cf-bf4c-aa5f3c6fa8ea' ; > QUERY PLAN > > ----------------------------------------------------------------------------------------------------------- > Aggregate (cost=14.80..14.81 rows=1 width=0) (actual time=0.115..0.115 > rows=1 loops=1) > Buffers: shared hit=12 > -> 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) The only difference I see is that for the long value the planner expects 5 rows, while for the short one it expects 1 row. That may seem a bit strange, but I'd bet it finds the short value in some statistic (MCV, histogram) ans so can provide very accurate estimate. While for the longer one, it ends up using some default (0.5% for equality IIRC) or value deduced from ndistinct. Or something like that. The differences between the two plans are rather negligible, both in terms of costs (8.3 vs. 14.81) and runtime (0.1 vs 0.2 ms). The choice of a sequential scan seems perfectly reasonable for such tiny tables. FWIW it's impossible to draw conclusions based on two EXPLAIN ANALYZE executions. The timing instrumentation from EXPLAIN ANALYZE may have significant impact impact (different for each plan!). You also need to testing with more values and longer runs, not just a single execution (there are caching effects etc.) 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