jmf
jmf

Reputation: 396

Update table rows in Postgresql taking too much time

I am trying to update around 6000 rows in a table but my query never finishes. I have put the data to be updated in a temp table and using a join to update rows. This was working pretty fast in Sql Server but in Postgresql it never finishes. I am updating around 40 columns. Here is the sql I am running.

UPDATE "STG_magento_de".sales_flat_order
SET customer_id = b.customer_id
    ,created_at = b.created_at
    ,updated_at = b.updated_at
    ,coupon_code = b.coupon_code
    ,box_id = b.box_id
    ,beautytrends_glossydots = b.beautytrends_glossydots
    ,billing_address_id = b.billing_address_id
    ,shipping_address_id = b.shipping_address_id
    ,base_discount_amount = b.base_discount_amount
    ,base_discount_canceled = b.base_discount_canceled
    ,base_discount_invoiced = b.base_discount_invoiced
    ,base_discount_refunded = b.base_discount_refunded
    ,base_grand_total = b.base_grand_total
    ,base_shipping_amount = b.base_shipping_amount
    ,base_shipping_canceled = b.base_shipping_canceled
    ,base_shipping_invoiced = b.base_shipping_invoiced
    ,base_shipping_refunded = b.base_shipping_refunded
    ,base_shipping_tax_amount = b.base_shipping_tax_amount
    ,base_shipping_tax_refunded = b.base_shipping_tax_refunded
    ,base_subtotal = b.base_subtotal
    ,base_subtotal_canceled = b.base_subtotal_canceled
    ,base_subtotal_invoiced = b.base_subtotal_invoiced
    ,base_tax_amount = b.base_tax_amount
    ,base_tax_canceled = b.base_tax_canceled
    ,base_tax_invoiced = b.base_tax_invoiced
    ,base_tax_refunded = b.base_tax_refunded
    ,base_to_global_rate = b.base_to_global_rate
    ,base_to_order_rate = b.base_to_order_rate
    ,base_total_canceled = b.base_total_canceled
    ,base_total_invoiced = b.base_total_invoiced
    ,base_total_invoiced_cost = b.base_total_invoiced_cost
    ,base_total_offline_refunded = b.base_total_offline_refunded
    ,base_total_online_refunded = b.base_total_online_refunded
    ,base_total_paid = b.base_total_paid
    ,base_total_qty_ordered = b.base_total_qty_ordered
    ,base_total_refunded = b.base_total_refunded
    ,increment_id = b.increment_id
    ,order_type = b.order_type
    ,STATUS = b.STATUS
    ,is_chargerun = b.is_chargerun
    ,chargeback_flag = b.chargeback_flag
    ,gift_message_id = b.gift_message_id
    ,dispatch = b.dispatch
FROM "STG_magento_de".sales_flat_order a
JOIN "STG_magento_de".sales_flat_order_temp b ON a.entity_id = b.entity_id

Upvotes: 0

Views: 239

Answers (1)

user330315
user330315

Reputation:

From the manual:

Note that the target table must not appear in the from_list, unless you intend a self-join (in which case it must appear with an alias in the from_list).

(emphasis mine)

So you actually want:

UPDATE "STG_magento_de".sales_flat_order
SET customer_id = b.customer_id, 
    ....
from sales_flat_order_temp b  --<< do NOT repeat the target table here
where "STG_magento_de".sales_flat_order = b.entity_id`

Unrelated, but: you should really avoid those dreaded quoted identifiers. They are much more trouble than they are worth.

Upvotes: 3

Related Questions