Oracle 5.0 Reference Manual page 713

Table of Contents

Advertisement

MySQL can use multiple-column indexes for queries that test all the columns in the index, or queries
that test just the first column, the first two columns, the first three columns, and so on. If you specify the
columns in the right order in the index definition, a single composite index can speed up several kinds
of queries on the same table.
A multiple-column index can be considered a sorted array, the rows of which contain values that are
created by concatenating the values of the indexed columns.
Note
As an alternative to a composite index, you can introduce a column that is
"hashed" based on information from other columns. If this column is short,
reasonably unique, and indexed, it might be faster than a "wide" index on many
columns. In MySQL, it is very easy to use this extra column:
SELECT * FROM
WHERE hash_col=MD5(CONCAT(val1,val2))
AND
Suppose that a table has the following specification:
CREATE TABLE test (
id
INT NOT NULL,
last_name
CHAR(30) NOT NULL,
first_name CHAR(30) NOT NULL,
PRIMARY KEY (id),
INDEX name (last_name,first_name)
);
The
index is an index over the
name
for lookups in queries that specify values in a known range for combinations of
values. It can also be used for queries that specify just a
first_name
column is a leftmost prefix of the index (see
index is used for lookups in the following queries:
name
SELECT * FROM test WHERE last_name='Widenius';
SELECT * FROM test
WHERE last_name='Widenius' AND first_name='Michael';
SELECT * FROM test
WHERE last_name='Widenius'
AND (first_name='Michael' OR first_name='Monty');
SELECT * FROM test
WHERE last_name='Widenius'
AND first_name >='M' AND first_name < 'N';
However, the
index is not used for lookups in the following queries:
name
SELECT * FROM test WHERE first_name='Michael';
SELECT * FROM test
WHERE last_name='Widenius' OR first_name='Michael';
Suppose that you issue the following
mysql>
SELECT * FROM tbl_name WHERE col1=val1 AND col2=val2;
If a multiple-column index exists on
If separate single-column indexes exist on
Merge optimization (see
restrictive index by deciding which index excludes more rows and using that index to fetch the rows.
If the table has a multiple-column index, any leftmost prefix of the index can be used by the optimizer
to find rows. For example, if you have a three-column index on
indexed search capabilities on (col1),
Multiple-Column Indexes
tbl_name
col1=val1
AND col2=val2;
last_name
Section 8.5.2, "Multiple-Column
statement:
SELECT
and col2, the appropriate rows can be fetched directly.
col1
col1
Section 8.3.1.4, "Index Merge
(col1,
693
and
columns. The index can be used
first_name
last_name
and col2, the optimizer attempts to use the Index
Optimization"), or attempts to find the most
(col1, col2,
col2), and
(col1, col2,
and
last_name
value because that
Indexes"). Therefore, the
col3), you have
col3).

Advertisement

Table of Contents
loading

This manual is also suitable for:

Mysql 5.0

Table of Contents