一、聚集索引介绍

1.什么是聚集索引?

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

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的结构

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的结构

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.

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

1.

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.主键是否连续为什么会有差别?

连续主键的插入

不连续主键的插入

插入不连续主键的缺点:

• 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.

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

  1. mysql 聚集索引 存什么_什么是mysql的聚集索引?

    什么是MySQL的聚集索引?在本文将给大家讲解mysql的聚集索引,包括聚集索引与普通的索引的区别. 在MySQL里,聚集索引和非聚集索引分别是什么意思,有什么区别?在MySQL中,InnoDB引擎表 ...

  2. MySQL聚集索引详解_MySQL innodb 聚集索引的概念与使用教程

    聚集索引是指数据库表行中数据的物理顺序与键值的逻辑(索引)顺序相同.一个表只能有一个聚集索引,因为一个表的物理顺序只有一种情况,所以,对应的聚集索引只能有一个. 在MySQL中,InnoDB引擎表是( ...

  3. mysql创建聚集索引sql_MySQL之聚集索引

    在MySQL里,聚集索引和非聚集索引分别是什么意思,有什么区别,课课家来讲一下他们的意思和区别. 在MySQL中,InnoDB引擎表是(聚集)索引组织表(clustered index organiz ...

  4. mysql 主键 聚集索引_MySQL主键索引和聚焦索引

    主键索引 主键索引,简称主键,原文是PRIMARY KEY,由一个或多个列组成,用于唯一性标识数据表中的某一条记录.一个表可以没有主键,但最多只能有一个主键,并且主键值不能包含NULL. 在MySQL ...

  5. mysql主键创建非聚集索引_什么是聚集索引,非聚集索引,索引覆盖,回表,索引下推...

    聚集索引 我们先建如下的一张表 CREATE TABLE `student` (`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '学号',`name` var ...

  6. 关于高性能mysql的读书报告_《高性能MySQL》读书笔记:第一章[MySQL架构与历史]...

    <高性能MySQL>读书笔记:第一章[MySQL架构与历史] MySQL逻辑架构 MySQL最优秀的一点就是它的存储架构,将查询处理,系统任务,数据存储/提取相分离 并发控制 通过读写锁实 ...

  7. mysql第五章项目二_高性能MySQL笔记 第5章 创建高性能的索引

    索引(index),在MySQL中也被叫做键(key),是存储引擎用于快速找到记录的一种数据结构.索引优化是对查询性能优化最有效的手段. 5.1 索引基础 索引的类型 索引是在存储引擎层而不是服务器层 ...

  8. MySQL高级 —— 高性能索引

    引言 最近一直在抱着<高性能MySQL(第三版)>研究MySQL相关热点问题,诸如索引.查询优化等,这阶段的学习是前一段时间MySQL基础与官方的"阅读理解"的进一步延 ...

  9. 高性能Mysql——创建高性能索引详解

    索引(在MySQL中也叫做"键(key)")是存储引擎用于快速找到记录的一种数据结构.这是索引的基本功能,除此之外,本章还将讨论索引其他一些方面有用的属性. 索引对于良好的性能非常 ...

最新文章

  1. 智能指针_auto_ptr2_学习笔记
  2. 计算机主机安装系统安装系统,系统重装
  3. SAP Spartacus central Travis build的lint环节
  4. 使用junit-drools进行JBoss Drools单元测试
  5. Adobe Edge Animate --点击元件内部元素使元件其他元素发生改变
  6. html5初探ppt,HTML5---HTML5初探151019讲义.ppt
  7. 数据结构——>线索化二叉树
  8. python基础教程之pymongo库
  9. nodejs中使用nodemon加载文件报错
  10. Sentinel Slot扩展实践-流控熔断预警实现
  11. 这年头数学不好,连表情包都看不懂了…
  12. 域服务器的信息存放在哪,域名服务器上存放着internet主机的
  13. AtCoder Beginner Contest 172 E - NEQ(二项式反演)
  14. 打开mysql 的时候报错_关于mysql的启动报错处理
  15. 前后端对接及接口管理平台浅析
  16. 该虚拟机似乎正在使用中
  17. 用matlab实现任意点图片的旋转_(实验二) --- 图像旋转变换---matlab实现
  18. Java简简单单抢红包小程序(代码)
  19. nodejs获取本地IP地址
  20. PCIe ARI (Alternative Routing-ID Interpretation)介绍

热门文章

  1. Java 8中新的并行API:Glitz和Glamour的背后
  2. Java Comparable接口的陷阱
  3. MongoDB主键是您的朋友
  4. 走向REST:在Spring和JAX-RS(Apache CXF)中嵌入Jetty
  5. 评论:Arun Gupta撰写的“ Java EE 6 Pocket Guide”
  6. Linux 命令之 localectl -- 控制系统的本地化与键盘布局
  7. 分析 Web 资源的访问过程(Servlet 程序访问过程)
  8. 关键词分词工具_快图制作工具 | 如何制作词云图?
  9. LeetCode 1290 二进制链表转整数
  10. LeetCode 26.删除排序数组中的重复项