<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Index on NoneBack</title><link>https://noneback.github.io/tags/index/</link><description>Recent content in Index on NoneBack created by</description><generator>Hugo -- gohugo.io</generator><language>en</language><copyright>@NoneBack All rights reserved</copyright><lastBuildDate>Sun, 21 Mar 2021 20:41:33 +0800</lastBuildDate><atom:link href="https://noneback.github.io/tags/index/index.xml" rel="self" type="application/rss+xml"/><item><title>MySQL Index Overview</title><link>https://noneback.github.io/blog/mysql%E7%B4%A2%E5%BC%95%E6%B5%85%E6%9E%90/</link><pubDate>Sun, 21 Mar 2021 20:41:33 +0800</pubDate><guid>https://noneback.github.io/blog/mysql%E7%B4%A2%E5%BC%95%E6%B5%85%E6%9E%90/</guid><description>&lt;p&gt;&lt;strong&gt;Database indexes&lt;/strong&gt; are sorted data structures in DBMS that help in quickly querying and updating data in a database. Generally, data structures used for building indexes include B-trees, B+ trees, hash tables, etc.&lt;/p&gt;
&lt;p&gt;MySQL uses B+ trees to build indexes. The reason for this choice is that a B+ tree node can store more data, and in a B+ tree, only leaf nodes store data, while non-leaf nodes store only indexes. This allows the index to be stored in memory as much as possible, reducing tree height, minimizing disk I/O when querying indexes, and greatly improving query efficiency.&lt;/p&gt;
&lt;h2 id="indexes-in-innodb"&gt;Indexes in InnoDB&lt;/h2&gt;
&lt;h3 id="clustered-index-and-non-clustered-index"&gt;Clustered Index and Non-Clustered Index&lt;/h3&gt;
&lt;p&gt;Indexes can be divided into clustered and non-clustered indexes based on the data stored in the leaf nodes.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Clustered Index&lt;/strong&gt;: The leaf nodes store the data rows directly, allowing direct access to user data.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Non-Clustered Index&lt;/strong&gt;: The leaf nodes store the primary key value, and data must be fetched by traversing back to the primary key index (a process known as &lt;strong&gt;index backtracking&lt;/strong&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In the InnoDB engine, the table&amp;rsquo;s data is organized using the primary key index. Each table must have a primary key, which constructs the B+ tree, resulting in a primary key index. &lt;strong&gt;The primary key index is a clustered index&lt;/strong&gt;, and all other &lt;strong&gt;secondary indexes are non-clustered indexes&lt;/strong&gt;.&lt;/p&gt;
&lt;h3 id="composite-index"&gt;Composite Index&lt;/h3&gt;
&lt;p&gt;A composite index is an index composed of multiple fields.&lt;/p&gt;
&lt;div class="code-block" data-language="sql"&gt;
&lt;div class="code-block-header"&gt;
&lt;div class="code-block-meta"&gt;
&lt;span class="code-block-language"&gt;sql&lt;/span&gt;
&lt;/div&gt;
&lt;div class="code-block-actions"&gt;
&lt;button class="copy-code" type="button" aria-label="Copy"&gt;Copy&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-sql" data-lang="sql"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;create&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;index&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;index_name&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;on&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;table_name&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;col_1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;col_2&lt;/span&gt;&lt;span class="p"&gt;...)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Compared to a single-field index, the main difference is that it follows the &lt;strong&gt;leftmost prefix matching principle&lt;/strong&gt;.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Leftmost Prefix Matching Principle&lt;/strong&gt;: When using a composite index, the index values are sorted according to the fields in the index from left to right.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id="using-indexes-to-optimize-query-performance"&gt;Using Indexes to Optimize Query Performance&lt;/h2&gt;
&lt;p&gt;Since indexes are ordered, they can significantly improve query efficiency. When using indexes for query optimization, some principles must be followed.&lt;/p&gt;
&lt;h3 id="leftmost-prefix-matching-principle"&gt;Leftmost Prefix Matching Principle&lt;/h3&gt;
&lt;p&gt;When using a composite index, the index values are sorted from left to right based on the indexed fields. We need to follow the leftmost prefix matching rule in our queries; otherwise, the data arrangement becomes unordered, resulting in a full table scan.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Suppose you create an index on &lt;code&gt;col1, col2, col3&lt;/code&gt;. Following the leftmost prefix matching principle, the query conditions should be designed in the order &lt;code&gt;col1 -&amp;gt; col2 -&amp;gt; col3&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Example:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;select * from table_name where col1 = 1 and col2 = 2;&lt;/code&gt; This will use the index.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;select * from table_name where col2 = 1 and col3 = 2;&lt;/code&gt; This will not use the index.&lt;/p&gt;
&lt;p&gt;Note: &lt;strong&gt;MySQL will continue matching the columns until it encounters a range query (&amp;gt;, &amp;lt;, between, like), after which it stops matching.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3 id="index-coverage-principle"&gt;Index Coverage Principle&lt;/h3&gt;
&lt;p&gt;Index coverage refers to querying values directly from the index without needing to traverse back to the table. Well-designed indexes can reduce the number of backtracking operations.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;For a composite index &lt;code&gt;(col1, col2, col3)&lt;/code&gt;:&lt;/p&gt;
&lt;p&gt;A query like &lt;code&gt;select col1, col2, col3 from test where col1=1 and col2=2&lt;/code&gt; can directly retrieve values for &lt;code&gt;col1&lt;/code&gt;, &lt;code&gt;col2&lt;/code&gt;, and &lt;code&gt;col3&lt;/code&gt; without needing to traverse back to the table, as their values are already stored in the secondary index.&lt;/p&gt;
&lt;/blockquote&gt;</description></item></channel></rss>