2015-12-17 73 views
0

我正在尝试更新表格中的6000行左右,但我的查询从未结束。 我已经把数据更新到临时表中并使用连接来更新行。 这在Sql Server中工作得非常快,但在Postgresql中它永远不会结束。 我正在更新大约40列。 这是我正在运行的SQL。在Postgresql中更新表格行花费太多时间

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 
+0

您是否看过这里发布的解决方案http://dba.stackexchange.com/questions/41059/optimizing-bulk-update-performance-in-postgresql?newreg=4b48689f176940b2bd4862b8edd83425? – Dukefirehawk

+0

是的。我正在使用那里提到的使用临时表的方法。我没有索引,我也使用temp_buffer 3000MB。但仍然永远运行。我的数据只有6000行。 – jmf

回答

3

From the manual:

注意,目标表不得出现在from_list,除非你打算自连接(在这种情况下,它必须与from_list别名出现)。

(重点煤矿)

所以你真的想:

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` 

无关,而是:你应该避免那些可怕的带引号的标识符。他们比他们的价值更麻烦。

+0

其实PGAdmin花费太多时间来运行更新。当我使用Taled运行更新查询时,只需要一秒钟。 – jmf