Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gfKfw-0007BF-QW for pgsql-performance@arkaria.postgresql.org; Fri, 04 Jan 2019 08:17:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gfKfv-0006bc-CH for pgsql-performance@arkaria.postgresql.org; Fri, 04 Jan 2019 08:17:51 +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_SHA1:256) (Exim 4.89) (envelope-from ) id 1gfKfv-0006bV-4E for pgsql-performance@lists.postgresql.org; Fri, 04 Jan 2019 08:17:51 +0000 Received: from smtp1.iter.org ([193.51.56.7]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gfKfs-0005jC-At for pgsql-performance@lists.postgresql.org; Fri, 04 Jan 2019 08:17:50 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iter.org; l=3893; q=dns/txt; s=20170425; t=1546589868; x=1547194668; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=l3CABevJts75eO9W/Jd9XEViElxM5bQTMjmnAjD6JG4=; b=OLvOa5nAeXQmXSz4tyMChfLZ6+qISAnqoOG3kffg6OlkXQuqKgSb0eF1 f8AuiqgWSEHWGAAX5ZWgU6mjlA6Jmkx+QKbM0QVHs2j6VBNl4r29gnExH tg5yC86vN+4ydcJqARVV7JaZPkKK3GEnakP4GEi4K7fR1e6fHjRXNj6gp 8=; Received: from XCH-CLU1N3.iter.org (10.10.242.164) by XCH-CLU1N1.iter.org (10.10.240.164) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1261.35; Fri, 4 Jan 2019 09:17:47 +0100 Received: from XCH-CLU1N3.iter.org ([fe80::f903:4e8a:c365:4c74]) by XCH-CLU1N3.iter.org ([fe80::f903:4e8a:c365:4c74%16]) with mapi id 15.01.1261.036; Fri, 4 Jan 2019 09:17:46 +0100 From: Abadie Lana To: Justin Pryzby CC: David Rowley , "pgsql-performance@lists.postgresql.org" Subject: RE: select query does not pick up the right index Thread-Topic: select query does not pick up the right index Thread-Index: AdSitwt5/9TyB7hoTX++E94tUlkiMgAOgIkAABh1BzAAAkhrgAACgegQ///wggD//+6z4IAAwWGA//9iMjA= Date: Fri, 4 Jan 2019 08:17:46 +0000 Message-ID: <7e1a311617e441d2a40dc97fa9766753@iter.org> References: <49406fb0c239480980ffc6c2a250d4e9@iter.org> <438dcc57f91d4a9fab12ae4078592d69@iter.org> <20190103234743.GU25379@telsasoft.com> In-Reply-To: <20190103234743.GU25379@telsasoft.com> Accept-Language: en-GB, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.14.32.57] Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk -----Original Message----- From: Justin Pryzby =20 Sent: 04 January 2019 00:48 To: Abadie Lana Cc: David Rowley ; pgsql-performance@lists.po= stgresql.org Subject: Re: select query does not pick up the right index On Thu, Jan 03, 2019 at 12:57:27PM +0000, Abadie Lana wrote: > Main parameters : effective_cache_size : 4GB, shared_buffers 4GB,=20 > work_mem 4MB I doubt it will help much, but you should consider increasing work_mem, unl= ess you have many expensive queries running at once. Could you also send the rest of the pg_statistic for that table ? https://wiki.postgresql.org/wiki/Slow_Query_Questions#Statistics:_n_distinc= t.2C_MCV.2C_histogram SELECT (SELECT sum(x) FROM unnest(most_common_freqs) x) frac_MCV, tablename= , attname, null_frac, n_distinct, array_length(most_common_vals,1) n_mcv, a= rray_length(histogram_bounds,1) n_hist FROM pg_stats WHERE attname=3D'...' = AND tablename=3D'...' ORDER BY 1 DESC;=20 Hmm. Is it normal that the couple (tablename,attname ) is not unique? I'm s= urprised to see sample_{ctrl,util,buil} quoted twice css_archive_3_0_0=3D# SELECT (SELECT sum(x) FROM unnest(most_common_freqs) = x) frac_MCV, tablename, attname, null_frac, n_distinct, array_length(most_c= ommon_vals,1) n_mcv, array_length(histogram_bounds,1) n_hist FROM pg_stats = WHERE attname=3D'channel_id' AND tablename like 'sample%' ORDER BY 1 DESC; frac_mcv | tablename | attname | null_frac | n_distinct | n_mcv= | n_hist ----------+-------------------+------------+-----------+------------+------= -+-------- 1 | sample_buil_year | channel_id | 0 | 16 | 16= | 0.98249 | sample_ctrl | channel_id | 0 | 26 | 17= | 9 0.982333 | sample_ctrl_month | channel_id | 0 | 34 | 17= | 17 0.981533 | sample_ctrl | channel_id | 0 | 28 | 18= | 10 0.9371 | sample_ctrl_year | channel_id | 0 | 38 | 16= | 22 0.928767 | sample_buil_month | channel_id | 0 | 940 | 54= | 101 0.92535 | sample | channel_id | 0 | 2144 | 167= | 1001 0.907501 | sample_buil | channel_id | 0 | 565 | 43= | 101 0.8876 | sample_util_year | channel_id | 0 | 501 | 45= | 101 0.815 | sample_util | channel_id | 0 | 557 | 82= | 101 0.807667 | sample_buil | channel_id | 0 | 164 | 31= | 101 0.806267 | sample_util | channel_id | 0 | 732 | 100= | 101 0.803766 | sample_util_month | channel_id | 0 | 731 | 100= | 101 (13 rows) Ah...sample_ctrl_year and sample_buil_year have n_distinct -1? Unlike sampl= e_util_year. Could that explain the wrong choice?=20 SELECT (SELECT sum(x) FROM unnest(most_common_freqs) x) frac_MCV, tablename= , attname, null_frac, n_distinct, array_length(most_common_vals,1) n_mcv, a= rray_length(histogram_bounds,1) n_hist FROM pg_stats WHERE attname=3D'smpl_= time' AND tablename like 'sample%' ORDER BY 1 DESC; frac_mcv | tablename | attname | null_frac | n_distinct | n_m= cv | n_hist ------------+-------------------+-----------+-----------+-------------+----= ---+-------- | sample_ctrl_month | smpl_time | 0 | -1 | = | 101 | sample_ctrl_year | smpl_time | 0 | -1 | = | 101 | sample_ctrl | smpl_time | 0 | -1 | = | 101 | sample_ctrl | smpl_time | 0 | -1 | = | 101 | sample_buil_year | smpl_time | 0 | -1 | = | 101 0.0154667 | sample_buil_month | smpl_time | 0 | 1.03857e+06 | 1= 00 | 101 0.0154523 | sample_buil | smpl_time | 0 | 854250 | 1= 00 | 101 0.0115 | sample_util | smpl_time | 0 | 405269 | 1= 00 | 101 0.0112333 | sample_util | smpl_time | 0 | 537030 | 1= 00 | 101 0.0106667 | sample_util_month | smpl_time | 0 | 539001 | 1= 00 | 101 0.00946667 | sample_buil | smpl_time | 0 | -0.328554 | 1= 00 | 101 0.00852342 | sample | smpl_time | 0 | 1.5125e+07 | 10= 00 | 1001 0.00780001 | sample_util_year | smpl_time | 0 | 1.73199e+06 | 1= 00 | 101 (13 rows)