Thread: Querry correction required

Querry correction required

From
Sachin Kumar
Date:
Hi Expert,

While running the below mention query on 2 million cards it is taking too much time, say 420 min. is there any way I can reduce the timing..  

Please find my query.

UPDATE hk_card_master_test m
SET "ACCOUNT_NUMBER" = v."v_account_number", "ISSUANCE_NUMBER" = v."v_issuance_number","cron"=1
FROM (
SELECT h."id",h."CARD_SEQUENCE_NUMBER" ,h."ACCOUNT_NUMBER" ,h."ISSUANCE_NUMBER",c."ACCOUNT_NUMBER" v_account_number,c."ISSUANCE_NUMBER" v_issuance_number
FROM
hk_card_master_test h
JOIN
vdaccount_card_bank c
ON SUBSTR(c."ACCOUNT_NUMBER", 1, 10) = h."CARD_SEQUENCE_NUMBER"
ORDER BY h."id"
) AS v
WHERE m."CARD_SEQUENCE_NUMBER" = v."CARD_SEQUENCE_NUMBER";

--

Best Regards,
Sachin Kumar

Re: Querry correction required

From
Thomas Kellerer
Date:
Sachin Kumar schrieb am 18.11.2020 um 18:43:
> Hi Expert,
>
> While running the below mention query on 2 million cards it is taking too much time, say 420 min. is there any way I
canreduce the timing.. 
>
> Please find my query.
>
> UPDATE hk_card_master_test m
> SET "ACCOUNT_NUMBER" = v."v_account_number", "ISSUANCE_NUMBER" = v."v_issuance_number","cron"=1
> FROM (
> SELECT h."id",h."CARD_SEQUENCE_NUMBER" ,h."ACCOUNT_NUMBER" ,h."ISSUANCE_NUMBER",c."ACCOUNT_NUMBER"
v_account_number,c."ISSUANCE_NUMBER"v_issuance_number 
> FROM
> hk_card_master_test h
> JOIN
> vdaccount_card_bank c
> ON SUBSTR(c."ACCOUNT_NUMBER", 1, 10) = h."CARD_SEQUENCE_NUMBER"
> ORDER BY h."id"
> ) AS v
> WHERE m."CARD_SEQUENCE_NUMBER" = v."CARD_SEQUENCE_NUMBER";

I also answered that over at the Admin list

I don't think you need the derived table to begin with (which creates an implicit self-join of the target table):

As far as I can tell, the following would do the same thing:

     UPDATE hk_card_master_test m
       SET "ACCOUNT_NUMBER" = v."v_account_number",
           "ISSUANCE_NUMBER" = v."v_issuance_number",
           "cron"=1
     FROM vdaccount_card_bank v
     WHERE SUBSTR(v."ACCOUNT_NUMBER", 1, 10) = m."CARD_SEQUENCE_NUMBER"

You probably want those indexes:

    create index on vdaccount_card_bank ( (SUBSTR("ACCOUNT_NUMBER", 1, 10) );
    create index on hk_card_master_test ("CARD_SEQUENCE_NUMBER");

Unrelated to your question, but using quoted/uppercase identifiers is generally discouraged in Postgres:

   https://wiki.postgresql.org/wiki/Don't_Do_This#Don.27t_use_upper_case_table_or_column_names

you probably will have a lot less trouble if you get rid of those.

Thomas