Unique Contrains in Vertica DB

半世苍凉 提交于 2019-12-02 10:38:07

问题


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 UPDATEs 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!