# Optimising Drupal database usage

> Stop your Drupal database growing too large by running cron, clearing caches and limiting database logging.

Source: https://helpcenter.ruachost.com/apps/drupal/optimizing-drupal-database-usage/

Drupal’s caching and logging features can cause the database to grow very large over time, which may slow down site performance. On **ruachost.com**, you can optimize database usage by configuring cron jobs, clearing caches, and adjusting logging settings.

## Why Optimize the Database?

- Prevents slow site performance caused by oversized databases.
- Reduces server resource usage.
- Keeps logs and caches manageable.
- Improves scalability for high‑traffic sites.

## Steps to Optimize Drupal Database Usage

### 1. Configure Drupal’s Cron Routine

- Cron performs routine maintenance tasks such as updating modules and cleaning logs.
- Go to **Configuration → System → Cron**.
- Set a schedule (e.g., every 6 hours).
- Ensure cron is running regularly to prevent database bloat.

### 2. Clear Drupal’s Caches

Drupal maintains multiple caches that can grow large over time. You can clear them manually or automatically:

- ### Manual Clearing

  - Go to **Configuration → Development → Performance → Clear all caches**.
  - Or run the command:

    ```bash
    drush cache-clear all
    ```
- ### Automatic Clearing

  - Set up a cron job to clear caches periodically.
  - Example (weekly at 2:30 AM Sunday):

    ```bash
    30 02 * * Sun cd ${HOME}/public_html; drush cache-clear all >/dev/null 2>&1
    ```

### 3. Configure Database Logging (dblog)

The **dblog** module saves system events in the database, which can grow quickly if not managed.

- Go to **Configuration → Development → Logging and errors**.
- Under **Database log messages to keep**, set a reasonable limit (e.g., 100 rows).
- This reduces database size and improves performance.

### 4. Monitor Database Size

- Use **phpMyAdmin** or Drush to check table sizes.
- Regularly optimize tables using:

  ```bash
  mysqlcheck -o database_name
  ```

## Important Notes

- Always back up your database before making changes.
- Cron jobs for cache clearing are separate from Drupal’s built‑in cron routine.
- Database logging should be limited but not disabled entirely, as logs are useful for troubleshooting.
