Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how B-tree indexing works in mysql

When I create an index for a table in mysql, I see that the index_type is type BTREE. Now although I understand about btree(s), I do not quiet understand how it stores the index and how the database searches the records based on this.

I mean, btree is excellent for databases to perform read and writes large blocks of data, when we create an index for column type of Primary key, what I understand is, it creates a tree and splitting the values for the root based on the value type of the root.

Now, does it store only the the primary key ID under the trees or the whole data associated with that primary key?

After finding the wanted primary ID, how does the database extract the record?

like image 916
JPro Avatar asked Mar 02 '10 11:03

JPro


People also ask

What is B-tree index in MySQL?

The B-Tree is the basic index structure for most MySQL storage engines. Each node in a B-Tree has between d and 2d values. Values in each node are sorted. Each node has between 0 to 2d+1 child nodes. Each child node is attached before, after, or between values.

How do B-tree indexes work?

A B-tree index creates a multi-level tree structure that breaks a database down into fixed-size blocks or pages. Each level of this tree can be used to link those pages via an address location, allowing one page (known as a node, or internal page) to refer to another with leaf pages at the lowest level.

Is B-tree used for indexing?

B-tree provides an efficient way to insert and read data. In actual Database implementation, the database uses both B-tree and B+tree together to store data. B-tree used for indexing and B+tree used to store the actual records.

How does MySQL indexing work?

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.


2 Answers

The database stores the value indexed as a B-Tree key, and the record pointer as a B-Tree value.

Whenever you search for a record holding a certain value of an indexed column, the engine locates the key holding this value in the B-Tree, retrieves the pointer to the record and fetches the record.

What exactly is a "record pointer", depends on the storage engine.

  • In MyISAM, the record pointer is an offset to the record in the MYI file.

  • In InnoDB, the record pointer is the value of the PRIMARY KEY.

In InnoDB, the table itself is a B-Tree with a PRIMARY KEY as a B-Tree key. This is what called a "clustered index" or "index-organized table". In this case, all other fields are stored as a B-Tree value.

In MyISAM, the records are stored without any special order. This is called "heap storage".

like image 169
Quassnoi Avatar answered Oct 16 '22 09:10

Quassnoi


MySQL InnoDB is actually using B+Tree which add more features than B-Tree.

Such as:

Only leaves node has values to allow more keys in the same page node to reduce tree high which will reduce I/O count.

Add a bidirectional pointer in each leaf node to make range search faster

B-Tree

B-Tree

B+Tree

B+Tree

like image 43
sendon1982 Avatar answered Oct 16 '22 10:10

sendon1982