From ae2d80501a36bd4abaa81d6a1840c9bcc4d9b37c Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 7 Aug 2014 11:07:30 +1000 Subject: [PATCH] PERF: run full vacuum every 90 days (during migration) Will reclaim space and improve perf --- config/locales/server.en.yml | 2 ++ config/site_settings.yml | 5 +++++ lib/tasks/db.rake | 16 ++++++++++++++++ 3 files changed, 23 insertions(+) diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 85341ac14e1..c0f558132ec 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -971,6 +971,8 @@ en: enable_cdn_js_debugging: "Allow /logs to display proper errors by adding crossorigin permissions on all js includes." show_create_topics_notice: "If the site has fewer than 5 public topics, show a notice asking admins to create some topics." + vacuum_db_days: "Run VACUUM FULL ANALYZE to reclaim DB space after migrations (set to 0 to disable)" + errors: invalid_email: "Invalid email address." invalid_username: "There's no user with that username." diff --git a/config/site_settings.yml b/config/site_settings.yml index aa66a28deef..67cd795be2b 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -709,6 +709,11 @@ uncategorized: company_short_name: 'Unconfigured Forum' company_domain: 'www.example.com' + vacuum_db_days: 90 + last_vacuum: + default: 0 + hidden: true + tos_topic_id: default: -1 hidden: true diff --git a/lib/tasks/db.rake b/lib/tasks/db.rake index 82f2b239284..bc5cc8206ea 100644 --- a/lib/tasks/db.rake +++ b/lib/tasks/db.rake @@ -2,6 +2,22 @@ task 'db:migrate' => 'environment' do I18n.locale = SiteSetting.default_locale rescue :en SeedFu.seed + + if SiteSetting.vacuum_db_days > 0 && + SiteSetting.last_vacuum < (Time.now.to_i - SiteSetting.vacuum_db_days.days.to_i) + puts "Running VACUUM FULL ANALYZE to reclaim DB space, this may take a while" + puts "Set to run every #{SiteSetting.vacuum_db_days} days (search for vacuum in site settings)" + puts "#{Time.now} starting..." + begin + + Topic.exec_sql("VACUUM FULL ANALYZE") + rescue => e + puts "VACUUM failed, skipping" + puts e.to_s + end + SiteSetting.last_vacuum = Time.now.to_i + puts "#{Time.now} VACUUM done" + end end task 'test:prepare' => 'environment' do