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 1go7vB-0003WA-Ae for pgsql-general@arkaria.postgresql.org; Mon, 28 Jan 2019 14:29:57 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1go7vA-0008Be-4k for pgsql-general@arkaria.postgresql.org; Mon, 28 Jan 2019 14:29:56 +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 1go7v9-0008AW-MA for pgsql-general@lists.postgresql.org; Mon, 28 Jan 2019 14:29:55 +0000 Received: from mail-ot1-x334.google.com ([2607:f8b0:4864:20::334]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1go7v7-0005Go-8V for pgsql-general@lists.postgresql.org; Mon, 28 Jan 2019 14:29:54 +0000 Received: by mail-ot1-x334.google.com with SMTP id g16so10733839otg.11 for ; Mon, 28 Jan 2019 06:29:53 -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-language; bh=wPwHYsiA5rcR8K6eYoYyV2yBdpBle6dvL4kCI069nT8=; b=Wh7xu2/nmsLA0V7DjK3q1ZhxHWYvZgJ6OFhp6ZqYR82R9xp0UY6Dr0e0BIrSjUcWfs pxZVCIIuBMKGAryGVLS0pWtvMoIV1R7IbscRcdxyP4W5ul/zhyDUDftwUFGFISOit/PK TLURvRPosP8Ia+p47vCIj1si9zLn/r5urUrF6VthJHsMMJSIgNERJyuNSyw1fPETxy2x 2Jpkigua8kHB/rntQSki06+vVgBLe3486bmseMi5SRxdkQS8wrpdMg9chEpSz/SPloJO 8c1evpWlXIxpbNyyQqGxBpOExEeXdKhTAElezLv1NuGzv9tUEqHXUxVc6Fv4E5qYOV0Q vyZg== 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-language; bh=wPwHYsiA5rcR8K6eYoYyV2yBdpBle6dvL4kCI069nT8=; b=nFZFFYmuyykr1/mdAI/VoD5k/C6V6A10NDTLFz3iiJy6Rns7uUILF8EYSCF6VMW//y nZXb10e04yDQLv2QbG4Y113aFuT0UfIf5j50qQboqCkD3cjOzqdtzyXNr0h+wYC4gF0T rw6oeIirwfIDEJJiw0UBNqYu1UBITuTIknDm4ybH6iVJrJBBa1Kp/MkbkXoL/5wcF/D8 apFTdGKfqcjaU1sBU2jVj1S57lT/fgM6veUi/nW7PwRL+2WE8+CV9dcl/72kooYj68/M bgKf117WmBuFBlXR5HzUY+MVGqNCTlpdQ/AUZvYEO4MDLfNlQYQ2JMYSHuOk+UZK7lAg l2RA== X-Gm-Message-State: AJcUukeedGm0n5wHhxDgfhGUk3XhxUaNH+TdWdfq7TF5nRq3m7PUQ0jY UTHtcJ1kNhQN9tvGaBbpk0PoZZFb X-Google-Smtp-Source: ALg8bN5giXN+qTLe07uXwf4+7e5+ZoVf6GCukfOmpmYOYSjOe+IyvFsXHg1DiRIty+COU9GgrI48Jg== X-Received: by 2002:a9d:7ac6:: with SMTP id m6mr16726077otn.62.1548685792343; Mon, 28 Jan 2019 06:29:52 -0800 (PST) Received: from [192.168.1.10] (ip70-171-116-89.no.no.cox.net. [70.171.116.89]) by smtp.googlemail.com with ESMTPSA id j5sm5512417otc.54.2019.01.28.06.29.51 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 28 Jan 2019 06:29:51 -0800 (PST) Subject: Re: Regarding query execution for long time To: pgsql-general@lists.postgresql.org References: <041e270e-2426-4b33-682d-231057c7c71e@gmail.com> From: Ron Message-ID: <12e95129-bec5-4ba4-6f30-8c99057e772a@gmail.com> Date: Mon, 28 Jan 2019 08:29:50 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.4.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------7C1FA5EF57C277F29B5F4C61" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------7C1FA5EF57C277F29B5F4C61 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit On 1/28/19 8:10 AM, Durgamahesh Manne wrote: > > > On Mon, Jan 28, 2019 at 6:34 PM Ron > wrote: > > On 1/28/19 6:20 AM, Durgamahesh Manne wrote: > > Hi > > > > below query is being executed for long time > > > > Select > > distinct ltrim(rtrim(ssnumber)), CL.iInsightClientId, > > ltrim(rtrim(TFA.client_account_key)) as vchAccountNumber, > > concat(TFA.first_name, ' ', TFA.last_name) as vchAccountName, > > concat('AP:TIAA', TFA.routing_number) as vchRepLabel, > > null as vchOpenDate, TFA.address1 as vchAccountTiteLine2, > > TFA.address2 as vchAccountTitleLine3,null as vchAccountType, > > TFA.address1 as vchPrimaryContact, 'TIAA-CREF' as Custodian, > > TFA.routing_number as vchCustodian_RepId, null as vchCustodianRepName, > > CONCAT ('TIAA-CREF:',ltrim(rtrim(client_account_key)),':', > > ltrim(rtrim(ssnumber))) as vchAccountKey, > > null as vchFeedsAccountType > > from accounts as TFA > > join client3 as CL on > ltrim(rtrim(Cl.vchssnumber))=ltrim(rtrim(TFA.ssnumber)) > > left join account3 as AC on > > > ltrim(rtrim(AC."vchCustodianAccountNumber"))=ltrim(rtrim(TFA.client_account_key)) > > where AC."iInsightAccountID" is null; > > > >  query is being executed for long time even after i have created > required > > indexes on columns of the tables > > please help for fast query execution > > I bet this is what's causing your troubles: > on ltrim(rtrim(Cl.vchssnumber))=ltrim(rtrim(TFA.ssnumber)) > > The query optimizer probably can't use indexes on those tables. > > Hi >   why query optimizer can't use indexes on those columns of the tables Because of the ltrim() functions.  B-trees sort the data on the field values, and ltrim() changes that. For example, these two strings are different, and therefore the b-tree puts them in different places in the tree.   snagglefrob snagglefrob Using lrtrim() makes them logically eqivalent, but not physically equivalent. -- Angular momentum makes the world go 'round. --------------7C1FA5EF57C277F29B5F4C61 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit On 1/28/19 8:10 AM, Durgamahesh Manne wrote:


On Mon, Jan 28, 2019 at 6:34 PM Ron <ronljohnsonjr@gmail.com> wrote:
On 1/28/19 6:20 AM, Durgamahesh Manne wrote:
> Hi
>
> below query is being executed for long time
>
> Select
> distinct ltrim(rtrim(ssnumber)), CL.iInsightClientId,
> ltrim(rtrim(TFA.client_account_key)) as vchAccountNumber,
> concat(TFA.first_name, ' ', TFA.last_name) as vchAccountName,
> concat('AP:TIAA', TFA.routing_number) as vchRepLabel,
> null as vchOpenDate, TFA.address1 as vchAccountTiteLine2,
> TFA.address2 as vchAccountTitleLine3,null as vchAccountType,
> TFA.address1 as vchPrimaryContact, 'TIAA-CREF' as Custodian,
> TFA.routing_number as vchCustodian_RepId, null as vchCustodianRepName,
> CONCAT ('TIAA-CREF:',ltrim(rtrim(client_account_key)),':',
> ltrim(rtrim(ssnumber))) as vchAccountKey,
> null as vchFeedsAccountType
> from accounts as TFA
> join client3 as CL on ltrim(rtrim(Cl.vchssnumber))=ltrim(rtrim(TFA.ssnumber))
> left join account3 as AC on
> ltrim(rtrim(AC."vchCustodianAccountNumber"))=ltrim(rtrim(TFA.client_account_key))
> where AC."iInsightAccountID" is null;
>
>  query is being executed for long time even after i have created required
> indexes on columns of the tables
> please help for fast query execution

I bet this is what's causing your troubles:
on ltrim(rtrim(Cl.vchssnumber))=ltrim(rtrim(TFA.ssnumber))

The query optimizer probably can't use indexes on those tables.

Hi 
 
  why query optimizer can't use indexes on those columns of the tables

Because of the ltrim() functions.  B-trees sort the data on the field values, and ltrim() changes that.
For example, these two strings are different, and therefore the b-tree puts them in different places in the tree.
  snagglefrob
snagglefrob

Using lrtrim() makes them logically eqivalent, but not physically equivalent.


--
Angular momentum makes the world go 'round.
--------------7C1FA5EF57C277F29B5F4C61--