Introduction
In this lab, you will learn how to maintain your SQLite database for optimal performance. We'll cover essential techniques like using the VACUUM command to reclaim space, rebuilding indexes to speed up queries, and analyzing table statistics to help SQLite make better decisions. Let's get started!
Create a Database and Sample Table
Before we dive into maintenance, let's create a SQLite database and a sample table with some data. This will give us something to work with.
First, open your terminal in the LabEx VM. Your default path is /home/labex/project.
To create a SQLite database named mydb.db, run the following command:
sqlite3 mydb.db
This command will both create the database file (if it doesn't exist) and open the SQLite command-line tool, connecting you to the database. You'll see the sqlite> prompt.
Now, let's create a table named users to store user information. Execute the following SQL command:
CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, email TEXT);
This command creates a table with three columns: id (an integer that uniquely identifies each user), name (the user's name), and email (the user's email address). The PRIMARY KEY constraint ensures that each id is unique.
Next, let's insert some sample data into the users table:
INSERT INTO users (name, email) VALUES ('Alice', 'alice@example.com');
INSERT INTO users (name, email) VALUES ('Bob', 'bob@example.com');
INSERT INTO users (name, email) VALUES ('Charlie', 'charlie@example.com');
These commands add three rows to the users table.
To verify that the table and data have been created successfully, run the following command:
SELECT * FROM users;
Expected Output:
1|Alice|alice@example.com
2|Bob|bob@example.com
3|Charlie|charlie@example.com
This output shows the contents of the users table.
Simulate Data Deletion
To demonstrate the effect of VACUUM, we need to simulate data deletion, which can lead to fragmentation in the database file.
Let's delete a row from the users table:
DELETE FROM users WHERE id = 2;
This command removes the row where the id is 2 (Bob's record).
To confirm the deletion, run the following command:
SELECT * FROM users;
Expected Output:
1|Alice|alice@example.com
3|Charlie|charlie@example.com
You'll notice that Bob's record is no longer in the table. This deletion leaves a gap in the database file, which VACUUM can help to address.
Run VACUUM to Reclaim Space
Now, let's use the VACUUM command to defragment the database file and reclaim the space left by the deleted record.
Execute the following command within the SQLite shell:
VACUUM;
This command rewrites the entire database file, consolidating the data and removing the empty space.
You won't see any specific output after running VACUUM, but it's working behind the scenes to optimize your database.
To exit the SQLite shell, run:
.exit
You are now back in your terminal.
Create an Index
Indexes are crucial for speeding up queries. Let's create an index on the email column of the users table.
First, connect to the SQLite database again:
sqlite3 mydb.db
Now, create the index using the following command:
CREATE INDEX idx_users_email ON users (email);
This command creates an index named idx_users_email on the email column. SQLite will use this index to quickly find users based on their email address.
Rebuild the Index
Over time, indexes can become fragmented, especially after many data modifications. Rebuilding the index can improve its efficiency.
To rebuild the index we just created, use the REINDEX command:
REINDEX idx_users_email;
This command rebuilds the idx_users_email index, ensuring it's optimized for the current data.
To exit the SQLite shell, run:
.exit
You are now back in your terminal.
Analyze Table Statistics
SQLite uses statistics about your data to optimize queries. It's a good practice to update these statistics periodically.
First, connect to the SQLite database again:
sqlite3 mydb.db
To analyze the users table, run the following command:
ANALYZE users;
This command collects statistics about the users table, which the query optimizer can use to improve query performance.
Finally, exit the SQLite shell:
.exit
You are now back in your terminal.
Summary
In this lab, you have learned how to perform essential maintenance tasks on your SQLite database. You used the VACUUM command to reclaim space, created and rebuilt an index to improve query performance, and analyzed table statistics to help SQLite optimize queries. These techniques are crucial for keeping your database running smoothly and efficiently.


