The purpose of this post is to describe what covering indexes are and how they can be used to improve the performance of queries. People mostly use indexes to filter or sort the results but not much thought is given to actually reduce the disk reads by using proper indexes. So I will show you how to reduce disk reads and hence improve the performance of queries by utilizing indexes properly.
There’s a really important difference between a unique index (MySQL’s answer to a “unique constraint”) and a primary key in MySQL. Please take a look at this:
CREATE TABLE `t` ( `a` int, `b` int, `c` int, UNIQUE KEY `a` (`a`,`b`) )
The combination of columns
a, b should uniquely
identify any tuple in the table, right?
select * from t; +------+------+------+ | a | b | c | +------+------+------+ | 1 | 2 | 3 | | NULL | NULL | 1 | | NULL | NULL | 1 | | NULL | NULL | 1 | +------+------+------+
Wrong. Our arch-enemy
NULL messes things up again:
A UNIQUE index creates a constraint such that all values in the index must be distinct. An error occurs if you try to add a new row with a key value that matches an existing row. This constraint does …[Read more]
Many times DBAs work with data sets that are not the most optimal (in their opinion), and I recently had the pleasure of working with a table that did not have a unique identifier. And I needed to get unique results from this table, as I would need to do this query in different forms in the future, without selecting records from the past.Caveat: I could not alter the table structure yet; I