[英]PostgreSQL updating one table from another
編輯:抱歉,我應該更好地解釋一下,數據已從Salesforce中淘汰,從后端轉儲,ID字段為字母數字[例如。 00190000010PBdSAAX],通常長度均為18,並且總是唯一的。 我將對數據類型進行一些更改,擺脫引號引起的標識符,並對索引進行一些更改,看看如何!
我正在使用PostgreSQL 9.5。 我正在將1個表更新為另一個表,兩個表的結構相同,一個表具有[目標] 200萬條記錄,另一個表[源]大約70k,這基本上是對現有表執行更新,並使用唯一的表插入任何新記錄要檢查的ID。
它比我想象的要花費更長的時間,即使沒有什么要更新並且只掃描記錄,仍然有5分鍾甚至更長的時間才能更新,無論有沒有索引,都嘗試將其中的2個字段加入其中略有不同的方式(僅使用WHERE和實際JOIN)。 只想知道是否有更好的方法來做,或者我做錯了什么,只使用了Postgres幾天。
我知道5分鍾沒什么大不了的(如果執行任何更新,則時間會更長),但是它與大約9個其他表的過程類似,這是中等大小的示例
兩個表都如下所示[但僅具有不同的表名]
CREATE TABLE public."Cases"
(
"Past_Due__c" character varying(255),
"Case_Age__c" character varying(255),
"Next_Step_Due_Date__c" character varying(255),
"Id" character varying(255),
"AccountId" character varying(255),
"Account_Number__c" character varying(255),
"Account_Type__c" character varying(255),
"CaseNumber" character varying(255),
"CaseSubTypeDetail__c" character varying(255),
"Case_Sub_Type__c" character varying(255),
"Case_Type__c" character varying(255),
"ClosedDate" character varying(255),
"Collections_Step__c" character varying(255),
"Customer_Number__c" character varying(255),
"Next_Collections_Step__c" character varying(255),
"Origin" character varying(255),
"Priority" character varying(255),
"Related_Complaint_Case__c" character varying(255),
"Status__c" character varying(255),
"Subject" text,
"Type" character varying(255),
"CreatedDate" character varying(255),
"OwnerId" character varying(255),
"ContactId" character varying(255),
"Status" character varying(255),
"Case_Comments__c" text,
"Subscription__c" character varying(255),
"Description" text,
"Case_Outcome__c" text,
"Case_Outcome_Reason__c" text,
"Adjustment_Amount__c" character varying(255),
"Product_Adjustment_Amount__c" character varying(255),
"Product_Adjustment_Reason__c" character varying(255),
"Service__c" character varying(255),
"ParentId" character varying(255)
)
WITH (
OIDS=FALSE
);
更新腳本如下
update public."cases" t2
set past_due__c = t1.past_due__c, case_age__c = t1.case_age__c, next_step_due_date__c = t1.next_step_due_date__c, accountid = t1.accountid, account_number__c = t1.account_number__c, account_type__c = t1.account_type__c, casesubtypedetail__c = t1.casesubtypedetail__c, case_sub_type__c = t1.case_sub_type__c, case_type__c = t1.case_type__c, closeddate = t1.closeddate, collections_step__c = t1.collections_step__c, customer_number__c = t1.customer_number__c, next_collections_step__c = t1.next_collections_step__c, origin = t1.origin, priority = t1.priority, related_complaint_case__c = t1.related_complaint_case__c, status__c = t1.status__c, subject = t1.subject, type = t1.type, ownerid = t1.ownerid, contactid = t1.contactid, status = t1.status, case_comments__c = t1.case_comments__c, subscription__c = t1.subscription__c, description = t1.description, case_outcome__c = t1.case_outcome__c, case_outcome_reason__c = t1.case_outcome_reason__c, adjustment_amount__c = t1.adjustment_amount__c, product_adjustment_amount__c = t1.product_adjustment_amount__c, product_adjustment_reason__c = t1.product_adjustment_reason__c, service__c = t1.service__c, parentid = t1.parentid, billing_account__c = t1.billing_account__c, billing_account_credit_balance__c = t1.billing_account_credit_balance__c, billing_address__c = t1.billing_address__c, lastmodifiedbyid = t1.lastmodifiedbyid, lastmodifieddate = t1.lastmodifieddate
from public."temp_update_cases" t1
where t1.id = t2.id
我想出的所有其他事情我都想通了,但這是殺了我
您的查詢基本上是這樣的:
update public."cases" t2
set . . .
from public."temp_update_cases" t1
where t1.id = t2.id;
我建議索引:
create index idx_cases_id on public."cases"(id);
create index idx_temp_updte_cases_id on public."temp_update_cases"(id);
筆記:
id
的列是不錯的選擇。 serial
是主鍵更好的選擇。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.