问题
Disclaimer: My DB knowledge comes mostly from Mysql so I might misunderstand some things in vertica...
I would like to know if there exist a technique of inserting/updating values in vertica while enforcing unique constrains across multiple sessions. Let's assume I have a table:
'id', 'unique_field', 'some_filed'
And there is a unique constraint on unique_field. My understanding is that in vertica one first needs to do an insert and then do ANALYZE_CONSTRAINTS to verify if the constraint was violated. In my specific case, I have multiple sessions preforming INSERTS to the same table and they may try to push the same record. The sequence for a single session appears to be:
Insert into table (id, unique_field, some_field) values (5, 'abc', 'data');
SELECT ANALYZE_CONSTRAINTS(table);
COMMIT;
If I run this sequence in two separate sessions, there is a chance that both queries try to insert the same unique_field and for each of them there will be no constrain violation detected as they're in separate transactions, which will result in duplicate data.
Is there a better way to prevent this from happening ? Or I'm missing something.
回答1:
Vertica does not enforce uniqueness of primary key or unique constraints on load due to the potential overhead associated with the operation.
If ANALYZE_CONSTRAINTS()
is run before committing, you should be able to capture potential duplicates. There's also overhead with doing UPDATE
s and those should be avoided.
There are ways to enforce uniqueness on load such as using MERGE
, or staging the data in a temporary table. Each method has its own limitations. You can read more about enforcing uniqueness of data in my blog post.
Update: As of 7.2, Vertica can automatically enforce primary and unique key constraints. See the documentation for more information.
来源:https://stackoverflow.com/questions/26019462/unique-contrains-in-vertica-db