I am fetching around more than 300000 items through a query:
@items = current_company.item_masters.user_hierarchies_filter(current_user).accessible_by(current_ability).paginate(:conditions => condition.compile, :include=> includable_tables,
:order => (sort_column + " " + sort_direction),
:page=> new_params[:page],:per_page => new_params[:per_page_records].blank? ? 10 : new_params[:per_page_records])
And the scope used in that query is :
named_scope :user_hierarchies_filter, lambda { |user|
{select: "DISTINCT item_masters.id", conditions: ["item_masters.company_id = ?", user.company_id]}
}
It produces this log message:
ItemMaster Load (43445.6ms) SELECT DISTINCT item_masters.id FROM `item_masters` WHERE (item_masters.status IN ('Outstanding')) AND (((item_masters.company_id = 1) AND (`item_masters`.company_id = 1))) ORDER BY item_number asc LIMIT 331230, 10
It takes around 44 seconds to load items. Can anyone please suggest any option to reduce the loading time of this query?