简体   繁体   English

改进 SCD2 解决方案的插入查询

[英]Improving insert query for SCD2 solution

I have two insert statements.我有两个插入语句。 The first query is to inserta new row if the id doesn't exist in the target table.如果目标表中不存在 id,第一个查询是插入一个新行。 The second query inserts to the target table only if the joined id hash value is different (indicates that the row has been updated in the source table) and the id in the source table is not null. These solutions are meant to be used for my SCD2 solution, which will be used for inserts of hundreds thousands of rows.第二个查询插入到目标表只有当加入的id hash值不同(表示该行已在源表中更新)并且源表中的id不是null。这些解决方案旨在用于我的SCD2 解决方案,将用于插入数十万行。 I'm trying not to use the MERGE statement for practices.我尽量不使用 MERGE 语句进行练习。

The columns "Current" value 1 indicates that the row is new and 0 indicates that the row has expired. “当前”列值 1 表示该行是新行,0 表示该行已过期。 I use this information later to expire my rows in the target table with my update queries.稍后我使用此信息通过更新查询使目标表中的行过期。

Besides indexing is there a more competent and effective way to improve my insert queries in a way that resembles the like of the SCD2 merge statement for inserting new/updated rows?除了索引之外,是否还有一种更有效的方法来改进我的插入查询,其方式类似于用于插入新/更新行的 SCD2 合并语句?

Query:询问:

Query 1:
INSERT INTO TARGET
SELECT Name,Middlename,Age, 1 as current,Row_HashValue,id
from Source s
Where s.id not in (select id from TARGET) and s.id is not null

Query 2:
INSERT INTO TARGET
SELECT Name,Middlename,Age,1 as current ,Row_HashValue,id
FROM SOURCE s 
LEFT JOIN TARGET t ON s.id = t.id
AND s.Row_HashValue = t.Row_HashValue
WHERE t.Row_HashValue IS NULL and s.ID IS NOT NULL

You can use WHERE NOT EXISTS , and have just one INSERT statement:您可以使用WHERE NOT EXISTS ,并且只有一个INSERT语句:

INSERT INTO TARGET
SELECT Name,Middlename,Age,1 as current ,Row_HashValue,id
FROM SOURCE s 
WHERE NOT EXISTS (
    SELECT 1
    FROM TARGET t 
    WHERE s.id = t.id
    AND s.Row_HashValue = t.Row_HashValue)
AND s.ID IS NOT NULL;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM