search
HomeDatabaseMysql TutorialMysql simple index plan analysis

    Mysql simple index

    1. How to search when there is no index

    Ignore the concept of index first , if you want to check a certain record directly now, how to search it?

    Search in one page

    If there are very few records in the table and one page is enough, then there are two situations:

    • Use the primary key as the search condition: This is the method mentioned in the previous article. Use the dichotomy method to quickly locate the slot in the page directory, then traverse the records corresponding to the group of the slot, and finally find the specified record.

    • Use other non-primary key columns as search conditions: Because there is no page directory for non-primary key columns in the data page, the slot cannot be quickly located through the dichotomy method. You can only start from the Infimum record once. Traversing each record in a singly linked list is inefficient.

    Search in many pages

    When there are many records in the table, many data pages will be used to store them. In this case, 2 steps are required:

    • Locate the page where the record is located.

    • Repeat the above process of searching in a page.

    Generally speaking, when there is no index, we cannot quickly locate the page where the record is located. We can only follow the doubly linked list from the first page (the page has the previous page and the next page) Keep searching, and then repeat the above process on each page to query the specified record, which requires traversing all records, which is very time-consuming.

    2. A simple index

    Since the positioning record is too slow due to too many pages, how to solve it? You may wish to refer to the "Page Directory".

    The page directory is set up to quickly locate the position of a record in the page based on the primary key. Therefore, we can explore a method of creating an "other directory" to quickly locate the page where the record is located.

    But there are two things that need to be done in order to complete this "other directory".

    1. The primary key value of the user record on the next page must be greater than that of the previous page.

    Assuming that each data page can hold up to 3 records (actually many can be placed), then Now insert 3 records into the table, each record has 3 columns c1, c2, and c3. For convenience, the storage row format is also simplified, leaving only key attributes. The virtual records Infimum and Supremum are located at the beginning and end of the user record respectively, with three user records in the middle.

    Mysql simple index plan analysis

    At this time, continue to insert 1 record. In the hypothetical case, at least one new page needs to be allocated, so the two pages will be reallocated and rearranged.

    Mysql simple index plan analysis

    Please note that the two records shown in red font include a newly inserted record with a primary key of 4, which should be placed on a new page. However, in order to satisfy the requirement that the primary key value of the user record on the next page must be greater than the primary key value of the user record on the previous page, operations such as record movement are performed. This process can also be called "page splitting".

    Also, why is the new page page 28, not 11? Because the pages may not be next to each other on the disk, they just establish a linked list relationship by maintaining the numbers of the previous page and the next page.

    2. Create a directory entry for all pages

    Now continue to add data to the table. The final relationship between multiple pages is as follows:

    Mysql simple index plan analysis

    In order to quickly locate a record from multiple non-adjacent pages, a directory needs to be compiled for them, because these pages may not be contiguous on the disk.

    Each page corresponds to a directory entry, and each directory entry includes:

    • The smallest primary key value in the user record of the page, represented by key

    • Page number, represented by page_no

    So, after cataloging them, the relationship will be like this:

    Mysql simple index plan analysis

    So, now I want to find the record with the primary key value of 20. I will do it in two steps:

    Use the dichotomy method to quickly determine the record with the primary key value of 20 from the directory entry. Item 3, and the page number it is on is 9. Knowing that it is on page 9, repeat the previous approach to find the final target record.

    At this point, a simple plan is completed. The completed simple directory has an alias called index.

    3. Problems exposed by simple index

    The above-mentioned simple index is the content set up by the author of the original book to help readers understand step by step. This is not the indexing plan of innodb.

    Then look at the above suggested index and see what problems there are.

    Question 1:

    InnoDB uses pages as the basic unit for managing storage space, which means it can only store up to 16kb of continuous storage.

    When there are more and more records in the table, a very large continuous storage space is needed to hold all the directory entries, which is unrealistic for tables with large amounts of data.

    Question 2:

    We often have to add, delete, and modify records, which will affect the whole body.

    For example, if I delete all the records on page 28 in the picture above, then page 28 does not need to exist, and directory entry 2 does not need to exist. At this time, you need to move the directory items after directory item 2 forward.

    Even if it is not moved, placing directory entry 2 as redundant in the directory entry list will still waste a lot of storage space.

    The above is the detailed content of Mysql simple index plan analysis. For more information, please follow other related articles on the PHP Chinese website!

    Statement
    This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
    图文详解mysql架构原理图文详解mysql架构原理May 17, 2022 pm 05:54 PM

    本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于架构原理的相关内容,MySQL Server架构自顶向下大致可以分网络连接层、服务层、存储引擎层和系统文件层,下面一起来看一下,希望对大家有帮助。

    mysql怎么替换换行符mysql怎么替换换行符Apr 18, 2022 pm 03:14 PM

    在mysql中,可以利用char()和REPLACE()函数来替换换行符;REPLACE()函数可以用新字符串替换列中的换行符,而换行符可使用“char(13)”来表示,语法为“replace(字段名,char(13),'新字符串') ”。

    mysql怎么去掉第一个字符mysql怎么去掉第一个字符May 19, 2022 am 10:21 AM

    方法:1、利用right函数,语法为“update 表名 set 指定字段 = right(指定字段, length(指定字段)-1)...”;2、利用substring函数,语法为“select substring(指定字段,2)..”。

    mysql的msi与zip版本有什么区别mysql的msi与zip版本有什么区别May 16, 2022 pm 04:33 PM

    mysql的msi与zip版本的区别:1、zip包含的安装程序是一种主动安装,而msi包含的是被installer所用的安装文件以提交请求的方式安装;2、zip是一种数据压缩和文档存储的文件格式,msi是微软格式的安装包。

    mysql怎么将varchar转换为int类型mysql怎么将varchar转换为int类型May 12, 2022 pm 04:51 PM

    转换方法:1、利用cast函数,语法“select * from 表名 order by cast(字段名 as SIGNED)”;2、利用“select * from 表名 order by CONVERT(字段名,SIGNED)”语句。

    MySQL复制技术之异步复制和半同步复制MySQL复制技术之异步复制和半同步复制Apr 25, 2022 pm 07:21 PM

    本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于MySQL复制技术的相关问题,包括了异步复制、半同步复制等等内容,下面一起来看一下,希望对大家有帮助。

    带你把MySQL索引吃透了带你把MySQL索引吃透了Apr 22, 2022 am 11:48 AM

    本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了mysql高级篇的一些问题,包括了索引是什么、索引底层实现等等问题,下面一起来看一下,希望对大家有帮助。

    mysql怎么判断是否是数字类型mysql怎么判断是否是数字类型May 16, 2022 am 10:09 AM

    在mysql中,可以利用REGEXP运算符判断数据是否是数字类型,语法为“String REGEXP '[^0-9.]'”;该运算符是正则表达式的缩写,若数据字符中含有数字时,返回的结果是true,反之返回的结果是false。

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    Repo: How To Revive Teammates
    4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    Hello Kitty Island Adventure: How To Get Giant Seeds
    4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    ZendStudio 13.5.1 Mac

    ZendStudio 13.5.1 Mac

    Powerful PHP integrated development environment

    Safe Exam Browser

    Safe Exam Browser

    Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

    PhpStorm Mac version

    PhpStorm Mac version

    The latest (2018.2.1) professional PHP integrated development tool