Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Better Rails by Knowing Better Database

Dingding Ye
September 27, 2014

Better Rails by Knowing Better Database

It's my presentation at Rails Pacific. Knowing the database concept and implementation of Integrity, Locking, Transaction, Index, etc will help developers much better on data modeling design and build solid production applications.

Dingding Ye

September 27, 2014
Tweet

More Decks by Dingding Ye

Other Decks in Technology

Transcript

  1. 1FSGPSNBODF$BVTFT • SQL Statement • Data Model Design • DB

    Configurations • Indexes • Query Cache • Concurrency model • Data cache • Deadlocks
  2. id name email tasks_count 1 Dingding [email protected] 28 2 Ben

    [email protected] 12 3 Roy [email protected] 8 4 Terry [email protected] 2 5 JiaQi [email protected] 4 pry(main) > User.find(4) User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 4 LIMIT 1
  3. id name email tasks_count 1 Dingding [email protected] 28 2 Ben

    [email protected] 12 3 Roy [email protected] 8 4 Terry [email protected] 2 5 JiaQi [email protected] 4 pry(main) > User.find(4) User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 4 LIMIT 1 Is ID equal to 4?
  4. id name email tasks_count 1 Dingding [email protected] 28 2 Ben

    [email protected] 12 3 Roy [email protected] 8 4 Terry [email protected] 2 5 JiaQi [email protected] 4 pry(main) > User.find(4) User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 4 LIMIT 1
  5. id name email tasks_count 1 Dingding [email protected] 28 2 Ben

    [email protected] 12 3 Roy [email protected] 8 4 Terry [email protected] 2 5 JiaQi [email protected] 4 pry(main) > User.find(4) User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 4 LIMIT 1 Is ID equal to 4?
  6. id name email tasks_count 1 Dingding [email protected] 28 2 Ben

    [email protected] 12 3 Roy [email protected] 8 4 Terry [email protected] 2 5 JiaQi [email protected] 4 pry(main) > User.find(4) User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 4 LIMIT 1
  7. id name email tasks_count 1 Dingding [email protected] 28 2 Ben

    [email protected] 12 3 Roy [email protected] 8 4 Terry [email protected] 2 5 JiaQi [email protected] 4 pry(main) > User.find(4) User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 4 LIMIT 1 Is ID equal to 4?
  8. id name email tasks_count 1 Dingding [email protected] 28 2 Ben

    [email protected] 12 3 Roy [email protected] 8 4 Terry [email protected] 2 5 JiaQi [email protected] 4 pry(main) > User.find(4) User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 4 LIMIT 1
  9. id name email tasks_count 1 Dingding [email protected] 28 2 Ben

    [email protected] 12 3 Roy [email protected] 8 4 Terry [email protected] 2 5 JiaQi [email protected] 4 pry(main) > User.find(4) User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 4 LIMIT 1 Is ID equal to 4?
  10. id name email tasks_count 1 Dingding [email protected] 28 2 Ben

    [email protected] 12 3 Roy [email protected] 8 4 Terry [email protected] 2 5 JiaQi [email protected] 4 pry(main) > User.find(4) User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 4 LIMIT 1
  11. id name email tasks_count 1 Dingding [email protected] 28 2 Ben

    [email protected] 12 3 Roy [email protected] 8 4 Terry [email protected] 2 5 JiaQi [email protected] 4 pry(main) > User.find(4) User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 4 LIMIT 1 1 2 3 4 5
  12. id name email tasks_count 1 Dingding [email protected] 28 2 Ben

    [email protected] 12 3 Roy [email protected] 8 4 Terry [email protected] 2 5 JiaQi [email protected] 4 pry(main) > User.find(4) User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 4 LIMIT 1 1 2 3 4 5
  13. # 5SFF 5 8 9 2 2 3 4 1

    5 9 10 8 User.find(4)
  14. # 5SFF 5 8 9 2 2 3 4 1

    5 9 10 8 User.find(4) < 5
  15. # 5SFF 5 8 9 2 2 3 4 1

    5 9 10 8 User.find(4) < 5 > 2
  16. # 5SFF 5 8 9 2 2 3 4 1

    5 9 10 8 User.find(4) < 5 > 2
  17. # 5SFF 5 8 9 2 2 3 4 1

    5 9 10 8 User.find(4) < 5 > 2
  18. # 5SFF 5 8 9 2 2 3 4 1

    5 9 10 8 User.find(4) < 5 > 2 Users
  19. # 5SFF 5 8 9 2 2 3 4 1

    5 9 10 8 User.find(4) < 5 > 2 Users A = V
  20. # 5SFF 5 8 9 2 2 3 4 1

    5 9 10 8 User.find(4) < 5 > 2 Users A = V A < V
  21. # 5SFF 5 8 9 2 2 3 4 1

    5 9 10 8 User.find(4) < 5 > 2 Users A = V A < V V1 <= A <= V2
  22. # 5SFF 5 8 9 2 2 3 4 1

    5 9 10 8 User.find(4) < 5 > 2 Users A = V A < V V1 <= A <= V2 A > V
  23. • Atomicity: Either all operations of the transaction should be

    reflected properly in the database, or none are.
  24. • Atomicity: Either all operations of the transaction should be

    reflected properly in the database, or none are. • Consistency. Execution of the individual transaction should preserve the consistency of the database.
  25. • Atomicity: Either all operations of the transaction should be

    reflected properly in the database, or none are. • Consistency. Execution of the individual transaction should preserve the consistency of the database. • Isolation. Concurrently executing transactions should be isolated from one another, so that each has the impression that no other transaction is executing concurrently with it.
  26. • Atomicity: Either all operations of the transaction should be

    reflected properly in the database, or none are. • Consistency. Execution of the individual transaction should preserve the consistency of the database. • Isolation. Concurrently executing transactions should be isolated from one another, so that each has the impression that no other transaction is executing concurrently with it. • Durability. After a transaction commited, the changes it has made to the database should persist, even if there are system failures.
  27. $PODVSSFOU.PEFM Transaction 1 Transaction 2 User.transaction do sleep 3 user

    = User.find(3) user.name = "dingding- #{Time.now.to_i}" user.save end User.transaction do user = User.find(3) puts “1”+user.name sleep 5 user2 = User.find(3) puts “1”+user.name end
  28. $PODVSSFOU.PEFM Transaction 1 Transaction 2 User.transaction do sleep 3 user

    = User.find(3) user.name = "dingding- #{Time.now.to_i}" user.save end User.transaction do user = User.find(3) puts “1”+user.name sleep 5 user2 = User.find(3) puts “1”+user.name end
  29. Transaction 1 Transaction 2 User.transaction(:read_ committed) do sleep 3 user

    = User.find(3) user.name = "dingding- #{Time.now.to_i}" user.save end User.transaction(:rea d_committed) do user = User.find(3) puts “1”+user.name sleep 5 user2 = User.find(3) puts “1”+user.name end
  30. Transaction 1 Transaction 2 User.transaction(:read_ committed) do sleep 3 user

    = User.find(3) user.name = "dingding- #{Time.now.to_i}" user.save end User.transaction(:rea d_committed) do user = User.find(3) puts “1”+user.name sleep 5 user2 = User.find(3) puts “1”+user.name end
  31. Transaction 1 Transaction 2 User.transaction(:read_ committed) do sleep 3 user

    = User.find(3) user.name = "dingding- #{Time.now.to_i}" user.save end User.transaction(:rea d_committed) do user = User.find(3) puts “1”+user.name sleep 5 user2 = User.find(3) puts “1”+user.name end NO
  32. Transaction 1 Transaction 2 User.transaction(:repeat able_read) do sleep 3 user

    = User.find(3) user.name = "dingding- #{Time.now.to_i}" user.save end User.transaction(:rep eatable_read) do user = User.find(3) puts “1”+user.name sleep 5 user2 = User.find(3) puts “1”+user.name end
  33. Transaction 1 Transaction 2 User.transaction(:repeat able_read) do sleep 3 user

    = User.find(3) user.name = "dingding- #{Time.now.to_i}" user.save end User.transaction(:rep eatable_read) do user = User.find(3) puts “1”+user.name sleep 5 user2 = User.find(3) puts “1”+user.name end
  34. Transaction 1 Transaction 2 User.transaction(:repeat able_read) do sleep 3 user

    = User.find(3) user.name = "dingding- #{Time.now.to_i}" user.save end User.transaction(:rep eatable_read) do user = User.find(3) puts “1”+user.name sleep 5 user2 = User.find(3) puts “1”+user.name end YES
  35. Transaction 1 Transaction 2 User.transaction(:seriali zable) do sleep 3 user

    = User.find(3) user.name = "dingding- #{Time.now.to_i}" user.save end User.transaction(:seri alizable) do user = User.find(3) puts “1”+user.name sleep 5 user2 = User.find(3) puts “1”+user.name end
  36. Transaction 1 Transaction 2 User.transaction(:seriali zable) do sleep 3 user

    = User.find(3) user.name = "dingding- #{Time.now.to_i}" user.save end User.transaction(:seri alizable) do user = User.find(3) puts “1”+user.name sleep 5 user2 = User.find(3) puts “1”+user.name end
  37. Transaction 1 Transaction 2 User.transaction(:seriali zable) do sleep 3 user

    = User.find(3) user.name = "dingding- #{Time.now.to_i}" user.save end User.transaction(:seri alizable) do user = User.find(3) puts “1”+user.name sleep 5 user2 = User.find(3) puts “1”+user.name end YES