How Mysql Uses Indexes - Oracle 5.0 Reference Manual

Table of Contents

Advertisement

MySQL cannot use the index to perform lookups if the columns do not form a leftmost prefix of the
index. Suppose that you have the
SELECT * FROM
SELECT * FROM
SELECT * FROM
SELECT * FROM
If an index exists on
fourth queries do involve indexed columns, but
of
(col1, col2,

8.5.3. How MySQL Uses Indexes

Indexes are used to find rows with specific column values quickly. Without an index, MySQL must
begin with the first row and then read through the entire table to find the relevant rows. The larger the
table, the more this costs. If the table has an index for the columns in question, MySQL can quickly
determine the position to seek to in the middle of the data file without having to look at all the data. If a
table has 1,000 rows, this is at least 100 times faster than reading sequentially. If you need to access
most of the rows, it is faster to read sequentially, because this minimizes disk seeks.
Most MySQL indexes
Exceptions are that indexes on spatial data types use R-trees, and that
hash indexes.
Strings are automatically prefix- and end-space compressed. See
Syntax".
In general, indexes are used as described in the following discussion. Characteristics specific to hash
indexes (as used in
MySQL uses indexes for these operations:
• To find the rows matching a
• To eliminate rows from consideration. If there is a choice between multiple indexes, MySQL normally
uses the index that finds the smallest number of rows.
• To retrieve rows from other tables when performing joins. MySQL can use indexes on columns
more efficiently if they are declared as the same type and size. In this context,
are considered the same if they are declared as the same size. For example,
CHAR(10)
Comparison of dissimilar columns may prevent use of indexes if values cannot be compared directly
without conversion. Suppose that a numeric column is compared to a string column. For a given
value such as
column such as '1',
column.
• To find the
optimized by a preprocessor that checks whether you are using
on all key parts that occur before
lookup for each
expressions are replaced with constants, the query returns at once. For example:
SELECT MIN(key_part2),MAX(key_part2)
FROM
tbl_name
• To sort or group a table if the sorting or grouping is done on a leftmost prefix of a usable key (for
example,
read in reverse order. See
Optimization".
BY
How MySQL Uses Indexes
SELECT
tbl_name
WHERE col1=val1;
tbl_name
WHERE
col1=val1
tbl_name
WHERE col2=val2;
tbl_name
WHERE
col2=val2
(col1, col2,
col3).
(PRIMARY
KEY, UNIQUE, INDEX, and FULLTEXT) are stored in B-trees.
tables) are described at the end of this section.
MEMORY
WHERE
are the same size, but
in the numeric column, it might compare equal to any number of values in the string
1
1', '00001', or '01.e1'. This rules out use of any indexes for the string
'
[971]
or
MIN()
MAX()
[971]
or
MIN()
MAX()
WHERE key_part1=10;
ORDER BY key_part1,
Section 8.3.1.11,
statements shown here:
AND col2=val2;
AND col3=val3;
col3), only the first two queries use the index. The third and
and
(col2)
clause quickly.
and
VARCHAR(10)
CHAR(15)
[971]
value for a specific indexed column key_col. This is
in the index. In this case, MySQL does a single key
key_col
[971]
expression and replaces it with a constant. If all
key_part2). If all key parts are followed by DESC, the key is
"ORDER BY
694
are not leftmost prefixes
(col2, col3)
tables also support
MEMORY
Section 13.1.8,
VARCHAR
VARCHAR(10)
are not.
WHERE key_part_N = constant
Optimization", and
Section 8.3.1.12,
"CREATE INDEX
and
CHAR
and
"GROUP

Advertisement

Table of Contents
loading

This manual is also suitable for:

Mysql 5.0

Table of Contents