...

/

Indexes, Views, Sequences, Stored Procedures

Indexes, Views, Sequences, Stored Procedures

Learn about MySQL indexes, views, AUTO_INCREMENT, and stored procedures to optimize performance and simplify data handling.

Imagine our OnlineStore database is growing rapidly. We’re adding thousands of new products and customers every day, and our sales team is constantly running reports. Lately, they’ve been complaining that fetching simple sales data takes ages. Moreover, they often need to run the same complex queries repeatedly, and sometimes, they only need to see a subset of data for security reasons. How can we make their lives easier and our database more efficient and secure? This is where powerful database objects like indexes, views, sequences, and stored procedures come into play!

In this lesson, we’ll explore these essential database objects. We’ll learn how:

  • Indexes can dramatically speed up data retrieval.

  • Views can simplify complex queries and enhance data security.

  • MySQL’s AUTO_INCREMENT feature provides a straightforward way to generate unique sequence numbers for our records.

  • Stored procedures can encapsulate and reuse common database operations, improving efficiency and maintainability.

By the end of this lesson, we’ll be able to implement these objects in our OnlineStore database to optimize performance, simplify data access, and streamline our database operations.

Indexes: Speeding up your queries

Imagine trying to find a specific topic in a large textbook without an index at the back. We’d have to scan through every page, which would be incredibly time-consuming! An index in a database works much like the index in a book. When we query data from a table, especially a large one, the database might have to scan through many rows to find the ones that match our criteria. This is called a full table scan, and it can be very slow.

Indexes are crucial for performance. They allow the database to locate specific rows much faster, significantly reducing query execution time, especially for SELECT statements with WHERE clauses or JOIN operations.

An index is a special lookup table that the database search engine can use to speed up data retrieval. Simply put, an index is a pointer to data in a table. An index in a database is very similar to an index in the back of a book. It’s a data structure (commonly a B-tree) that stores the values of a specific column or set of columns in a table, along with a reference (a pointer) to the actual table row where that value is found.

When we create an index on a column, the database creates and maintains this separate structure. When we run a query that filters on that indexed column, the database can use the index to quickly find the matching rows, rather than scanning the entire table.

For example, in our OnlineStore database, if we frequently search for products by ProductName, creating an index on the ProductName column in the Products table would make these searches much faster.

Creating and using indexes

We can create an index using the CREATE INDEX statement. The basic syntax is:

CREATE INDEX index_name
ON table_name (column1, column2, ...);

Sample scenario: Speeding up product searches

Let’s say users of our OnlineStore frequently search for products by their name. Without an index on the ProductName column, the database would have to scan the entire Products table for every search.

Example: Creating an Index on ProductName

We can create an index on the ProductName column in our Products table to speed this up.

-- Create an index on the ProductName column of the Products table
CREATE INDEX idx_product_name
ON Products (ProductName);

Now, when we query the Products table using a WHERE clause on ProductName, MySQL can use idx_product_name to quickly locate the relevant products.

Press + to interact
MySQL 8.0
-- This query will now be faster thanks to the index
SELECT ProductID, ProductName, Price, Stock
FROM Products
WHERE ProductName = 'Laptop';

When to create indexes: ...