一、
1.什么是B-Tree indexes?
The general idea of a B-Tree is that all the values are stored in order, and each leaf page is the same distance from the root.
A B-Tree index speeds up data access because the storage engine doesn’t have to scan the whole table to find the desired data. Instead, it starts at the root node (not shown in this figure). The slots in the root node hold pointers to child nodes, and the storage engine follows these pointers. It finds the right pointer by looking at the values in the
node pages, which define the upper and lower bounds of the values in the child nodes.Eventually, the storage engine either determines that the desired value doesn’t exist or successfully reaches a leaf page.
Leaf pages are special, because they have pointers to the indexed data instead of pointers to other pages. (Different storage engines have different types of “pointers” to the data.) Our illustration shows only one node page and its leaf pages, but there might be many levels of node pages between the root and the leaves. The tree’s depth depends on how big the table is.
Because B-Trees store the indexed columns in order, they’re useful for searching for ranges of data. For instance, descending the tree for an index on a text field passes through values in alphabetical order, so looking for “everyone whose name begins with I through K” is efficient.
2.例子
1 CREATE TABLE People ( 2 last_name varchar(50) not null, 3 first_name varchar(50) not null, 4 dob date not null, 5 gender enum('m', 'f')not null, 6 key(last_name, first_name, dob) 7 );
3、B-tree index的适用场景
B-Tree indexes work well for lookups by the full key value, a key range, or a key prefix. They are useful only if the lookup uses a leftmost prefix of the index. 3 The index we showed in the previous section will be useful for the
following kinds of queries:
Match the full value
A match on the full key value specifies values for all columns in the index. For example, this index can help you find a person named Cuba Allen who was born on 1960-01-01.
Match a leftmost prefix
This index can help you find all people with the last name Allen. This uses only the first column in the index.
Match a column prefix
You can match on the first part of a column’s value. This index can help you find all people whose last names begin with J. This uses only the first column in the index.
Match a range of values
This index can help you find people whose last names are between Allen and Barrymore. This also uses only the first column.
Match one part exactly and match a range on another part
This index can help you find everyone whose last name is Allen and whose first name starts with the letter K (Kim, Karl, etc.). This is an exact match on last_name and a range query on first_name .
Index-only queries
B-Tree indexes can normally support index-only queries, which are queries that access only the index, not the row storage. We discuss this optimization in “Covering Indexes” on page 177.
Because the tree’s nodes are sorted, they can be used for both lookups (finding values) and ORDER BY queries (finding values in sorted order). In general, if a B-Tree can help you find a row in a particular way, it can help you sort rows by the same criteria. So,our index will be helpful for ORDER BY clauses that match all the types of lookups we just listed.
4.B-tree index的缺点
• They are not useful if the lookup does not start from the leftmost side of the indexed columns. For example, this index won’t help you find all people named Bill or all people born on a certain date, because those columns are not leftmost in the index.Likewise, you can’t use the index to find people whose last name ends with a particular letter.
• You can’t skip columns in the index. That is, you won’t be able to find all people whose last name is Smith and who were born on a particular date. If you don’t specify a value for the first_name column, MySQL can use only the first column of the index.
• The storage engine can’t optimize accesses with any columns to the right of the first range condition. For example, if your query is WHERE last_name="Smith" AND first_name LIKE 'J%' AND dob='1976-12-23' , the index access will use only the first two columns in the index, because the LIKE is a range condition (the server can use the rest of the columns for other purposes, though). For a column that has a limited number of values, you can often work around this by specifying equality conditions instead of range conditions. We show detailed examples of this in the indexing case study later in this chapter.
Now you know why we said the column order is extremely important: these limitations are all related to column ordering. For optimal performance, you might need to create indexes with the same columns in different orders to satisfy your queries.