Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
Rohitb
Helper I
Helper I

Use of Unique Index in Fabric Warehouse

In fabric data warehouse, what is the significance of having Unique constraint with Not Enforced enabled? Even after creating this I am able to insert duplicates within the table

 

https://learn.microsoft.com/en-us/fabric/data-warehouse/table-constraints

Rohitb_0-1728546595892.png

 

https://stackoverflow.com/questions/54513136/when-we-should-use-not-enforced-with-foreign-key-constr...

1 ACCEPTED SOLUTION
NandanHegde
Super User
Super User

setting the NOT ENFORCED option means the data engine does not enforce the constraint. for example, if the unique / primary key constant is NOT ENFORCED, then the engine will not check for duplicates on insert. this can improve insert performance. in the case of foreign key, with NOT ENFORCED the relationship can be inserted / deleted via separate transactions. but the query engine will use the constraint as a hint, and tools can know about the constraint.

not all sql database support NOT ENFORCED, say SqlServer. in the case of Fabric Warehouse, its the opposite, that is it does not support ENFORCED. with Fabric Warehouse the constraints are just "hints"




----------------------------------------------------------------------------------------------
Nandan Hegde (MSFT Data MVP)
LinkedIn Profile : www.linkedin.com/in/nandan-hegde-4a195a66
GitHUB Profile : https://github.com/NandanHegde15
Twitter Profile : @nandan_hegde15
MSFT MVP Profile : https://mvp.microsoft.com/en-US/MVP/profile/8977819f-95fb-ed11-8f6d-000d3a560942
Topmate : https://topmate.io/nandan_hegde
Blog :https://datasharkx.wordpress.com

View solution in original post

5 REPLIES 5
mikeburek
Advocate II
Advocate II

Interestingly, in Azure Synapse Analytics Dedicated Pools, I can create incorrect results when I enter duplicate data into a PRIMARY KEY NOT ENFORCED or UNIQUE NOT ENFORCED.

 

But I cannot create incorrect results in Fabric Warehouse.

 

Since we cannot see execution plans for Fabric Warehouse, I can't tell if the execution engine really uses this as an optimization. But it clearly doesn't shortcut the results the way Azure Synapse Analytics Dedicated Pools does.

 

Does anyone have any interesting results they have found when putting duplicate data into a PRIMARY KEY or UNIQUE?

frithjof_v
Community Champion
Community Champion

The way I understand it, and as other commenters also are pointing to, the NOT ENFORCED constraints are used for query optimizations.

 

I think it's like we're telling the engine: "trust me, all values in this column are unique. If you find a value, you don't need to look for the same value in other rows." So the engine can stop looking, instead of spending time on looking through all the rows. It can probably speed up read operations like joins, where-statements, group by, etc.

 

But it might lead to incorrect results if the data doesn't conform to the not enforced constraint. E.g. the query engine stops looking for other occurences of a value, because we have told it that there is only one copy of that value (by adding the not enforced constraint), but if in reality there are more occurences of that value - then the query engine might miss those rows.

 

So, when adding NOT ENFORCED constraints, we are taking the responsibility for uniqueness. We can use the ETL process to check for uniqueness. The benefit is improved read performance.

NandanHegde
Super User
Super User

setting the NOT ENFORCED option means the data engine does not enforce the constraint. for example, if the unique / primary key constant is NOT ENFORCED, then the engine will not check for duplicates on insert. this can improve insert performance. in the case of foreign key, with NOT ENFORCED the relationship can be inserted / deleted via separate transactions. but the query engine will use the constraint as a hint, and tools can know about the constraint.

not all sql database support NOT ENFORCED, say SqlServer. in the case of Fabric Warehouse, its the opposite, that is it does not support ENFORCED. with Fabric Warehouse the constraints are just "hints"




----------------------------------------------------------------------------------------------
Nandan Hegde (MSFT Data MVP)
LinkedIn Profile : www.linkedin.com/in/nandan-hegde-4a195a66
GitHUB Profile : https://github.com/NandanHegde15
Twitter Profile : @nandan_hegde15
MSFT MVP Profile : https://mvp.microsoft.com/en-US/MVP/profile/8977819f-95fb-ed11-8f6d-000d3a560942
Topmate : https://topmate.io/nandan_hegde
Blog :https://datasharkx.wordpress.com
v-huijiey-msft
Community Support
Community Support

Hi @Rohitb ,

 

Constraints are not actually enforced in Fabric Warehouse, so the NOT ENFORCED keyword is required.

 

This is a limitation of horizontally scaling SQL services such as Fabric SQL, where these constraints are more for engine optimization.

 

Here is a case with a similar problem to yours that might be helpful:

Solved: UNIQUE constraint doesn't work in Fabric Warehouse - Microsoft Fabric Community

 

Best Regards,
Yang
Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

Rohitb
Helper I
Helper I

In fabric data warehouse, what is the significance of having Unique constraint with Not Enforced enabled? Even after creating this I am able to insert duplicates within the table

https://learn.microsoft.com/en-us/fabric/data-warehouse/table-constraints

Rohitb_0-1728546299243.png

 

https://stackoverflow.com/questions/54513136/when-we-should-use-not-enforced-with-foreign-key-constr...
Can someone help me with this ?

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

Find out what's new and trending in the Fabric Community.

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

November Update

Fabric Monthly Update - November 2024

Check out the November 2024 Fabric update to learn about new features.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.