ActiveRecord::PostgreSQLAnalyzer gem を作った話 #m3dev

ActiveRecord::PostgreSQLAnalyzer gem を作った話 #m3dev

M3 Tech Talk で「ActiveRecord::PostgreSQLAnalyzer gem を作った話」というタイトルで LT したときの資料です。
#m3dev

509e5167fdb3871d6b6e045e34a3e019?s=128

Takayuki Matsubara

December 22, 2015
Tweet

Transcript

  1. ActiveRecord::Postgr eSQLAnalyzer gem Λ࡞ͬͨ࿩ !

  2. ࣗݾ঺հ

  3. TODO: !

  4. ⭐ github.com/ma2gedev/ power_assert_ex

  5. ActiveRecord::PostgreSQLAnalyzer https://github.com/m3dev/ active_record- postgresql_analyzer

  6. ͖͔͚ͬ

  7. m3 advent calendar

  8. SAStruts + S2JDBC ͷࢥ͍ग़ʹ͍ͭͯ ଘ෼ʹޠΔ #m3dev — @seratch http://qiita.com/ seratch@github/items/

    826f1b4a89993d3b0804
  9. Sequential Scan ݕ ஌

  10. ศརͦ͏ ͪͳΈʹ͜ͷΞΠσΞࣗମ͸ S2JDBC ʹݶఆ͞Ε·ͤΜɻࠓ೥΍ͬͨϓϩδ ΣΫτͰ͸ Scala ͷΞϓϦέʔγϣϯ Ͱ΋ಉ͡Α͏ͳίʔυΛॻ͍ͨͷͰ͢ ͕ɺඇৗʹ༗༻Ͱͨ͠ɻ —

    @seratch
  11. Ruby(RoR)͸ʁ

  12. ͳ͚Ε͹࡞Ζ ͏ OSS

  13. ActiveRecord::PostgreSQLAnalyzer https://github.com/m3dev/ active_record- postgresql_analyzer

  14. Rails ͰγʔέϯγϟϧεΩ ϟϯ͍ͯ͠ΔΫΤϦΛݟ ͚ͭͯḿΓ͍ͨ - PostgreSQL ݶ ఆ http://qiita.com/ma2ge/ items/0df91b0fbd90808f40d5

  15. όζͬ ͨʂʁ

  16. όζͬͯͳ ͍

  17. ͩ͞·͞͠ऑऀ͗ͯ͢ࠔͬͨͷͰ Scala ͰίϯύΠϧ͢Δ͜ͱʹͨ͠࿩ — @seratch http://qiita.com/ seratch@github/items/ 3be26b584f3d74aa1150

  18. ࢖͍ํ

  19. Gemfile gem 'active_record-postgresql_analyzer', group: :development

  20. Output logfile if sequential scan is detected ------------ find Seq

    Scan query ------------ SELECT "todo".* FROM "todo" WHERE "todo"."user_id" = $1 LIMIT 1 QUERY PLAN --------------------------------------------------------------------------------------------- Limit (cost=10000000000.00..10000000001.67 rows=1 width=81) -> Seq Scan on todo (cost=10000000000.00..10000000001.67 rows=1 width=81) Filter: (user_id = 13) (3 rows)
  21. ࢓૊Έ

  22. ActiveSupport::Notifications ActiveRecord::PostgreSQLAnalyzer::LogSubscriber.attach_to :active_record https://github.com/m3dev/ active_record-postgresql_analyzer/ blob/master/lib/active_record/ postgresql_analyzer.rb#L42

  23. Explain ActiveRecord::Base.connection.explain(payload[:sql], payload[:binds]) https://github.com/m3dev/ active_record-postgresql_analyzer/ blob/master/lib/active_record/ postgresql_analyzer.rb#L22

  24. Detect Sequential Scan SELECT "todo".* FROM "todo" WHERE "todo"."user_id" =

    $1 LIMIT 1 QUERY PLAN --------------------------------------------------------------------------------------------- Limit (cost=10000000000.00..10000000001.67 rows=1 width=81) -> Seq Scan on todo (cost=10000000000.00..10000000001.67 rows=1 width=81) Filter: (user_id = 13) (3 rows) https://github.com/m3dev/ active_record-postgresql_analyzer/ blob/master/lib/active_record/ postgresql_analyzer.rb#L23
  25. index షΓ๨Ε๷ ࢭ

  26. ActiveRecord::PostgreSQLAnalyzer https://github.com/m3dev/ active_record- postgresql_analyzer

  27. ⭐ github.com/ma2gedev/ power_assert_ex

  28. end