高性能mysql 聚簇索引,高性能MySQL笔记-第5章Indexing for High Performance-005聚集索引...

一、聚集索引介绍

1.什么是聚集索引?

InnoDB’s clustered indexes actually store a B-Tree index and the rows together in the same structure.

1dcc7a69df58f3a51153d5070e40474f.png

2.为什么一张表只能一个聚集索引?

When a table has a clustered index, its rows are actually stored in the index’s leaf pages.The term “clustered” refers to the fact that rows with adjacent key values are stored close to each other.  You can have only one clustered index per table, because you can’t store the rows in two places at once. (However, covering indexes let you emulate mul-

tiple clustered indexes; more on this later.)

3.聚集索引的优点

• You can keep related data close together. For example, when implementing a mailbox, you can cluster by user_id , so you can retrieve all of a single user’s messages by fetching only a few pages from disk. If you didn’t use clustering, each message might require its own disk I/O.

• Data access is fast. A clustered index holds both the index and the data together in one B-Tree, so retrieving rows from a clustered index is normally faster than a comparable lookup in a nonclustered index.

• Queries that use covering indexes can use the primary key values contained at the leaf node.

4.聚集索引的缺点

• Clustering gives the largest improvement for I/O-bound workloads. If the data fits in memory the order in which it’s accessed doesn’t really matter, so clustering doesn’t give much benefit.

• Insert speeds depend heavily on insertion order. Inserting rows in primary key order is the fastest way to load data into an InnoDB table. It might be a good idea to reorganize the table with OPTIMIZE TABLE after loading a lot of data if you didn’t load the rows in primary key order.

• Updating the clustered index columns is expensive, because it forces InnoDB to move each updated row to a new location.

• Tables built upon clustered indexes are subject to page splits when new rows are inserted, or when a row’s primary key is updated such that the row must be moved.A page split happens when a row’s key value dictates that the row must be placed into a page that is full of data. The storage engine must split the page into two to

accommodate the row. Page splits can cause a table to use more space on disk.

• Clustered tables can be slower for full table scans, especially if rows are less densely packed or stored nonsequentially because of page splits.

• Secondary (nonclustered) indexes can be larger than you might expect, because their leaf nodes contain the primary key columns of the referenced rows.

• Secondary index accesses require two index lookups instead of one.

二、聚集索引(用innodb)与非聚集索引(用MyISAM)的区别

表结构

CREATE TABLE layout_test ( col1 intNOT NULL, col2 intNOT NULL, PRIMARY KEY(col1), KEY(col2) );

1.MyISAM的结构

b044f5d77157901beb60c2bce03c7f4f.png

ce85266a72ff15035bac0bba75d4c566.png

20baccc3b024f1bed5172f2dc0883080.png

In fact, in MyISAM, there is no structural difference between a primary key and anyother index. A primary key is simply a unique, nonnullable index named PRIMARY .

2.Innodb的结构

6649e67ca25342fb69788dd392a16c00.png

At first glance, that might not look very different from Figure 5-5. But look again, andnotice that this illustration shows the whole table, not just the index. Because theclustered index “is” the table in InnoDB, there’s no separate row storage as there is forMyISAM.

Each leaf node in the clustered index contains the primary key value, the transactionID, and rollback pointer InnoDB uses for transactional and MVCC purposes, and therest of the columns (in this case, col2 ). If the primary key is on a column prefix, InnoDBincludes the full column value with the rest of the columns.

Also in contrast to MyISAM, secondary indexes are very different from clustered indexes in InnoDB. Instead of storing “row pointers,” InnoDB’s secondary index leafnodes contain the primary key values, which serve as the “pointers” to the rows. Thisstrategy reduces the work needed to maintain secondary indexes when rows move or

when there’s a data page split. Using the row’s primary key values as the pointer makesthe index larger, but it means InnoDB can move a row without updating pointers to it.

f22320f8a2245bce94ae48580340d713.png

8aa925c85716ad4c297bbd44c549ad21.png

三、用聚集索引时,primary key是否连续的影响

1.

d04b725b725faa48f887b872ce7a9afe.png

Notice that not only does it take longer to insert the rows with the UUID primary key,but the resulting indexes are quite a bit bigger. Some of that is due to the larger primarykey, but some of it is undoubtedly due to page splits and resultant fragmentation as well.

2.主键是否连续为什么会有差别?

连续主键的插入

ce0c36b8c07a595a1a1fd6bf57d30945.png

不连续主键的插入

f12dc5deeef020278f90284ad134e319.png

插入不连续主键的缺点:

• The destination page might have been flushed to disk and removed from the caches,or might not have ever been placed into the caches, in which case InnoDB will haveto find it and read it from the disk before it can insert the new row. This causes alot of random I/O.

• When insertions are done out of order, InnoDB has to split pages frequently tomake room for new rows. This requires moving around a lot of data, and modifyingat least three pages instead of one.

• Pages become sparsely and irregularly filled because of splitting, so the final datais fragmented.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/345020.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【渝粤题库】广东开放大学社会学概论形成性考核

选择题 题目:我国职业分层的基本特点是:职业地位的高低主要取决于()。 题目:()标示作为控制途径的社会规范的严密程度。 题目:在各种社会制度中,除了经济制度外&#xff…

【渝粤题库】陕西师范大学151213 公司战略与风险管理作业(专升本)

《内部控制与风险管理》作业 一、单选题 1.1992年,COSO发布了著名的( )。 2.下列选项中,被称为“萨班斯法案”中最难操作、最复杂、耗费成本最高的一个条款的是( )。 3.根据基本规定,规范企业内…

英特尔 至强 单核计算性能_使用英特尔性能计数器调整垃圾收集

英特尔 至强 单核计算性能介绍 我不得不承认我很震惊。 确实,当我意识到这个出现的日历帖子将涉及垃圾收集时,我感到非常震惊。 GC的主题引起了Java倡导者和那些认为内存管理应该是手动的人的热情。 撰写了许多文章,内容涉及看起来奇怪的命令…

mppt多峰追踪MATLAB仿真,基于光伏功率等效面积法的多峰最大功率追踪控制方法...

0引言在光伏系统实际应用中,由于天上移动的云朵、电池板累积的灰尘以及城镇中周围建筑物等的影响,光伏阵列在运行过程中总会受到不同程度的阴影遮挡,太阳能电池板的P-U曲线会受到影响出现多个峰值点。传统的MPPT控制方法在电池板没受到局部阴影影响的情况下可以有效的追踪到电池…

【渝粤题库】陕西师范大学163208 饭店管理 作业

《饭店管理》作业 名词解释 1、饭店星级制 2、品牌忠诚度 3、指挥职能 4、风险型决策 5、饭店全面质量管理 6、饭店形象识别系统(CIS) 7、饭店市场细分 8、饭店产品 9、单一性需要 10、社会性消费动机 11、决策职能 12、因果分析图法 13、知名度 14、客房…

【渝粤题库】陕西师范大学165104 组织行为学原理 作业(高起专)

《组织行为学原理》作业 一、单项选择题 1.同一个人穿横条纹的衣服会显得胖些,穿竖条纹的衣服会显得瘦些。这种现象称之为____。 A适应 B对比 C敏感化 D感受性降低 2.人们以自己所具有的品质为依据去判断别人的品质&am…

PHP应用GD2函数填充几何图形,使用GD2函数绘制几何图形(PHP图形图像的典型应用教程4)...

使用GD2函数绘制几何图形(PHP图形图像的典型应用教程4)本篇主要讲解使用GD2函数实现几何图形的绘制,首先我们需要的事创建一个图像,在之前的文章中我们就说过了,创建图像是所有图像操作的第一步,然后再背景上根据坐标点绘制图形轮…

API测试和自动化101:基本指南

API代表A pplication P AGC软件我覆盖整个院落。 通常,API用于通过使用任何通信方式来促进两个不同应用程序之间的交互。 在网络上使用API​​时,我们将其称为“ Web服务”。 最近,API已成为编程的Struts。 与在应用程序中一样,编…

【渝粤题库】陕西师范大学200161 文字学概论 作业

《文字学概论》作业 一、单选题 1、原始社会陶器上的图形符号刻划是以( )为代表的。 A 仰韶文化 B 马家窑文化 C 良渚文化 D 大汶口文化 2、下列各字中不属于象形字的是( )。 A十 B小 C羊 D未 3、“画成弃物 ,随体诘屈”,是许慎给…

oracle 调整dbw0,求助:DBW的内存占用率高,可能是什么原因?

没人遇到这样的问题吗?补充一个现象,v$session中"SQL*Net message from client"很多,下面是随便选择一个时间点查到的数据:SQL> select t.EVENT, count(1) from v$session t group by t.EVENT;EVENT …

【渝粤题库】陕西师范大学200601 英语报刊阅读

《报刊阅读》作业 Translate the following words into Chinese 1.International Herald Tribune 2.Secretary of State 3.Justice Department 4.Cult 5.Catholic Church 6.Bosnia 7.The Netherlands 8.EU 9.FBI 10.UNESCO 11.The Commonwealth 12.Speaker 13.constitutional…

【渝粤题库】陕西师范大学200971教育经济学 作业(专升本、高起本)

《教育经济学》作业 一、单选题 1.我国多数学者认为,应该把教育经济学隶属于经济科学体系,属于( )。 A.宏观经济学 B.微观经济学 C.部门经济学 D.产业经济学 2.教育经济学是现代经济和现代教育的产物,它在西方萌芽于20世纪20年代,形成于( &am…

oracle数据库安装HotSpot,安装Oracle数据库软件遭遇诡异的HotSpot Virtual Machine Error : 11报错...

虽然也装了很多次的数据库了,可是偶尔还是会碰见一些很无语的错误,前两天在RHEL5.0上安装Oracle 10g 10.2.0.1,起图形后点击下虽然也装了很多次的数据库了,可是偶尔还是会碰见一些很无语的错误,前两天在RHEL5.0上安装O…

jdk8lambda_JDK8 lambda的会话指南–术语表

jdk8lambda上次出现…我写了一篇与JDK8为我们提供的新方法有关的文章。 最令我兴奋的功能是lambda。 我必须承认,在即将成为浪子的第一年(在此期间,我使用C#进行了开发),我喜欢LINQ和它可以做的漂亮&#x…

【渝粤题库】陕西师范大学202041 国际经济学 作业(专升本)

《国际经济学》作业 一.判断题 1.重商主义认为每个国家应该努力实现出口等于进口的平衡贸易 2. 比较优势原理只能应用于简单的两国模型中,而不能应用于现实中众多国家与贸易的情况。 3.一般来说…

【渝粤题库】陕西师范大学202831 程序设计 作业(专升本)

陕西师范大学 内 部 题 库 教育 (yuyueshool) 编制 《VB程序设计》作业 一.选择题 1.在Visual Basic中,称对象的数据为_____。 (A) 属性 (B) 方法 © 事件 (D) 封装 2.下列选项中为字符串常量的是___…

oracle查询字段有list,java读取oracle表字段信息存到列表(LIST)中

今天在研究如何将oracle表字段的信息存到列表数组中,做了一点实验,是在java1.5下,用myeclips做的,代码如下:public static List> gocilist(Connection conn,String table){ResultSet rs null;Statement stmtnull;/…

【渝粤题库】陕西师范大学209912 金融中介学Ⅰ 作业

《金融中介学Ⅰ》作业 一、单选题 1、金融中介提供的是服务,收取的是( ) A 手续费 B 佣金 C 服务费 D金融费 2、( )是金融市场活动的组织承担者。 A银行 B投资银行 C 金融中介机构 D投资者 3、( &#xff0…

【渝粤题库】陕西师范大学210032学前心理学 作业(专升本)

《学前心理学》作业 一、填空 1、研究儿童心理必须遵循的原则是______ 、 _______ 、。 2、新生儿最基本的无条件反射是 、 ________ 、。 3、学前儿童掌握实物概念的特点是以_______为主,以_______为主。 4、感觉既是一种______现象,又是一种_______现象…