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 1oZTVQ-0003vC-Hy for pgsql-general@arkaria.postgresql.org; Sat, 17 Sep 2022 08:48:56 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oZTVP-0002OY-0b for pgsql-general@arkaria.postgresql.org; Sat, 17 Sep 2022 08:48:55 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oZTVO-0002OP-Js for pgsql-general@lists.postgresql.org; Sat, 17 Sep 2022 08:48:54 +0000 Received: from mail-oi1-x22d.google.com ([2607:f8b0:4864:20::22d]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1oZTVL-0003C1-UC for pgsql-general@lists.postgresql.org; Sat, 17 Sep 2022 08:48:53 +0000 Received: by mail-oi1-x22d.google.com with SMTP id o204so8845680oia.12 for ; Sat, 17 Sep 2022 01:48:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=in-reply-to:from:references:to:content-language:subject:user-agent :mime-version:date:message-id:from:to:cc:subject:date; bh=2BWr+j+oyshw7PFi8WJgdTkizDg/SnNj+U085+6n950=; b=d/wZ/rEOCZ5LEq4/PxdrmisIXD9QLH2YfebnV+eGV43/3I+iPGjo66GuZa30hlAbm2 Ug5FvAMeui0N6odCJb6cW/g1K+kNTDxMyLZkGzKiyl3tLkstTlousmnqAdZajtk+YDYK CDhCk6fzLQ5O8X+0toZeRhRcY6+r5EtpV7qPjw5HFKZ2HFdjFxwJr/6yzKui56yXlgJz nxdnJNMnhkA0mkAuhCSTvS8QTYTPdy+ov6FOmjtIMg+wHJeQCoe0qLaHQoLzpW9dXRJ2 /UcPhrTVcmGpo8so3fKdREF35hobvF85XYESvbn6BYL814hG9n4cljIbDuzD+zG6jRxV sbTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:from:references:to:content-language:subject:user-agent :mime-version:date:message-id:x-gm-message-state:from:to:cc:subject :date; bh=2BWr+j+oyshw7PFi8WJgdTkizDg/SnNj+U085+6n950=; b=39bbMEIcnBISuJfCyA3AW7P2WoLrilgwhOPNA8Y2EbfWqSE1tfbhNSwyVNwAR+Gim3 77oDJ/OXw+wuN/x5gmCpvXhdeUsis/5IRXWAHaufDa1Oc0SwfwnoqNqJdguuMDLoZZHj oOvit0kRNB5HQoY1ndhTKy9PStZFSa5xyt0UTFnESRLIbROY91x9LAdRqMynLMz8mVEQ oddOC8pCYAThYclpFbANkPe3l8vSLKvFJPP9t80q5Mcpd45VLy0QeSQgiAMpi8YufSy7 DG9qHKxz0mzqk6zgzhnYh/1ijfEVzvkhljltF8qHlKbcRhfsDnb9jFR2nckeyiuxOIeJ JDuA== X-Gm-Message-State: ACrzQf2+o0R2QPazEYPc3yxTSb8ubHReYKoILMjAlQLe1jmI+pA69PYj Bp7iqvC3yw0rJcaMRi9wCl2kPGUtP9A= X-Google-Smtp-Source: AMsMyM6ggMZPXZZPVxqo/IGOxRKmCTgokWw1dclAKQw+p6Je5MB99GE7RMguW8o0G275Rad9iqUKxA== X-Received: by 2002:a05:6808:d48:b0:343:1ed2:7d08 with SMTP id w8-20020a0568080d4800b003431ed27d08mr4083800oik.197.1663404529561; Sat, 17 Sep 2022 01:48:49 -0700 (PDT) Received: from [192.168.88.10] (ip68-11-68-85.no.no.cox.net. [68.11.68.85]) by smtp.googlemail.com with ESMTPSA id g26-20020a544f9a000000b0034305ddc29dsm10431565oiy.28.2022.09.17.01.48.48 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 17 Sep 2022 01:48:48 -0700 (PDT) Content-Type: multipart/alternative; boundary="------------5g2RCfDTlSRm0MUD2vGlYKaC" Message-ID: Date: Sat, 17 Sep 2022 03:48:48 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.9.1 Subject: Re: Query Performance Content-Language: en-US To: pgsql-general@lists.postgresql.org References: <96197456.387057.1663392505315.ref@mail.yahoo.com> <96197456.387057.1663392505315@mail.yahoo.com> From: Ron In-Reply-To: <96197456.387057.1663392505315@mail.yahoo.com> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------5g2RCfDTlSRm0MUD2vGlYKaC Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 9/17/22 00:28, sivapostgres@yahoo.com wrote: > Hello, > > My query is like this > > Select a.field1, a.field2, a.field3 > From   (Select a.field1, b.field2, c.field3 >         From   table1 a >         Join   table2 b >         on     b.something = a.something >         Join   table3 c >         On     c.something = a.something >         Where  a.field7 = 'value' > >         UNION ALL > >         Select a.field4, a.field5, a.field6 >         From   table11 a >         Join   table21 b >         On     b.something = a.something >         Where  a.field8 = 'something' ) a > Join   table10 b > On     b.field11 = (Select c.field11 >                     From   table10 c >                     Where  c.field10 = a.field1 )                <- > instead of a.field1, if I hardcode value (eg. '100') query runs faster > Join   table21 c > On     c.something = a.something > ... > > > In the above query, If I substitute a value for a.field1, query runs faster. > Any suggestion/guidance/links to improve the query performance without > substituting the value ? Is there an index on table1.field1? Have you vacuumed or analyzed table1 lately? Have you decomposed the query into the smallest possible query that demonstrates the problem? -- Angular momentum makes the world go 'round. --------------5g2RCfDTlSRm0MUD2vGlYKaC Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit On 9/17/22 00:28, sivapostgres@yahoo.com wrote:
Hello,

My query is like this   

Select a.field1, a.field2, a.field3
From   (Select a.field1, b.field2, c.field3
        From   table1 a
        Join   table2 b
        on     b.something = a.something
        Join   table3 c
        On     c.something = a.something 
        Where  a.field7 = 'value'

        UNION ALL

        Select a.field4, a.field5, a.field6
        From   table11 a
        Join   table21 b
        On     b.something = a.something
        Where  a.field8 = 'something' ) a
Join   table10 b
On     b.field11 = (Select c.field11
                    From   table10 c
                    Where  c.field10 = a.field1 )                     <- instead of a.field1, if I hardcode value (eg. '100') query runs faster
Join   table21 c
On     c.something = a.something
...


In the above query, If I substitute a value for a.field1, query runs faster.   
Any suggestion/guidance/links to improve the query performance without substituting the value ?

Is there an index on table1.field1?
Have you vacuumed or analyzed table1 lately?
Have you decomposed the query into the smallest possible query that demonstrates the problem?


--
Angular momentum makes the world go 'round.
--------------5g2RCfDTlSRm0MUD2vGlYKaC--