简体   繁体   中英

How does an index work internally in mysql?

I am using indexes with my tables, but I honestly do not know how they work internally.

Let's say I have the following table

id age
1  20
2  35
3  19 
4  18

if I make an index of 'age', how does mysql organize the rows? Does it organize depends on field type? or...? how does mysql make indexes for unicode??

MySQL indexes are generally byte-ordered. The first part of the index is the data that is indexed (often the full field, though it can be a partial index of the first few characters), and the second part is the associated primary key.

For handling unicode, if MySQL is aware, it operates just like comparator classes in other languages. Unicode-aware versions of MySQL have a section of code which is aware of the proper ordering for unicode and can compare two unicode characters telling which one is greater than the other.

Your resultant index for InnoDB (assuming your PK is id) would be:

18->4
19->3
20->1
35->2

For MyISAM, an offset reference to the actual row location would be used.

Also, a good article that might give some insight: http://www.xaprb.com/blog/2006/07/04/how-to-exploit-mysql-index-optimizations/

MyISAM keeps rows in insertion order, not clustered by key. The index is hierarchical, with the leaves pointing to the actual rows. This article provides a good introduction.

I'm not sure what you mean by "internally", but here's a pretty comprehensive piece of the MySQL docs that explains indexes in the MyISAM engine.

Of course MySQL is open source, if you mean you want to understand how indexing works then you can look at the code.

InnoDB and MyISAM can both use(and which is a very popular choice by Database Implementors) B+ Trees, which is a data structure that is most popular in the use of indexes for HD bound databases(like most RDBMS). To learn more about B+Trees, I highly suggest reading up on them here .

You could have a look at my SQL Indexing Tutorial (covers MySQL) .

I guess many questions are already answered in the first chapter Anatomy of an Index: What is an SQL Index? .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM