Metal
Metal

Reputation: 205

Performance tuning for rails active record querying

I have rails app where I have multiple models. Such as model Input with following associations :-

belongs_to :department, :class_name => 'Department'

has_many :frame_inputs, class_name: 'Frames::FrameInput', foreign_key: 'input_id', :dependent => :destroy
has_many :frames, :through => :frame_inputs, :class_name => 'Frames::Base'

has_many :scales, :foreign_key => :input_id, :dependent => :destroy
has_many :input_items, :class_name => 'InputItems::Base', :foreign_key => :department_id, :dependent => :destroy
has_many :linenumbers, :class_name => 'Frames::InputItemPriority', :through => :input_items

Challenge:- Whenever trying to delete an Input record from the application, the backend Active record query takes lot of time, i.e. 10 - 20 seconds for completing one single transaction.

Question:- As, it has to destroy all the record dependencies, can I reduce the transaction time for executing the delete/destroy query ?

For destroying the records I have following method in my Inputs controller

def destroy
 @input.destroy
end

Please guide me on how to optimize the destroy query to reduce the transaction time.

Thanks

Upvotes: 1

Views: 506

Answers (1)

tirdadc
tirdadc

Reputation: 4713

:destroy will actually instantiate the associated records and also trigger callbacks, so it's a lot more costly than using :delete_all.

If you don't have any callbacks you need to re-run,and just need the associated records to be deleted, use the latter.

See here: Rails :dependent => :destroy VS :dependent => :delete_all

Upvotes: 1

Related Questions