DB Maintenance

Runs database optimization queries (OPTIMIZE TABLE for MySQL, VACUUM ANALYZE for PostgreSQL) on selected tables during cron runs.

db_maintenance
3,579 sites
45
drupal.org

インストール

Drupal 9, 8 v2.0.0
composer require 'drupal/db_maintenance:^2.0'

概要

DB Maintenanceは、選択したデータベーステーブルに対して最適化クエリを実行することでデータベースメンテナンス操作を行うDrupalモジュールです。MySQLデータベースでは、削除または分割された行を持つテーブルを修復し、テーブルインデックスをソートし、テーブル統計を更新するOPTIMIZE TABLEを実行します。PostgreSQLデータベースでは、削除されたタプルが占有するストレージを回収するためにVACUUM ANALYZEを実行します。

このモジュールは、Cacheテーブル、セッションテーブル、watchdogログなど、データの入れ替わりが激しいテーブルで使用するよう設計されています。Cronベースのスケジューリングをサポートしており、毎回のCron実行から隔月間隔まで頻度オプションを設定できます。さらに、時間間隔制限機能により、管理者は最適化を特定の時間帯に制限でき、低トラフィック時間帯にメンテナンスを実行するのに便利です。

重要な考慮事項:MySQLのOPTIMIZE TABLEコマンドは実行中にテーブルをロックするため、アクセスの多いサイトではサイトパフォーマンスに影響を与える可能性があります。PostgreSQLのVACUUM(FULL以外)操作は通常のデータベース操作と並行して実行できます。大規模なインストールでは、すべてのテーブルを最適化するのではなく、データの入れ替わりが激しいテーブルのみを選択することをお勧めします。

Features

  • 選択したMySQL/MariaDBデータベーステーブルに対してOPTIMIZE TABLEクエリを実行し、削除または分割された行を持つテーブルのデフラグと修復を行う
  • PostgreSQLテーブルに対してVACUUM ANALYZEを実行し、削除されたタプルからストレージを回収して統計を更新する
  • 設定可能なCron実行頻度:毎回のCron実行、毎時、2時間ごと、毎日、2日ごと、毎週、2週間ごと、毎月、隔月
  • 最適化を特定の時間帯(例:夜間の低トラフィック時間帯)に制限する時間間隔制限機能
  • マルチデータベースDrupalインストールでの複数データベース接続のサポート
  • すべてのテーブルを自動的に最適化するか、特定のテーブルを手動で選択するオプション
  • 最適化操作のwatchdogログ記録(オプション)
  • 管理インターフェースのリンクから手動で最適化をトリガー
  • データベーステーブルプレフィックスを正しく処理

Use Cases

Optimizing cache tables on a high-traffic site

On sites with heavy traffic, cache tables frequently have rows inserted and deleted. Configure DB Maintenance to optimize cache_* tables daily during low-traffic hours (e.g., 02:00-04:00) by enabling the time interval feature. This helps maintain database performance without impacting users.

Maintaining watchdog table size

The watchdog table can grow significantly on sites with extensive logging. Select the watchdog table for weekly optimization to reclaim disk space from deleted log entries and maintain query performance when reviewing logs.

Multi-site database maintenance

For Drupal installations with multiple database connections, DB Maintenance can optimize tables across all configured databases. Each database appears with its own table selection list in the configuration form.

Scheduled overnight maintenance

Configure the module to run optimization between 01:30 and 02:30 AM by setting the time interval. Combined with a weekly frequency, this ensures maintenance runs during the lowest traffic period without manual intervention.

PostgreSQL vacuum scheduling

PostgreSQL databases benefit from regular VACUUM operations. While PostgreSQL has autovacuum, DB Maintenance provides Drupal-aware scheduling that can target specific tables and integrate with Drupal's cron system.

Tips

  • Focus on tables with high data turnover (cache_*, sessions, watchdog) rather than optimizing all tables
  • Use the time interval feature to schedule optimization during off-peak hours to minimize impact on users
  • Check the watchdog logs periodically when logging is enabled to verify optimization is running as expected
  • For MySQL, monitor the Overhead column in phpMyAdmin to identify tables that would benefit from optimization
  • The module supports midnight crossover for time intervals (e.g., 23:00 to 01:00)
  • Use the 'Optimize now' link to manually trigger optimization after making significant database changes

Technical Details

Admin Pages 1
DB Maintenance /admin/config/system/db_maintenance

Configure database table optimization settings. Select which tables to optimize, set the optimization frequency, and optionally restrict optimization to specific time intervals.

権限 1
Administer DB Maintenance

Select which tables to optimize during cron jobs. Required to access the module configuration page and trigger manual optimization.

Hooks 1
hook_cron

Performs scheduled database table optimization. Checks if the configured time interval has passed since the last run and if the current time falls within any configured time restriction window before executing optimization.

Troubleshooting 4
Optimization not running at expected times

Verify that Drupal cron is running at the expected frequency. The module can only run when cron executes. Also check if time interval restriction is enabled and verify the current server time falls within the configured window.

Tables showing as not existing error in logs

If you see log messages about tables not existing, the table may have been removed or renamed. Go to the configuration page and update your table selection to remove references to non-existent tables.

Site performance degradation during optimization

MySQL locks tables during OPTIMIZE TABLE operations. Schedule optimization during low-traffic periods using the time interval feature, or reduce the number of tables being optimized at once. Consider optimizing only high-churn tables like cache and sessions.

Configuration not saving table selections

Ensure you have the 'administer db maintenance' permission. Clear Drupal caches if the form is not displaying correctly.

Security Notes 3
  • The 'administer db maintenance' permission should only be granted to trusted administrators as it allows database operations
  • The manual optimization link is protected by CSRF token to prevent unauthorized triggering
  • Database credentials are never exposed through the module interface