WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

MySQL · Basic · question 15 of 100

What are MySQL indexes, and why are they important for query performance?

📕 Buy this interview preparation book: 100 MySQL questions & answers — PDF + EPUB for $5

In MySQL, an index is a data structure that helps to retrieve data from a database table more efficiently. It is analogous to an index at the back of a book, which allows readers to quickly locate information without scanning the entire book. Like the book index, the database index achieves speed by organizing data based on a specific key.

MySQL indexes are used to speed up the data retrieval process for SELECT, JOIN, and WHERE clauses in SQL queries. By creating an index on one or more columns, MySQL can locate data more quickly, reducing the amount of time necessary to retrieve data from the database. For example, if we have a database of 1,000,000 rows and we want to retrieve all revenue data for the year 2019, if an index has been created on the revenue and date columns, the query will retrieve the data faster than if there was no index on those columns.

Indexes in MySQL can be created using the CREATE INDEX statement. The following example creates an index named ‘idx_revenue_date‘ on the ‘revenue‘ and ‘date‘ columns of a table named ‘sales‘.

CREATE INDEX idx_revenue_date ON sales(revenue, date);

There are different types of MySQL indexes, including:

- B-tree index: This is the default index type in MySQL and is suitable for most queries that use the equals (=) operator in the WHERE clause.

- Hash index: This is suitable for queries that use the equals (=) or IN operator in the WHERE clause.

- Full-text index: This is suitable for text search queries that use the MATCH() AGAINST() syntax in the WHERE clause.

MySQL indexes are essential for query performance because without them, the database would have to scan every row sequentially to find the required data, which can be very slow and expensive in terms of resources. Indexes help to reduce the amount of data MySQL needs to scan to find the data required by the query, thereby significantly improving the performance of the query.

However, it’s important to note that indexes come with a trade-off. While they can significantly speed up SELECT queries, they can also slow down INSERT, UPDATE, and DELETE queries because the indexes have to be updated every time a change is made to the table. In addition, indexes can take up a significant amount of disk space, which can impact the overall performance of the database. Therefore, it’s important to carefully consider which columns to index and when to create indexes based on the types and frequency of queries executed in the system.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic MySQL interview — then scores it.
📞 Practice MySQL — free 15 min
📕 Buy this interview preparation book: 100 MySQL questions & answers — PDF + EPUB for $5

All 100 MySQL questions · All topics