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 1l0QxI-0007hN-1d for pgsql-general@arkaria.postgresql.org; Fri, 15 Jan 2021 15:24:04 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1l0QxG-0007T9-Uf for pgsql-general@arkaria.postgresql.org; Fri, 15 Jan 2021 15:24:02 +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 1l0QxG-0007T2-NO for pgsql-general@lists.postgresql.org; Fri, 15 Jan 2021 15:24:02 +0000 Received: from mail-ot1-x333.google.com ([2607:f8b0:4864:20::333]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1l0QxE-0000O4-G3 for pgsql-general@lists.postgresql.org; Fri, 15 Jan 2021 15:24:01 +0000 Received: by mail-ot1-x333.google.com with SMTP id o11so8853090ote.4 for ; Fri, 15 Jan 2021 07:24:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=nTjUqUVeAPG0D/ijOhGlrV3BYOfGK76gyY1UZSzl8Jw=; b=UEyoxmpt6qlmoo+CayatLCiQUiKDihn8j+PjqQrVVtYgZ9YvPWVR1Gt6++qo38W1Vv YMBL4m9TZiCfmvSIx3oneE/hgCs4YteXIrw1BoSyqmhaa53lcYrRVjCAN5s3nu7JNbsK RfV5pkk2HL2zyFzSllgaOEFD4mD0+x6wSdtuy5vYSDV33xie/73UvlOpx2Yw2l2KFcOq aArMiTyLcD5PbJKTlNP38siOgyccJq8gssrNqHL3v6RIkEYXaPNjKBw1ywuE2i0qQAu1 yT/OaxweSGJbMZOye5ORcjsvDz3B8D9i/uVV7gAeDniQGi0aGrZv5GrL2kBa+GiTo/cr GkwA== 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 :content-language; bh=nTjUqUVeAPG0D/ijOhGlrV3BYOfGK76gyY1UZSzl8Jw=; b=JmuPNs86UavwEMouNc96AKuLXu8WmqO4vBD2NfuVlepQhWky46MGvgu9jbdS/jsxFA 9zUwvwkTg+DT6J9z3TaYata+DH0n1aK82oyWXYs3Uco30LMnZa7MaD27FE9iM5uf70n9 pzprGlix7lgdvsSaUx4MY2aYEqMPNZvlQ9Q4GD6J8LeCHZDwrBtdMck2zVNUCf2yHBSX 8VkbSN4IsILzT6IkRaTm6UtwvzvuklU6edrMLrIH2OFOfUE1sJINzg5rxRJzcxvvZ1VP OsQXOr2JZo1U6wfECTPGIsEt09Y3fz+F5duLwAdXPbluyb5le1crI4SRw2kcw7B1S8Mv gRtQ== X-Gm-Message-State: AOAM5312E0n4fYonQlVAqbIMW4wE2JN8m2G7Fu1RV6to5DH029VPVZQ/ BNhd9A4ILutjiCoNxTKmMQJqG7z9GvvX+g== X-Google-Smtp-Source: ABdhPJz8+Kq7BPC2JsNy+fL553T7t/RiOZ2ZBRPbUyqH64KK7rNZ04lETKuUvmJ8/J2xG4DZ/JF3bg== X-Received: by 2002:a9d:c66:: with SMTP id 93mr1486350otr.289.1610724239578; Fri, 15 Jan 2021 07:23:59 -0800 (PST) Received: from [192.168.1.10] (ip174-67-141-204.no.no.cox.net. [174.67.141.204]) by smtp.googlemail.com with ESMTPSA id f25sm1827615oou.39.2021.01.15.07.23.58 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 15 Jan 2021 07:23:59 -0800 (PST) Subject: Re: Accounting for between table correlation To: pgsql-general@lists.postgresql.org References: From: Ron Message-ID: Date: Fri, 15 Jan 2021 09:23:58 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 1/15/21 9:19 AM, Alexander Stoddard wrote: > I am having ongoing trouble with a pair of tables, the design of which is > beyond my control. > > There is a 'primary' table with hundreds of millions of rows. There is > then a 'subclass' table ~ 10% of the primary which has additional fields. > The tables logically share a primary key field (although that is not > annotated in the schema). > > Membership of the subclass table has high correlation with fields in the > primary table - it is very much not random. It seems query plans where the > two tables are joined are 'unstable'. Over time very different plans can > resultĀ  even for unchanged queries and some plans are exceedingly inefficient. > > I think what is going on is that the query planner assumes many fewer rows > are going to join to the subtable than actually do (because of the strong > correlation). > > Can anyone offer any advice on dealing with this scenario (or better > diagnosing it)? Do the tables get analyzed on a regular basis? -- Angular momentum makes the world go 'round.