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 1gyQ9l-0006A8-Dp for pgsql-performance@arkaria.postgresql.org; Mon, 25 Feb 2019 23:59:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gyQ9k-0007Ik-1j for pgsql-performance@arkaria.postgresql.org; Mon, 25 Feb 2019 23:59:32 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gyQ9j-0007HT-IS for pgsql-performance@lists.postgresql.org; Mon, 25 Feb 2019 23:59:31 +0000 Received: from mail-it1-x144.google.com ([2607:f8b0:4864:20::144]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gyQ9b-0003dI-Qs for pgsql-performance@postgresql.org; Mon, 25 Feb 2019 23:59:30 +0000 Received: by mail-it1-x144.google.com with SMTP id d125so1072498ith.1 for ; Mon, 25 Feb 2019 15:59:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=ilLhISJFgM3Ur3yI05y6dafuQihH1R27lEuzxtiupj0=; b=nXvYa3iNutG3YjYYMJoXUnD/rGJtcp4P1ABl5IIVPjefnJj0U/5a9ONvCnfbIxhYKE R5SrtVrWPdy3zOLy0qnjvA9REMPqZ1TnnqYlIHQ0Elwdloi21X+N2oacELnziVluisK6 g4ourysg0mV8jr+DJ0GCHmIdj+QJLIvQUuftQLMCjaWsq2OBGxv9FsXoi4LYbDeh7fLv PbMrs/mV4Qd0L0FSOFxpKgief5yvuTgivTMF5isoGAeeUl4P4XM+HZISDr6retKZEzj0 9diLPujrffVptRmZWFHrO799X36snKeeBfsSdlfVlGrnJhXOZGpRkSxF7KnVt99VgYya ii7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=ilLhISJFgM3Ur3yI05y6dafuQihH1R27lEuzxtiupj0=; b=kZLs+aEgUQvq7nxjCFGUQI78F3DD9gNG7GRfF7DT9trKZKMtWwI/tXPFxs0RyCVdic J3TIZWSO08goVhFskmUs3XGs1WCKweVv3gADPFP4bOllYkiNRydcyr0VLVN+tz6CZ3Lq 9l+Uf+BXCjiX2Xs+ZOoOo8eVuow0cE7YZPho/ilNtsrnkzAJAutRf5yieag9+yg6yY5B HpEOcKzHXIhGAcBJQV+2kYePWgUwzaP7jLBrOXKJHly1GQQtomQzpvxKS7UDttWxd6IC bip/PCQnAlRbiwL/wofge6LKNbY2ipctDKPQFnK5cafdT6RkdM3XMmvvZLWRNlg+t7v/ +q/Q== X-Gm-Message-State: APjAAAV0yDPwiKvQE8LrWDW0bzSUEJEpN8jzHC/vHoM0/IGThDFPzLK7 50wXvpgJ4to05/mVHRmguHP4NoAju30= X-Google-Smtp-Source: APXvYqxv6XCeuAdoVlMO+jTlr+ID2ceu14jJDZy+c1Ef55VZuYSGRFx4XB28PwKx+j+ZnjKPRnVbGA== X-Received: by 2002:a24:4198:: with SMTP id b24mr1013702itd.25.1551139162587; Mon, 25 Feb 2019 15:59:22 -0800 (PST) Received: from pryzbyj (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id q133sm3815149itb.19.2019.02.25.15.59.21 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 25 Feb 2019 15:59:21 -0800 (PST) Received: by pryzbyj (Postfix, from userid 1000) id 1D7DF800C83; Mon, 25 Feb 2019 17:59:20 -0600 (CST) Date: Mon, 25 Feb 2019 17:59:20 -0600 From: Justin Pryzby To: support@mekong.be Cc: pgsql-performance@postgresql.org Subject: Re: Query slow for new participants Message-ID: <20190225235919.GR28750@telsasoft.com> References: <1551091278468-0.post@n3.nabble.com> <20190225161607.GQ28750@telsasoft.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.23 (2014-03-12) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Tue, Feb 26, 2019 at 12:22:39AM +0100, support@mekong.be wrote: > Hardware > Standard DS15 v2 (20 vcpus, 140 GB memory) > "effective_cache_size" "105GB" "configuration file" > "effective_io_concurrency" "200" "configuration file" > "maintenance_work_mem" "2GB" "configuration file" > "max_parallel_workers" "20" "configuration file" > "max_parallel_workers_per_gather" "10" "configuration file" > "max_worker_processes" "20" "configuration file" > "random_page_cost" "1.1" "configuration file" > "shared_buffers" "35GB" "configuration file" > "work_mem" "18350kB" "configuration file" I don't know for sure, but 35GB is very possibly too large shared_buffers. The rule of thumb is "start at 25% of RAM" but I think anything over 10-15GB is frequently too large, unless you can keep the whole DB in RAM (can you?) > Table Metadata > relname, relpages, reltuples, relallvisible, relkind, relnatts, relhassubclass, reloptions, pg_table_size(oid) > "companyarticledb" 6191886 "5.40276e+08" 6188459 "r" 44 false "50737979392" work_mem could probably benefit from being larger (just be careful that you don't end up with 20x parallel workers running complex plans each node of which using 100MB work_mem). > Full Table and Index Schema > The difference is very bad for the new company, even on the simplest query > > SELECT * FROM CompanyArticleDB > WHERE CompanyId = '77' > AND ArticleId= '7869071' It sounds to me like the planner thinks that the distribution of companyID and articleID are independent, when they're not. For example it think that companyID=33 filters out 99% of the rows. > companyid | integer | | not null | > articleid | integer | | not null | > EXPLAIN (ANALYZE, BUFFERS), not just EXPLAIN > SELECT * FROM CompanyArticleDB > WHERE CompanyId = '77' > AND ArticleId= '7869071' > "Index Scan using ix_companyarticledb_company on companyarticledb (cost=0.57..2.80 rows=1 width=193) (actual time=1011.335..1011.454 rows=1 loops=1)" > " Index Cond: (companyid = 77)" > " Filter: (articleid = 7869071)" > " Rows Removed by Filter: 2674361" > " Buffers: shared hit=30287" > Example for another participant, there another index is used. > "Index Scan using pk_pricedb on companyarticledb (cost=0.57..2.79 rows=1 width=193) (actual time=0.038..0.039 rows=0 loops=1)" > " Index Cond: ((companyid = 39) AND (articleid = 7869071))" > " Buffers: shared hit=4" > I do not know why this participant is different than the others except that > it was recently added. Were the tables ANALYZEd since then ? You could check: SELECT * FROM pg_stat_user_tables WHERE relname='companyarticledb'; If you have small number of companyIDs (~100), then the table statistics may incldue a most-common-values list, and companies not in the MCV list may end up with different query plans, even without correlation issues. It looks like the NEW company has ~3e6 articles, out of a total ~5e8 articles. The planner may think that companyID doesn't exist at all, so scanning the idx on companyID will be slightly faster than using the larger, composite index on companyID,articleID. Justin > Indexes: > "pk_pricedb" PRIMARY KEY, btree (companyid, articleid) > "EnabledIndex" btree (enabled) > "ix_companyarticledb_article" btree (articleid) > "ix_companyarticledb_company" btree (companyid) > "participantarticlecodeindex" btree (articlecode) > "participantdescriptionindex" gin (participantdescription gin_trgm_ops) > Foreign-key constraints: > "fk_companyarticledb_accountsdb" FOREIGN KEY (modifiedby) REFERENCES accountsdb(id) > "fk_companyarticledb_accountsdb1" FOREIGN KEY (createdby) REFERENCES accountsdb(id) > "fk_companyarticledb_accountsdb2" FOREIGN KEY (preventioncounselorid) REFERENCES accountsdb(id) > "fk_companyarticledb_articledb" FOREIGN KEY (articleid) REFERENCES articledb(id) > "fk_companyarticledb_companydb" FOREIGN KEY (companyid) REFERENCES companydb(id) > "fk_companyarticledb_interfaceaccountdb" FOREIGN KEY (interfaceaccountid) REFERENCES interfaceaccountdb(id) > "fk_companyarticledb_supplieraccountdb" FOREIGN KEY (createdbysupplier) REFERENCES supplieraccountdb(id) > "fk_companyarticledb_supplieraccountdb1" FOREIGN KEY (modifiedbysupplier) REFERENCES supplieraccountdb(id)