Here comes your interviewer , Wearing a plaid shirt , With a beer belly , A middle-aged man whose hairline has moved back seriously .

Holding a thermos cup soaked with Chinese wolfberry , With arms MacBook,MacBook There are company slogans on the :“ Working overtime makes me happy ”.

interviewer : Look at your resume MySQL, Let me ask you a few simple questions . What are clustered indexes and nonclustered indexes ?

This problem can't help me . I'll take a look at a lamp before I come MySQL stereotyped writing .

I : for instance : There is such a user table

CREATE TABLE `user` (
`id` int COMMENT ' Primary key ID',
`name` varchar(10) COMMENT ' full name ',
`age` int COMMENT ' Age ',
PRIMARY KEY (`id`)
) ENGINE=InnoDB CHARSET=utf8 COMMENT=' User table ';

These data are stored in the user table :

idnaneage
1 A lamp 18
2 Zhang San 22
3 Li Si 21
4 The king 2 19
5 Pockmarks 20

So in the index , How is this data stored ?

MySQL Of InnoDB The index used in the engine B+ Tree structure .

Don't ask why the root node stores (1,4) Two elements , The left child node stores (1,2,3) Three elements , There are three leaf nodes below , Leaf nodes are connected by an ordered linked list ?

Asking is B+ Characteristics of trees , If you don't know, you can turn to the article in the previous issue .

As shown in the figure above , The leaf node stores the indexes of all elements , Namely Cluster index .

Generally, the primary key index is Cluster index , If there is no primary key in the table ,MySQL A hidden primary key will also be created by default as the primary key index .

What is a nonclustered index ?

Suppose we were age( Age ) Create a common index on the field ,age The index storage structure above the field is as follows :

Only the current index field and primary key are stored in the leaf node ID, Such a storage structure is a non clustered index .

interviewer : So what is a joint index ?

I : An index consisting of multiple fields is a union index .

interviewer : 【 dizzy 】 What are the benefits of building a joint index ? What is the difference between it and indexing on a single field ?

I : Suppose there is such a query statement .

select * from user where age = 18 and name = ' Zhang San ';

If we were age and name Two indexes are built on the field , This query statement only uses one of the indexes .

But we are age and name Field to create a union index (age,name), Its storage structure becomes like this .

If only age Build an index on it , We'll check age The above non clustered index , There are three age=18 The record of , Primary key ID Namely 1、4、5, Then use these three ID Go to query the primary key ID Clustering index of .

If in age and name Build a joint index on it , We'll check age and name The non clustered index above , Match to a record , Primary key ID yes 1, Then use this ID Go to query the primary key ID Clustering index of .

From this we can get , Advantages of Federated indexing : Greatly reduce the number of scanning lines .

interviewer : What is the leftmost matching principle ?

I : The leftmost matching principle refers to when establishing a union index , Follow top left priority , Any consecutive index starting from the leftmost can match up .

When we're in (age,name) When building a union index on ,where Only age You can use indexes , At the same time there is age and name You can also use indexes . But only name You can't use the index when .

Why does this happen ?

Look at the picture above , I understand. ,(age,name) Joint index of , It's according to age Sort ,age Equal lines are then followed by name Sort . If where Only one condition name, Of course you can't use an index .

interviewer : What are overlay indexes and backtable queries ?

I : This is even easier , This knowledge point has been mentioned above .

When we're in age When building an index , Inquire about SQL This is the time :

select id from user where age = 18;

The overlay index will be used , because ID We use age It has been found when indexing , There is no need to query the table twice .

But when querying SQL This is the time :

select * from user where age = 18;

Want to query all fields , You need to query the table twice . Because for the first time we used age When indexing, only the primary key is found ID, You also need to use the primary key ID Return to the table to query all fields .

interviewer : Ask one more , Do you know what index pushdown is ?

Such an unpopular question , You can ask , Really want to interview to build a rocket !

I : Index push down (Index Condition Pushdown) yes MySQL5.6 The introduction of a feature that optimizes indexes .

give an example :

stay (age,name) Build a joint index on it , And check SQL This is the time :

select * from user where age = 18 and name = ' Zhang San ';

If there is no index push down , Will match first age = 18 Three records of , Reuse ID Return to the table for query , select name = ' Zhang San ' The record of .

If you use index push down , Will match first age = 18 Three records of , And screen out name = ' Zhang San ' A record of , Last but not least ID Return to the table for query .

The resulting , Advantages of index push down : Reduces the number of rows scanned back to the table .

** interviewer : ** Young man , The eight part essay recites very smoothly . Let me give you a practical problem , See if you are ready . Here's the query SQL How to build a union index ?

select a from table where b = 1 and c = 2;

Deliberately make things difficult for me ? Do you think you can't recite the eight part essay for the practical problem ?

I : I have already mentioned this knowledge point when talking about joint index ,where There are conditions b and c The equivalent query of , The joint index is built (b,c), because select In the back a, We will establish (b,c,a) Joint index of , And you can use the overlay index , Faster queries .

interviewer : Young man , There's something . I'll send it to you in a minute offer, Come to work tomorrow , Salary double.

Articles are constantly updated , You can search through wechat 「 One light architecture 」 Read more technical dry goods for the first time .

An article makes it clear MySQL Clustering of / union / Overlay index 、 Back to the table 、 More related articles pushed down the index

  1. I can't tell Cookie、Session、Token、JWT? An article makes it clear

    I can't tell Cookie.Session.Token.JWT? An article makes it clear Reprint source official account : Front end plus author : Autumn leaves no leaves What is certification (Authentication) Generally speaking, it is to verify the identity of the current user , Prove ...

  2. Clustering ( Or aggregation ,cluster) Index and non clustered index

    The phonetic directory of dictionaries is called cluster (cluster) Indexes , The stroke directory is a non clustered index . This is how to query “G To M Chinese characters ” Very fast , And inquiry “6 Row to 8 Delimited character ” Then slow . Clustered index is a special index , It makes the data stored in the table according to the sort order of the index . Clustering ...

  3. 【mysql】 Indexes Back to the table Overlay index Index push down

    Index type Index types include primary key index and non primary key index .( Remember , How to store data ) The leaf node of the primary key index stores the whole row of data . stay InnoDB in , Primary key indexes are also called clustered indexes (clustered index). Nondominant ...

  4. An article makes it clear android ImageView.ScaleType

    2016-01-10 At first android When programming , About ImageView.ScaleType There are many on the Internet , To tell you the truth, I didn't understand . This article is to make this clear , Turn away if it's useful , Please indicate the original address and the author . A typical generation ...

  5. Oracle Index carding series ( Nine )- On the impact of clustering factor on the use of index and optimization methods

    Copyright notice : This article is published in http://www.cnblogs.com/yumiko/, Copyright by Yumiko_sunny all , Welcome to reprint . When reprinted , Please indicate the original link in the obvious position of the article . If without the author's consent , In this paper ...

  6. Clustered index 、 Nonclustered indexes 、 A clustered index organizes tables 、 Pile organization chart 、Mysql/PostgreSQL contrast 、 Combined the primary key / Self growth 、InnoDB/MyISAM( Another article on the engine )

    Refer to many articles , Record separately , as follows . The following is a summary of the first article http://www.jb51.net/article/76007.htm: stay MySQL in ,InnoDB The engine table is ( Gather ) Index organization table (cluste ...

  7. oracle Understanding of cluster table ( from :https://blog.csdn.net/gumengkai/article/details/51009345 )

    Oracle Two types of clustering are supported : Index clustering and hash clustering One . The principle of indexing clustered tables Clustering : If some tables have some common Columns , Such a set of tables is stored in the same data block Clustering also means storing related data in the same block . Use clustering , A block can ...

  8. oracle How an indexed clustered table works

    author :Richard-Lui One : First, I will introduce the working principle of the index cluster table :( Create a cluster first , Then create an index in the cluster , Specify the cluster type of the column when creating the table ) Clustering refers to : If a set of tables have some columns in common , Then store such a set of tables in the same database block : gather ...

  9. Detailed introduction Oracle Clustering technology of database

    author :* Tracy * 1. What is clustering d1=A=: Clustering is to find the physical storage location of data according to the code value , So as to achieve the purpose of rapid data retrieval . The order of clustering index is the physical storage order of data , Leaf nodes are data nodes . The order of non clustered indexes is the same as ...

  10. Mysql Advanced operation learning notes : Index structure 、 The difference between trees 、 Index advantages and disadvantages 、 Creating index principles ( What kind of data do we index )、 Index classification 、Sql Performance analysis 、 Index usage 、 Index failure 、 Index design principles

    Mysql Advanced operations Index Overview : Index is a data structure for efficient data acquisition Index structure : B+Tree() Hash( Range query is not supported , Accurate matching and high efficiency ) The difference between trees : Binary tree : There may be an imbalance , Sequential data may have a linked list structure ...

Random recommendation

  1. Learn more jQuery Selector series 4 —— The property selector of filter selector

    × Catalog [1] Simple attribute [2] Specific properties [3] Conditional properties In front of the word The filter rule of the attribute filter selector is to get the corresponding element through the attribute of the element , Corresponding to CSS Property selector in . Attribute filter selectors can be divided into simple attribute selectors . The specific attribute is ...

  2. Centos7 Set up IP Is a fixed value

    1. Into the system IP Address the directory where the file is saved [[email protected] ~]# cd /etc/sysconfig/network-scripts 2. Changes to save IP Documents of information [[email protected] ...

  3. Xshell Access the virtual machine Linux

    During this time at home , Need to use Linux, The computer hard disk is very small , The frequency of installing dual systems is not high, and disk space is wasted , Or use virtual machines , adopt Xshell Manage the virtual machine Ubuntu It's more convenient . Study very early hadoop When you use ...

  4. Beauty of design pattern :Memento( Memorandum )

    Indexes Intention structure participants Applicability effect Related patterns Realization Realization way ( One ):Memento Pattern structure style code . Alias Token Intention Without breaking encapsulation , Capture the internal state of an object , And save this... Outside the object ...

  5. [ Kernel synchronization ] elementary analysis Linux Kernel synchronization mechanism

    from :http://blog.csdn.net/fzubbsc/article/details/37736683?utm_source=tuicool&utm_medium=referral ...

  6. Intellij idea Development Hadoop MapReduce Program

    1. First download a Hadoop package , only Hadoop that will do . http://mirrors.hust.edu.cn/apache/hadoop/common/hadoop-2.6.0/hadoop-2.6.0 ...

  7. Rendering Problems: No Android SDK found. Please configure an Android SDK. How to solve ?

    Rendering Problems No Android SDK found. Please configure an Android SDK.

  8. Start using Mac OS X—— written Mac New people

    This article is from blog Garden :http://www.cnblogs.com/chijianqiang/archive/2011/08/03/2126593.html There are two reasons for writing this document , One . Use around Mac Friend, ...

  9. .NET Six swordsmen : Stack 、 Pile up 、 Value type 、 Reference type 、 Packing and unpacking

    .NET Six swordsmen : Stack . Pile up . Value type . Reference type . Packing and unpacking One .“ Pile up ”,“ Stack ” Prefecture I believe you are too familiar with these two words , Even what rice is ? I do not know! ...“ Pile up ”,“ Stack ” What is it? ? Oh , This one knows ... I've written a stack before ...

  10. eclipse or adt-bundle Created android The project is not automatically generated MainActivity.java and activity_main.xml And so on

    I used to use my computer all the time eclipse3.7 To develop android Project , establish android The project can also be generated normally MainActivity.java and activity_main.xml Wait for the documents . Later I didn't know anything ...