Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12345»»»

The Pitfalls of Foreign Keys Expand / Collapse
Author
Message
Posted Friday, February 10, 2006 3:46 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Thursday, May 16, 2013 3:16 PM
Points: 2,749, Visits: 1,405
Comments posted to this topic are about the content posted at http://www.sqlservercentral.com/columnists/dpoole/thepitfallsofforeignkeys.asp

LinkedIn Profile
Post #257700
Posted Monday, February 20, 2006 11:34 PM


SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Tuesday, September 11, 2012 1:10 PM
Points: 649, Visits: 201
When working on deleting audit data from several very large tables without indexes, I borrowed a trick from warehousing: I created appropriate indexes, ran the deletes, then dropped the indexes. (To mention, in one case I had to use a query hint to force the use of the index--the only time I've ever had to do that.)

Philip



Post #260063
Posted Thursday, March 02, 2006 2:32 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Wednesday, April 20, 2011 8:50 AM
Points: 245, Visits: 11

Using DBMS-features like foreign keys, check constraints, defaults, unique keys, etc. as some sort of "last line of defence" to my opinion is crucial whenever designing/maintaining a database system, especially in "evolving systems" that are not out-of-the-box.

of course you could do all those things by "well tested SPs", but how can you ever assure to have your 1000+ procedures in a larger system up-to-date, especially when the underlying DB-table structure has to be changed?

the cost of such features is absolutely outweighed by the fact that you've done everything possible to ensure data integrity.




Post #262619
Posted Thursday, March 02, 2006 2:37 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, May 01, 2013 6:58 AM
Points: 12, Visits: 126

"scarcely necessary if creating, updating and deleting are done through well-tested stored procedures".

Where is it more likely that bugs will arise ? In a "well-tested" stored-proc, or in a foreign key declaration ?

With the no-foreignkeys approach, you end up writing all sorts of repair routines to search for & clean up orphaned data. Of course, this orphaned data should never exist, because these "well-tested" stored-procs are entirely 100% bug-free and never allow duff data into the database. Perhaps in fantasy-land that is true, but in the real world ... And of course, every application always uses these stored-procs, no application ever bypasses them and updates the data directly. That would never happen. No, of course not - that would just be daft, and lead to duff data.

If declarative integrity checks are used, you avoid wasting time on such cleanup activities. You state to the DBMS what things must be true, and let the DBMS worry about it. Make it work for its living - that's what it's there for! OK there is some (minimal) overhead, but I'd rather pay that cost and have good data, than save some minimal amount of performance and end up with crap data that needs sorting later.




Post #262620
Posted Thursday, March 02, 2006 4:26 AM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, February 12, 2013 8:35 AM
Points: 271, Visits: 314

Good article David.

But the first thing I thought was: Oh no, not another discussion on DRI...

Sounds a bit defeatist but, as with so many things it's impossible to get everything that you want. If someone creates a method to enforce DRI without the overheads involved then I'm sure we'll all be pleased to make them very rich. In the mean time, however, we're going to have to work around the constraints.

Enforcing DRI programmatically using stored procedures sounds like it's got some potential for disaster. Once the database ownership has been handed over, you can't rely on the initial protocol. I've seen people go to extraordinary lengths to remove and create their constraints and DRI programmatically, not something I would recommend for the faint of heart.

I would be in favour of being able to use TRUNCATE if there's no associated child data though. Does anyone know why this has been "blanket-enforced"?



Max
Post #262641
Posted Thursday, March 02, 2006 4:56 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: 2 days ago @ 1:59 AM
Points: 1,126, Visits: 924

Nice article

Particularly interesting for me, because in my spare time I am working on building a .net CMS system using SQL 2000 for the backend.  There have been a couple of occasions where I've been unsure whether to include DRI or not, but for now my approach is to always include it.

Good stuff.

Post #262645
Posted Thursday, March 02, 2006 5:12 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, August 04, 2009 7:44 AM
Points: 263, Visits: 44
Interesting consideration, but of course with no DRI your database is not a relational database, just a convenient collection of tables written to by applications.  (You could potentially gain more performance gain by using a CSV file for each table and bypassing SQL Server alltogether. )
Post #262654
Posted Thursday, March 02, 2006 5:43 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: 2 days ago @ 8:34 AM
Points: 4, Visits: 45
Some additional thoughts:
Re: Deletes are expensive
1. How often is a delete from a code table performed (hint if there are <100, <1000 entries?) There may be no need for optimization
2. If you are doing a lot of deletes as in a DW feed, then you can use disable/enable check/no check to improve performance. Then TRUNCATE is also an option.
Re: DRI not necessary
1. At least create the DRI with the NO CHECK option to allow tools that read metadata to understand the joins. There is no performance argument against a NO CHECK constraint. Most DB tools nowadays use this information.
2. At some point in Development and Testing, you should turn appropriate DRI on to keep persons honest. So I would advocate turning to NO CHECK any constraints in PRODUCTION that you feel cause performance concerns.
3. If developers instinctively know about integrity, why do some have no clue when they get an RI constraint violation? I frequently use Triggers vs DRI to give a more precise error message including the value of the FK column that caused the problem, and the name of the PK attribute. Some developers STILL email the message/stack trace to me and ask what it means. This way, I can answer the question without doing any research, and accomplishing a training goal in the same breath

Recommendations:
1. Choose only between creating a disabled or enabled constraint. Even if you use RI triggers or SPs.
2. Perform Performance Tuning by eliminating constraints the same way you add indexes- on an as needed basis. Wait until you have a problem, then disable the constraint/trigger.
2. Consider temporarily disabling constraints for certain high volume activities. Ditto for special use indexes as another poster mentions.
Post #262660
Posted Thursday, March 02, 2006 6:12 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 12:19 PM
Points: 13,371, Visits: 25,144

That was an outstanding article. Thanks.

We're currently in a bit of a struggle with a development team that has become completely enamored with generated code. Problem is, with type of coding being generated it's a lot of extra work to determine if a record has been edited. Therefore, they want to simply delete complete record sets and re-insert them. We've been arguing against it at length, and you've just given us some more ammunition. Thanks again.

BTW, our own tests, while not focusing on DRI, do bear out most of your tests. For those who are concerned that he did the test on a code table, which is unlikely to change, our developers are deleting from an active table with about eight child tables, so the paradigm holds up.



----------------------------------------------------
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood..." Theodore Roosevelt
The Scary DBA
Author of: SQL Server 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #262674
Posted Thursday, March 02, 2006 6:43 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, May 08, 2012 9:26 AM
Points: 237, Visits: 23
Good article. Quick question, has anyone ever experienced the case where the foreign keys ARE indexed but a table scan appears to take place on referential deletes?


Post #262685
« Prev Topic | Next Topic »

Add to briefcase 12345»»»

Permissions Expand / Collapse