Download presentation
Presentation is loading. Please wait.
1
Where Are My (Primary) Keys?
Ami Levin Say friend, did you know that the US Standard railroad gauge (distance between the rails) is 4 feet, 8 1/2 inches. That's an exceedingly odd number. Why was that gauge used? Because that's the way they built them in England, and the US railroads were built by English expatriates. I see, but why did the English build them like that? Because the first railway lines were built by the same people who built the pre-railroad tramways, and that's the gauge they used. Well, why did they use that gauge in England? Because the people who built the tramways used the same jigs and tools that they used for building wagons, which used that wheel spacing. Okay! Why did their wagons use that odd wheel spacing? Because, if they tried to use any other spacing the wagon wheels would break on some of the old, long distance roads. Because that's the spacing of the old wheel ruts. So who built these old rutted roads? The first long distance roads in Europe were built by Imperial Rome for the benefit of their legions. The Roman roads have been used ever since. And the ruts? The original ruts, which everyone else had to match for fear of destroying their wagons, were first made by the wheels of Roman war chariots. Since the chariots were made for or by Imperial Rome they were all alike in the matter of wheel spacing. Thus, we have the answer to the original question. The United States standard railroad gauge of 4 feet, 8 1/2 inches derives from the original specification for an Imperial Roman army war chariot. And the motto of the story is Specifications and bureaucracies live forever. So, the next time you are handed a specification and wonder what horse's ass came up with it, you may be exactly right. Because the Imperial Roman chariots were made to be just wide enough to accommodate the back-ends of two war-horses. So, just what does this have to do with the exploration of space? Well, there's an interesting extension of the story about railroad gauge and horses' behinds. When we see a Space Shuttle sitting on the launch pad, there are two big booster rockets attached to the sides of the main fuel tank. These are the solid rocket boosters, or SRBs. The SRBs are made by Thiokol at a factory in Utah. The engineers who designed the SRBs might have preferred to make them a bit fatter, but the SRBs had to be shipped by train from the factory to the launch site. The railroad from the factory runs through a tunnel in the mountains. The SRBs had to fit through that tunnel. The tunnel is slightly wider than a railroad track, and the railroad track is about as wide as two horses' behinds. So a major design feature of what is arguably the world's most advanced transportation system was originally determined by the width of a horse's ass.
2
Session Goals Revisit one of the fundamental design principals of relational databases - key selection. Explore the controversies associated with it from a very practical, hands-on perspective, with a special emphasis on some surprising performance issues that may arise from sub-optimal selection of keys... 3 | Where are my (primary) keys?
3
What is a Primary Key?
4
Keys Simple Composite Candidate Primary Artificial / Surrogate
Intelligent Natural 12 | Where are my (primary) keys?
5
The Debate To ID or not to ID? IDENTITY (1,1) vs. Natural key 13 |
13 | Where are my (primary) keys?
6
The Forefathers 14 | Where are my (primary) keys?
7
Source of ‘ID’ concept Punched cards and magnetic tapes had physical, sequential access addresses Memory address used as physical pointers Spinning hard disks came, but the sequential mindset remained RDBMS is a logical model Keys in RM are logical entities Physical Data Independence (PDI) 15 | Where are my (primary) keys?
8
Pro Artificial (I) In some cases, no natural key exists and an artificial key is the only option. Examples? 16 | Where are my (primary) keys?
9
Pro Artificial (II) Natural keys can change.
Artificial keys never change. How Often? Cascading referential constraints Artificial keys can change 17 | Where are my (primary) keys?
10
Pro Artificial (III) Natural keys may be long and complex.
Become longer with each level 900 Bytes limit in SQL Server Multi-column joins 18 | Where are my (primary) keys?
11
Pro Artificial (IX) Artificial keys help improve performance.
Simpler join predicates Ever increasing clustering effect Short keys = Smaller DB = Faster 19 | Where are my (primary) keys?
12
Pro Artificial (X) Artificial keys reduce clustered index fragmentation. Minimize maintenance down time What about deletes? What about non-clustered indexes? 20 | Where are my (primary) keys?
13
Pro Natural (I) Natural keys have business meaning.
Artificial keys are never queried for 21 | Where are my (primary) keys?
14
Pro Natural (II) Queries on tables using natural keys require fewer joins. The more familiar and meaningful the key, the less joins are required “Bypass” joins 22 | Where are my (primary) keys?
15
Pro Natural (III) Data consistency is maintained explicitly when using natural keys. Artificial keys enable logical duplicates 23 | Where are my (primary) keys?
16
Pro Natural (IX) Natural keys eliminate potential physical clustering performance issues. Contention for clustered regions 24 | Where are my (primary) keys?
17
Less Mentioned Issues (I)
Artificial keys are the de-facto standard. ORMs generate artificial keys LINQ doesn’t cache composite key rows … 25 | Where are my (primary) keys?
18
Less Mentioned Issues (II)
Data statistics and optimizations. Statistics on artificial keys are useless for parameter sniffing Estimations on composite key statistics are less accurate 26 | Where are my (primary) keys?
19
Less Mentioned Issues (III)
Modularity and portability. Migration to other platforms Merging with other databases 27 | Where are my (primary) keys?
20
Less Mentioned Issues (IX)
Simplicity and aesthetics. 28 | Where are my (primary) keys?
21
Demo Spec A database of web sites. URL Country and city of owner
Country ISO code for external app Data consistency is crucial 29 | Where are my (primary) keys?
22
Natural vs. Artificial Keys
DEMO 30 | Where are my (primary) keys?
23
Ask Yourself Is there a natural key that I can use as a primary key?
Are there a few natural candidates? Which one is the simplest and most familiar? How stable is it? How will it be used logically? What will be the physical access patterns for this table? What are the common query types for this table? 31 | Where are my (primary) keys?
24
For More Information A Relational Model of Data for Large Shared Data Banks (E.F. CODD) The Relational Model for Database Management: Version 2 (E.F. Codd) An introduction to database systems (C.J. Date) Database in Depth: Relational Theory for Practitioners (C.J. Date) The Database Relational Model: A Retrospective Review and Analysis (C.J. Date) Joe Celko's Data and Databases: Concepts in Practice (J. Celko) Joe Celko's SQL for Smarties, Fourth Edition: Advanced SQL Programming (J. Celko) Database Modeling and Design, Fifth Edition: Logical Design (T.J. Teorey, S.S. Lightstone, T. Nadeau, and H.V. Jagadish) Pro SQL Server 2008 Relational Database Design and Implementation (L. Davidson, K. Kline, S. Klein, and K. Windisch) 32 | Where are my (primary) keys?
25
Where Are My Keys? Q&A? 33 | Where are my (primary) keys?
26
Where Are My Keys? 34 | Where are my (primary) keys?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.