10 WAYSTO WRECK YOURDATABASE   Josh Berkus   PostgreSQL Experts, Inc.   For OReilly Media, July 2009
REVENGEagainst your boss
guarantee yourselfJOB SECURITY
ensurePROJECTFAILURE
make sureNOBODY KNOWSTHE REAL DATA
open adatabaseBACKDOOR
force aTOTALREWRITEof the code
SLOWDOWNthe applicationto a crawl
make those stupidDEVELOPERSMISERABLE
10 Ways         to:  Lose Data  Be InsecureBecome Unmaintainable  Stife Changes         and Make Everyone  Miserable
One Big Spreadsheet
2. ranDom_naming(s)          plural vs. singular●    users          ●                       profle●    forum          ●   ...
2. ranDom_naming(s)        camel vs. underscore●    users            ●                         userProfle●    threadCatego...
2. ranDom_naming(s)                    felds●    users             ●                          users    ●        frstName  ...
2. ranDom_naming(s)             Hungarian Notation●    tblUsers            ●                            tblUsers    ●     ...
3&4We dont need no stinking keys
3. No Real Keys●    Users    ●        userID AUTONUMBER PRIMARY KEY    ●        frst_name    ●        last_name    ●      ...
3. No Real KeysID    first_name last_name         email         login password active level21       Josh     Berkus     jo...
4. No Foreign Keys                     posts.content                      Josh Berkus                       Whats up? user...
4. Fun with Orphan Rows                    posts.content                    Josh Berkus                     Whats up? user...
4. Fun With UpdatesINSERT INTO threads VALUES ( .... );if $dbh(success) then   while $these_posts.date > $cutdate     UPDA...
5. No Constraints●    Users    ●        userID AUTONUMBER PRIMARY KEY    ●        frst_name TEXT    ●        last_name TEX...
5. No Constraints●    Users    ●        userID AUTONUMBER PRIMARY KEY    ●        frst_name VARCHAR (30)    ●        last_...
5. No Constraintsfirst_name   last_name        email           login     password active      level   Josh        Berkus  ...
6. Non-Atomic Fields                   name                Josh BerkusSELECT SUBSTR(name,STRPOS(name,  )) ...             ...
6. Non-Atomic Fields                  name               Josh BerkusSELECT SUBSTR(name,STRPOS(name,  )) ...               ...
6. Non-Atomic Fields●    Account Type    ●        5400 active individual    ●        5401 inactive individual    ●        ...
7. Magic Numbers   ID = 0
7. Magic Numbers        ID = 0SELECT case_file, count(1)FROM case_eventsWHERE ID > 0AND scheduled = TRUEGROUP BY case_file;
7. Magic Numbers2008-02-30
7. Magic Numbers      2008-02-30SELECT user_id, min(date)FROM user_postsWHERE NOT (  extract(month from date) = 2  and ext...
7. Magic Numbers-1, 1, 2, 3, 4, 5, 100
7. Magic Numbers-1, 1, 2, 3, 4, 5, 100 SELECT household, avg(headcount) FROM house WHERE headcount BETWEEN 0 and 99 GROUP ...
7. Magic Numbers   $-0.01
7. Magic Numbers     $-0.01SELECT *FROM invoicesWHERE total <> $0.01  AND sent_on IS NULL;
7. Magic Numbers Document themonly in your code!
8. Polymorphic Fields    Name         AccountType PrefContact     ContactInfoJosh Berkus      Individual   Email        jo...
9. EAV (entity-attribute-value)         ID    Property   Setting         407    Eyes      Brown         407    Height     ...
9. EAV (entity-attribute-value)●    Diffcult tasks:    ●        Find out how many men have brown hair and are        over ...
9. EAV 2.0: E-BLOB ID            Properties 407   <eyes=”brown”><height=”73”>        <married=”1”><smoker=”1”> 408    <hai...
10. Let The ORMTake Care Of It
The Ten Ways1.one big           6.non-atomic felds spreadsheet        7.magic numbers2.random naming     8.polymorphic3.no...
Ten Ways to Fix Your Database1.normalization   6.atomic felds2.consistent      7.atomic felds naming           8.atomic fe...
More Wreckage●    Simplifying Database Design Tutorial    –   OSCON, Monday 8:30 AM●    PostgreSQL Day San Jose    –   Bef...
Upcoming SlideShare
Loading in...5
×

Ten Ways to Destroy Your Database

1,211

Published on

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,211
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
8
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Ten Ways to Destroy Your Database

  1. 1. 10 WAYSTO WRECK YOURDATABASE Josh Berkus PostgreSQL Experts, Inc. For OReilly Media, July 2009
  2. 2. REVENGEagainst your boss
  3. 3. guarantee yourselfJOB SECURITY
  4. 4. ensurePROJECTFAILURE
  5. 5. make sureNOBODY KNOWSTHE REAL DATA
  6. 6. open adatabaseBACKDOOR
  7. 7. force aTOTALREWRITEof the code
  8. 8. SLOWDOWNthe applicationto a crawl
  9. 9. make those stupidDEVELOPERSMISERABLE
  10. 10. 10 Ways to: Lose Data Be InsecureBecome Unmaintainable Stife Changes and Make Everyone Miserable
  11. 11. One Big Spreadsheet
  12. 12. 2. ranDom_naming(s) plural vs. singular● users ● profle● forum ● styles● threads ● images● posts ● survey● message ● faqs
  13. 13. 2. ranDom_naming(s) camel vs. underscore● users ● userProfle● threadCategory ● user_styles● threads ● post_images● posts ● survey● privateMessage ● faqs
  14. 14. 2. ranDom_naming(s) felds● users ● users ● frstName ● is_active ● last_name ● ACL ● login ● email ● crt_date ● lstLogin
  15. 15. 2. ranDom_naming(s) Hungarian Notation● tblUsers ● tblUsers ● txtFirstName ● bool_is_active ● str_last_name ● int4ACL ● txtLogin ● email ● dtcrt_date ● tmslstLogin
  16. 16. 3&4We dont need no stinking keys
  17. 17. 3. No Real Keys● Users ● userID AUTONUMBER PRIMARY KEY ● frst_name ● last_name ● login ● email ● is_active
  18. 18. 3. No Real KeysID first_name last_name email login password active level21 Josh Berkus josh@pgexperts.com josh jehosaphat TRUE u47 Joshua Berkus josh@pgexperts.com joshb dbguy TRUE u198 Josh Berkus josh@postgresql.org joshb jehosaphat FALSE u204 Josh Berkus josh@agliodbs.com jberkus joshiam TRUE a
  19. 19. 4. No Foreign Keys posts.content Josh Berkus Whats up? users.login Im going crazy!Josh Berkus jberkus www.pornking.com jerkyboy Why? selena www.whitehouse.com OSCON! Its too much! www.whiteslavery.com www.lolcats.com I told you so ...
  20. 20. 4. Fun with Orphan Rows posts.content Josh Berkus Whats up? users.login Im going crazy!Josh Berkus jberkus Why? selena www.whitehouse.com OSCON! Its too much! www.lolcats.com I told you so ...
  21. 21. 4. Fun With UpdatesINSERT INTO threads VALUES ( .... );if $dbh(success) then while $these_posts.date > $cutdate UPDATE posts SET thread = $newthread WHERE id = $these_posts.id; if not $dbh(success) then while $these_posts.id > $last_id UPDATE posts SET thread = $oldthread WHERE id = $these_posts.id; DELETE FROM threads WHERE id = $newthread;
  22. 22. 5. No Constraints● Users ● userID AUTONUMBER PRIMARY KEY ● frst_name TEXT ● last_name TEXT ● login TEXT ● email TEXT ● is_active TEXT
  23. 23. 5. No Constraints● Users ● userID AUTONUMBER PRIMARY KEY ● frst_name VARCHAR (30) ● last_name VARCHAR (30) ● login VARCHAR (20) ● email VARCHAR (40) ● is_active BOOLEAN
  24. 24. 5. No Constraintsfirst_name last_name email login password active level Josh Berkus josh@pgexperts.com jberkus jehosaphat TRUE a NULL NULL kelley@ucb k NULL FALSE u Mike Hunt www.pornking.com c34521 c34521 TRUE I S F gavin@sf.gov gavin twitter NULL x
  25. 25. 6. Non-Atomic Fields name Josh BerkusSELECT SUBSTR(name,STRPOS(name, )) ... status a … WHERE status = a OR status = u ...
  26. 26. 6. Non-Atomic Fields name Josh BerkusSELECT SUBSTR(name,STRPOS(name, )) ... status i … WHERE status = ??? ...
  27. 27. 6. Non-Atomic Fields● Account Type ● 5400 active individual ● 5401 inactive individual ● 5600 active board individual ● 5601 inactive board individual ● 6600 active corporate donor ● 6601 active corporate board member ● 6602 inactive corporate donor ● 6603 inactive corporate board member
  28. 28. 7. Magic Numbers ID = 0
  29. 29. 7. Magic Numbers ID = 0SELECT case_file, count(1)FROM case_eventsWHERE ID > 0AND scheduled = TRUEGROUP BY case_file;
  30. 30. 7. Magic Numbers2008-02-30
  31. 31. 7. Magic Numbers 2008-02-30SELECT user_id, min(date)FROM user_postsWHERE NOT ( extract(month from date) = 2 and extract (day from date) = 30 )GROUP BY user_id;
  32. 32. 7. Magic Numbers-1, 1, 2, 3, 4, 5, 100
  33. 33. 7. Magic Numbers-1, 1, 2, 3, 4, 5, 100 SELECT household, avg(headcount) FROM house WHERE headcount BETWEEN 0 and 99 GROUP BY household;
  34. 34. 7. Magic Numbers $-0.01
  35. 35. 7. Magic Numbers $-0.01SELECT *FROM invoicesWHERE total <> $0.01 AND sent_on IS NULL;
  36. 36. 7. Magic Numbers Document themonly in your code!
  37. 37. 8. Polymorphic Fields Name AccountType PrefContact ContactInfoJosh Berkus Individual Email josh@pgexperts.comJohn Dillinger Board Phone 415-555-1212Pearson Company NULL www.pearson.comClorox Company Jackie 510-555-1111 x202
  38. 38. 9. EAV (entity-attribute-value) ID Property Setting 407 Eyes Brown 407 Height 73in 407 Married? TRUE 408 Married? FALSE 408 Smoker FALSE 408 Age 37 409 Height 66in
  39. 39. 9. EAV (entity-attribute-value)● Diffcult tasks: ● Find out how many men have brown hair and are over 6 feet? ● Make marital status and age required, but not other things. ● Apply constraints to feld values.
  40. 40. 9. EAV 2.0: E-BLOB ID Properties 407 <eyes=”brown”><height=”73”> <married=”1”><smoker=”1”> 408 <hair=”brown”><age=”49”> <married=”0”><smoker=”0”> 409 <age=”37”><height=”66”> <hat=”old”><teeth=”gold”>
  41. 41. 10. Let The ORMTake Care Of It
  42. 42. The Ten Ways1.one big 6.non-atomic felds spreadsheet 7.magic numbers2.random naming 8.polymorphic3.no keys felds4.no foreign keys 9.EAV & EBlob5.no constraints 10.ORM
  43. 43. Ten Ways to Fix Your Database1.normalization 6.atomic felds2.consistent 7.atomic felds naming 8.atomic felds3.keys 9.limit EAV &4.foreign keys EBlob5.constraints 10.DB design
  44. 44. More Wreckage● Simplifying Database Design Tutorial – OSCON, Monday 8:30 AM● PostgreSQL Day San Jose – Before OSCON, Sunday 19th, all day – wiki.postgresql.org/wiki/PgDaySanJose2009● Me – PostgreSQL Experts: www.pgexperts.com – blog: it.toolbox.com/blogs/database-soup – email: josh@pgexperts.com This presentation copyright 2009 Josh Berkus, licensed for distribution under the Creative Commons Attribution License.
  1. ¿Le ha llamado la atención una diapositiva en particular?

    Recortar diapositivas es una manera útil de recopilar información importante para consultarla más tarde.

×