By the end of this chapter, you will be able to:
Mastering these skills helps keep databases running smoothly and efficiently, which is essential for any organization relying on reliable data.
Compiled by: Beatrice Adhiambo
Database maintenance is the regular work that keeps a database available, correct, fast and recoverable after it has gone into use. A database does not stay healthy by itself. Data grows, indexes become uneven, logs fill the disk, backups fail without anyone noticing, and software needs patches. A maintenance schedule turns this work into a written timetable, so that each task is done on time by a named person and nothing depends on someone remembering.
Maintenance work falls into three kinds. Preventive maintenance stops problems before they happen, for example by taking backups and checking disk space. Corrective maintenance repairs a fault that has already happened, such as restoring a damaged table. Adaptive maintenance changes the database to suit new conditions, such as a new version of the DBMS or a new business requirement. A schedule is mostly made of preventive tasks, which is why it saves effort in the long run.
Example: a hardware wholesaler keeps its stock and sales in one MySQL database that is used from seven in the morning to seven in the evening. Its administrator cannot run heavy maintenance during those hours, so the schedule places each task at a time when it will disturb nobody, and records what was done.
A maintenance plan is the written document that lists the maintenance tasks for a database, how often each is done, who does it, which tool is used and how success is checked. It is the foundation of the schedule: the plan says what must be done, and the schedule says when.
| Part | What it states |
|---|---|
| Scope | The databases and servers covered |
| Tasks | Each task named clearly, for example full backup or update statistics |
| Frequency | Daily, weekly, monthly, quarterly or annual |
| Time window | The agreed time when users are not affected |
| Owner | The person responsible and a deputy |
| Tool or script | The command, script or scheduler that does the task |
| Success check | How the owner proves that the task worked, for example a backup file of the expected size |
| Failure action | What to do and whom to tell if the task fails |
Daily tasks are the short checks and routine jobs that make sure the database is working and protected from one day to the next. They take little time but catch most serious problems early. Many of them are automated, and the administrator's part is to read the result each morning.
# Linux cron: run a backup of the hardware database every night at 01:30.
# The backup account's password is kept in /etc/mysql/backup.cnf, not in this line.
30 1 * * * /usr/bin/mysqldump --defaults-extra-file=/etc/mysql/backup.cnf --single-transaction --routines --events hardware > /backups/hardware_$(date +\%F).sql
-- MySQL 8: how long the server has been running, in seconds
SHOW GLOBAL STATUS LIKE 'Uptime';
# Check free disk space and read the last 50 lines of the MySQL error log (Ubuntu)
df -h
sudo tail -n 50 /var/log/mysql/error.log
Weekly tasks are the jobs that need more time or more attention than a daily check, and that do not change fast enough to be done every day. They keep the database's statistics and backups trustworthy.
-- MySQL 8: refresh optimiser statistics for the busiest table
ANALYZE TABLE stock_movement;
-- List the ten largest tables of the hardware database in megabytes
SELECT table_name,
ROUND((data_length + index_length) / 1024 / 1024, 1) AS size_mb
FROM information_schema.tables
WHERE table_schema = 'hardware'
ORDER BY (data_length + index_length) DESC
LIMIT 10;
# Restore last night's backup into a test database to prove it works
mysql -u root -p -e "CREATE DATABASE IF NOT EXISTS hardware_test"
mysql -u root -p hardware_test < /backups/hardware_2026-11-05.sql
Monthly tasks deal with slower changes: fragmentation, growth, access and patches. They are larger jobs and are best done in the quietest period of the month.
-- MySQL 8: check a table for errors, then rebuild it and refresh its statistics
CHECK TABLE stock_movement;
OPTIMIZE TABLE stock_movement;
-- MySQL 8 sys schema: indexes that have not been used since the server started
SELECT * FROM sys.schema_unused_indexes WHERE object_schema = 'hardware';
OPTIMIZE TABLE on an InnoDB table rebuilds the table, so it is run in the maintenance window. The list of unused indexes is trustworthy only after the server has been running through a full business cycle.
Quarterly tasks, done every three months, test whether the database could survive a serious failure and whether the plan itself is still suitable.
-- MySQL 8: list the accounts and then the rights of one of them
SELECT user, host FROM mysql.user ORDER BY user;
SHOW GRANTS FOR 'shop_app'@'localhost';
Annual tasks look at the year ahead: whether the software and hardware will still be supported, whether the policies are right and whether the budget is enough.
Preparing the schedule means turning the maintenance plan into a calendar that fits the way the business works. A schedule that is too heavy is ignored, and one that clashes with business hours harms users. The administrator builds it from the tasks listed for each period, places them in suitable time windows and records who is responsible.
An extract from a schedule for the hardware wholesaler is shown below. The times are examples chosen to fall outside business hours.
| Task | Frequency | When | Owner | Tool | Evidence of success |
|---|---|---|---|---|---|
| Full database backup | Daily | 01:30 | Administrator | cron and mysqldump | Backup file exists and its size is checked |
| Update statistics | Weekly | Sunday 02:00 | Administrator | MySQL client script | Command log shows no error |
| Restore test | Weekly | Sunday 03:00 | Deputy administrator | Test server | Row counts of the main tables agree with the figures recorded when the backup was taken |
| Rebuild busy tables | Monthly | First Sunday 02:00 | Administrator | MySQL client script | Table size recorded before and after |
| Recovery drill | Quarterly | Saturday morning | Administrator and deputy | Clean server | Recovery time written in the drill report |
| Version and support review | Annual | January | Senior developer | Vendor notices | Upgrade plan approved |
Every maintenance schedule needs the same key elements. If one is missing, tasks are forgotten, clash with business hours or cannot be repeated.
Create a free account to open more of this chapter.
Free: practical guides, quick cards, workplace scenarios and more.
Create a free accountType: Individual
| Tools & Equipment | Materials |
|---|---|
| Relational Database Management System (MySQL 8.0) | |
| Database Client Software (MySQL Workbench) | |
| Laptop or Desktop Computer |
| S/N | Item | Quantity |
|---|---|---|
| 1 | Relational Database Management System (MySQL 8.0) | 1 instance per Candidate |
| 2 | Database Client Software (MySQL Workbench) | 1 instance per Candidate |
| 3 | Laptop or Desktop Computer | 1 per Candidate |
| Items to be Evaluated | Marks Available | Marks Obtained | Comments |
|---|---|---|---|
| TASK 1: Database Optimization Process | |||
| Verified the existing database structure and identified performance bottlenecks (Award 1 mark for identifying slow queries, 1 mark for analyzing indexes, 1 mark for recognizing missing keys) | 3 | ||
| Created appropriate indexes on frequently queried columns (Award 1 mark each for creating at least 5 effective indexes) | 5 | ||
| Updated database statistics to aid query optimizer (Award 1 mark for running ANALYZE TABLE or equivalent, 1 mark for verifying statistics update) | 2 | ||
| Optimized slow queries using EXPLAIN plans and rewrote queries where necessary (Award 1 mark for using EXPLAIN, 1 mark for rewriting query, 1 mark for testing improved query, 2 marks for demonstrating improved performance) | 5 | ||
| Backed up the optimized database schema and data (Award 1 mark for performing backup, 1 mark for verifying backup integrity) | 2 | ||
| Sub-Total | 17 | ||
| PRODUCT CHECKLIST | |||
| Database performance improved as evidenced by reduced query execution times and properly implemented indexes (Award 2 marks for each of the four key improvements: indexes created, statistics updated, queries optimized, backup done) | 8 | ||
| Sub-Total | 8 | ||
| GRAND TOTAL | 25 | ||
Type: Individual
| Tools & Equipment | Materials |
|---|---|
| Relational Database Management System (MySQL Server 8.0 or higher) | Sample Student Performance Database Script |
| SQL Query Editor (MySQL Workbench or equivalent) |
| S/N | Item | Quantity |
|---|---|---|
| 1 | Relational Database Management System (MySQL Server 8.0 or higher) | 1 Instance per Candidate |
| 2 | SQL Query Editor (MySQL Workbench or equivalent) | 1 Instance per Candidate |
| 3 | Sample Student Performance Database Script | 1 Copy per Candidate |
| Items to be Evaluated | Marks Available | Marks Obtained | Comments |
|---|---|---|---|
| TASK 1: Apply Indexes for Query Optimization | |||
| Identified slow-running queries using EXPLAIN plan (Award 2 marks for accurate use of EXPLAIN to identify bottlenecks) | 2 | ||
| Created appropriate indexes on columns used in WHERE and JOIN clauses (Award 1 mark each for creating indexes on at least four relevant columns) | 4 | ||
| Tested queries after indexing to confirm performance improvement (Award 2 marks for demonstrating reduced query execution time) | 2 | ||
| Sub-Total | 8 | ||
| TASK 2: Rewrite Queries for Efficient Execution | |||
| Rewrote inefficient queries using JOINs instead of subqueries (Award 1 mark for each query rewritten correctly) | 3 | ||
| Used WHERE clauses effectively to filter data early (Award 2 marks for correct use of WHERE filtering) | 2 | ||
| Avoided SELECT * and specified necessary columns only (Award 2 marks for optimized SELECT statements) | 2 | ||
| Sub-Total | 7 | ||
| TASK 3: Create and Use Views for Simplified Querying | |||
| Created a view named 'student_performance_summary' joining relevant tables (Award 3 marks for correct CREATE VIEW syntax and logic) | 3 | ||
| Queried the view to retrieve performance data for a specific student (Award 2 marks for correct SELECT query using the view) | 2 | ||
| Sub-Total | 5 | ||
| PRODUCT CHECKLIST | |||
| Indexes created on specified columns with correct syntax and effective in improving query speed (Award 5 marks for correct and effective indexing) | 5 | ||
| Optimized queries execute correctly and return expected results with improved performance (Award 5 marks for correctness and improved efficiency) | 5 | ||
| View 'student_performance_summary' correctly created and returns accurate consolidated data (Award 5 marks for correct view creation and usage) | 5 | ||
| Sub-Total | 15 | ||
| GRAND TOTAL | 35 | ||
At the start of this chapter we promised you would be able to:
Tick each one you can genuinely do.
So, are you there yet?
You're competent when you can confidently do 50% or more of what this chapter promised.
Sign in to record how you're doing.