From 06415b17e0bb0bd1dc4a59cbcce831dc2c960ecc Mon Sep 17 00:00:00 2001 From: Karel Picman Date: Thu, 20 Apr 2017 14:43:59 +0200 Subject: [PATCH] #699 SQL optimization --- app/models/dmsf_folder.rb | 35 +++++++++++++++-------------------- app/views/dmsf/_file.html.erb | 2 +- 2 files changed, 16 insertions(+), 21 deletions(-) diff --git a/app/models/dmsf_folder.rb b/app/models/dmsf_folder.rb index 713cfd26..8db10488 100644 --- a/app/models/dmsf_folder.rb +++ b/app/models/dmsf_folder.rb @@ -311,31 +311,26 @@ class DmsfFolder < ActiveRecord::Base def modified last_update = updated_at - dmsf_folders.each do |subfolder| - last_update = subfolder.updated_at if subfolder.updated_at > last_update - end - dmsf_files.each do |file| - last_update = file.updated_at if file.updated_at > last_update - end - folder_links.each do |folder_link| - last_update = folder_link.updated_at if folder_link.updated_at > last_update - end - file_links.each do |file_link| - last_update = file_link.updated_at if file_link.updated_at > last_update - end - url_links.each do |url_link| - last_update = url_link.updated_at if url_link.updated_at > last_update - end + time = DmsfFolder.where( + ['project_id = ? AND dmsf_folder_id = ? AND updated_at > ?', + self.project_id, self.id, last_update]).maximum(:updated_at) + last_update = time if time + time = DmsfFile.where( + ['container_id = ? AND container_type = ? AND dmsf_folder_id = ? AND updated_at > ?', + self.project_id, 'Project', self.id, last_update]).maximum(:updated_at) + last_update = time if time + time = DmsfLink.where( + ['project_id = ? AND dmsf_folder_id = ? AND updated_at > ?', + self.project_id, self.id, last_update]).maximum(:updated_at) + last_update = time if time last_update end # Number of items in the folder def items - dmsf_folders.visible.count + - dmsf_files.visible.count + - folder_links.visible.count + - file_links.visible.count + - url_links.visible.count + dmsf_folders.where(:project_id => self.project_id).visible.count + + dmsf_files.visible.where(:container_id => self.project_id).count + + dmsf_links.visible.where(:project_id => self.project_id).count end def self.is_column_on?(column) diff --git a/app/views/dmsf/_file.html.erb b/app/views/dmsf/_file.html.erb index 2c9bc730..f9601572 100644 --- a/app/views/dmsf/_file.html.erb +++ b/app/views/dmsf/_file.html.erb @@ -20,7 +20,7 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. %> -<% wf = DmsfWorkflow.find_by_id(file.last_revision.dmsf_workflow_id) %> +<% wf = DmsfWorkflow.find_by_id(file.last_revision.dmsf_workflow_id) if file.last_revision.dmsf_workflow_id %> <%= check_box_tag(name, id, false, :title => l(:title_check_for_zip_download_or_email), :id => "file_#{id}") %>