M3 Tech Talk で「ActiveRecord::PostgreSQLAnalyzer gem を作った話」というタイトルで LT したときの資料です。 #m3dev
ActiveRecord::PostgreSQLAnalyzergem Λ࡞ͬͨ !
View Slide
ࣗݾհ
TODO: !
⭐github.com/ma2gedev/power_assert_ex
ActiveRecord::PostgreSQLAnalyzerhttps://github.com/m3dev/active_record-postgresql_analyzer
͖͔͚ͬ
m3 advent calendar
SAStruts + S2JDBC ͷࢥ͍ग़ʹ͍ͭͯଘʹޠΔ #m3dev— @seratchhttp://qiita.com/[email protected]/items/826f1b4a89993d3b0804
Sequential Scan ݕ
ศརͦ͏ͪͳΈʹ͜ͷΞΠσΞࣗମ S2JDBCʹݶఆ͞Ε·ͤΜɻࠓͬͨϓϩδΣΫτͰ Scala ͷΞϓϦέʔγϣϯͰಉ͡Α͏ͳίʔυΛॻ͍ͨͷͰ͕͢ɺඇৗʹ༗༻Ͱͨ͠ɻ— @seratch
Ruby(RoR)ʁ
ͳ͚Ε࡞Ζ͏ OSS
Rails ͰγʔέϯγϟϧεΩϟϯ͍ͯ͠ΔΫΤϦΛݟ͚ͭͯḿΓ͍ͨ - PostgreSQL ݶఆhttp://qiita.com/ma2ge/items/0df91b0fbd90808f40d5
όζͬͨʂʁ
όζͬͯͳ͍
ͩ͞·͞͠ऑऀ͗ͯ͢ࠔͬͨͷͰScala ͰίϯύΠϧ͢Δ͜ͱʹͨ͠— @seratchhttp://qiita.com/[email protected]/items/3be26b584f3d74aa1150
͍ํ
Gemfilegem 'active_record-postgresql_analyzer', group: :development
Output logfile if sequential scan is detected------------ find Seq Scan query ------------SELECT "todo".* FROM "todo" WHERE "todo"."user_id" = $1 LIMIT 1QUERY 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)
Έ
ActiveSupport::NotificationsActiveRecord::PostgreSQLAnalyzer::LogSubscriber.attach_to :active_recordhttps://github.com/m3dev/active_record-postgresql_analyzer/blob/master/lib/active_record/postgresql_analyzer.rb#L42
ExplainActiveRecord::Base.connection.explain(payload[:sql], payload[:binds])https://github.com/m3dev/active_record-postgresql_analyzer/blob/master/lib/active_record/postgresql_analyzer.rb#L22
Detect Sequential ScanSELECT "todo".* FROM "todo" WHERE "todo"."user_id" = $1 LIMIT 1QUERY 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
index షΓΕࢭ
end