Class: ActiveRecord::PostgreSQLAnalyzer::LogSubscriber
- Inherits:
-
LogSubscriber
- Object
- LogSubscriber
- ActiveRecord::PostgreSQLAnalyzer::LogSubscriber
- Defined in:
- lib/active_record/postgresql_analyzer.rb
Constant Summary collapse
- IGNORED_PAYLOADS =
%w(SCHEMA EXPLAIN CACHE)
- EXPLAINED_SQLS =
/\A\s*(select|update|delete|insert)\b/i- SEQ_SCAN =
/.*Seq Scan.*/
Instance Method Summary collapse
Instance Method Details
#ignore_payload?(payload) ⇒ Boolean
32 33 34 |
# File 'lib/active_record/postgresql_analyzer.rb', line 32 def ignore_payload?(payload) payload[:exception] || IGNORED_PAYLOADS.include?(payload[:name]) || payload[:sql] !~ EXPLAINED_SQLS end |
#seq_scan?(explain_result) ⇒ Boolean
36 37 38 |
# File 'lib/active_record/postgresql_analyzer.rb', line 36 def seq_scan?(explain_result) explain_result =~ SEQ_SCAN end |
#sql(event) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/active_record/postgresql_analyzer.rb', line 13 def sql(event) payload = event.payload return if ignore_payload?(payload) connection = ObjectSpace._id2ref(payload[:connection_id]) # disable SeqScan when index exists # SEE ALSO: http://www.postgresql.org/docs/9.4/static/indexes-examine.html connection.execute("SET enable_seqscan TO off", "SCHEMA") explain_result = connection.explain(payload[:sql], payload[:binds]) if seq_scan?(explain_result) debug '------------ find Seq Scan query ------------' debug payload[:sql] debug explain_result end end |