rsim / oracle-enhanced

Oracle enhaced adapter for ActiveRecord
MIT License
545 stars 308 forks source link

prefetch_primary_key? performance issues #2292

Open ioev opened 2 years ago

ioev commented 2 years ago

Expected behavior

prefetch_primary_key? should not impact performance much when saving

Actual behavior

prefetch_primary_key? is calling queries for sequence and primary key on every save, impacting performance

System configuration

Rails version: 7.0.2.2

Oracle enhanced adapter version: 7.0.2

Ruby version: 3.1.0

Oracle Database version: Oracle Database 12c Standard Edition 12.2.0.1.0 - 64bit

We were noticing some performance issues in an application where saving was taking a very long time. I traced this down to the .prefetch_primary_key? method, which calls @connection.describe and eventually calls pk_and_sequence_for on every save, which triggers some very slow (on this particular db) schema related queries.

I'm using a schema cache dump, which populates the primary_key and sequence_name for tables, so I'd assume this method could make use of this as well but it doesn't seem to. For the time being I've overridden this via:

ActiveRecord::Base.class_eval do
  def self.prefetch_primary_key?
    sequence_name.present?
  end
end

which seems to have solved the performance issues, but likely will not handle all cases corectly (ex: autogenerated sequences.)