我的數據庫表有以下幾列: A1,A2,A3,A4,A5,A6SQL UPDATE表現不佳
我必須更新這些記錄和移位值留每當我遇到一個空列。目的不是在具有從左開始的值的列之間具有空值。例如,如果:
A1 = NULL , A2 = 1 , A3 = 4, A4 = 5, A5 = 9, A6 = 8
我有左移這樣的結果將是值:
A1 = 1, A2 = 4 , A3 = 5, A4 = 9, A5 = 8, A6 = NULL
到目前爲止,我想出了下面的查詢,但它是緩慢的。讓我知道,如果你可以調整查詢,使其更快。還有一件事,如果我在c#中這樣做呢?如果我循環遍歷數據行並更新每一行,會更快嗎?
UPDATE myTable SET
A5 = A6,
A6 = NULL
WHERE (A5 IS NULL) AND (NOT A6 IS NULL)
UPDATE myTable SET
A4 = A5,
A5 = A6
WHERE (A4 IS NULL) AND (NOT A5 IS NULL)
UPDATE myTable SET
A3 = A4,
A4 = A5,
A5 = A6
WHERE (A3 IS NULL) AND (NOT A4 IS NULL)
UPDATE myTable SET
A2 = A3,
A3 = A4,
A4 = A5,
A5 = A6
WHERE (A2 IS NULL) AND (NOT A3 IS NULL)
UPDATE myTable SET
A1 = A2,
A2 = A3,
A3 = A4,
A4 = A5,
A5 = A6
WHERE (A1 IS NULL) AND (NOT A2 IS NULL)
有什麼理由,你不只是在添加這些爲相關的表?通過這樣做違反了常規表單... http://en.wikipedia.org/wiki/First_normal_form#Repeating_groups_across_columns –
確實是一個糟糕的數據模型。 – duffymo
此表中還有其他列,我無法更改設計。我將不得不找到一種方法來在不改變設計的情況下做到這一點。這是一個數據模型不佳的舊系統。 – Sev