当前位置: 首页 > news >正文

电子商务网站建设公司阜阳seo

电子商务网站建设公司,阜阳seo,西安网站开发招聘,vps 网站异常系列文章 基础篇——MySQL 的基础架构 基础篇——redo log 和 binlog 基础篇——事务隔离 目录 系列文章深入浅出索引(上)4.1 索引的常见模型4.2 InnoDB 的索引模型4.3 索引维护4.4 思考:为什么要重建索引以及如何做? 深入浅出索…

系列文章

基础篇——MySQL 的基础架构
基础篇——redo log 和 binlog
基础篇——事务隔离


目录

  • 系列文章
  • 深入浅出索引(上)
    • 4.1 索引的常见模型
    • 4.2 InnoDB 的索引模型
    • 4.3 索引维护
    • 4.4 思考:为什么要重建索引以及如何做?


深入浅出索引(上)

索引的出现其实就是为了提高数据查询的效率,就像书的目录一样。我们从小就用的汉语字典里面的声母查询方式就是聚簇索引, 偏旁部首就是二级索引,偏旁部首+笔画就是联合索引。同样,对于数据库的表而言,索引其实就是它的“目录”。

4.1 索引的常见模型

  1. 哈希表:它是一种以键 - 值(key-value)存储数据的结构。把值放在数组里,用一个哈希函数把 key 换算成一个确定的位置,然后把 value 放在数组的这个位置。如果多个 key 值经过哈希函数的换算,会出现了同一个值,处理这种情况的一种方法是,拉出一个链表。

    哈希表这种结构适用于只有等值查询的场景。因为在哈希表中,数据是根据它们的哈希值散列存储的,这意味着相邻的键值在物理上可能相隔很远。对于区间查询,这种无序性导致无法直接定位到查询区间的起始点和结束点(等值查询就是用等号来匹配查询结果,分为单条件查询、多条件查询,与等值查询对应的是模糊查询、范围查询)。

  2. 有序数组:在等值查询和范围查询场景中的性能就都非常优秀。使用二分法能够快速得到值,时间复杂度是O(log(N))。

    缺点是更新数据的成本高,所以有序数组索引只适用于静态存储引擎,比如要保存的是一些不会再修改的历史数据。

  3. 搜索树:树可以有二叉,也可以有多叉。搜索树的特点就是父节点的左子树所有节点的值小于父节点的值,右子树所有节点的值大于父节点的值。这种结构使得查找操作的时间复杂度可以保持在O(log(N))。

    大多数数据库存储不使用二叉树而是多叉搜索树比如B+树。因为索引不止存在内存中,还要写到磁盘上。二叉树比 n 叉树的高度更高,这就意味着需要更多的磁盘 I/O,时间更长。

4.2 InnoDB 的索引模型

InnoDB 使用了 B+ 树索引模型,所以数据都是存储在 B+ 树中的。每一个索引在 InnoDB 里面对应一棵 B+ 树。

根据叶子节点的内容,索引类型分为主键索引和非主键索引

  • 主键索引的叶子节点存的是整行数据。在 InnoDB 里,主键索引也被称为聚簇索引(clustered index)
  • 非主键索引的叶子节点内容是主键的值。在 InnoDB 里,非主键索引也被称为二级索引(secondary index)。

在查询方面,假设,我们有一个主键列为 ID 的表,表中有字段 k,并且在 k 上有索引。

  • 如果语句是 select * from T where ID=500,即主键查询方式,则只需要搜索 ID 这棵 B+ 树;
  • 如果语句是 select * from T where k=5,即普通索引查询方式,则需要先搜索 k 索引树,得到 ID 的值为 500,再到 ID 索引树搜索一次。这个过程称为回表

所以,使用非主键索引查询可能会触发回表,因为非主键索引的B+树中只存储着数据的主键索引(上面例子里的ID),我们需要用ID在到主键索引的B+树中查询一遍,找到完整的数据。

4.3 索引维护

B+ 树为了维护索引有序性,在插入新值的时候需要做必要的维护。

因此推荐使用自增主键(NOT NULL PRIMARY KEY AUTO_INCREMENT

  • 从性能角度:自增主键的插入数据模式,正符合了递增插入的场景。这种情况下每次插入一条新记录,都是追加操作,都不涉及到挪动其他记录,也不会触发叶子节点的分裂。而有业务逻辑的字段做主键,则往往不容易保证有序插入,这样写数据成本相对较高。

  • 从存储空间角度:主键长度越小,普通索引的叶子节点就越小,普通索引占用的空间也就越小。

当然也有些场景适合用业务字段直接做主键:

  1. 只有一个索引;
  2. 该索引必须是唯一索引。

这就是典型的KV 场景,这种场景下的数据被存储为键和值之间的映射,适用于只需要通过一个唯一键来访问特定值的场合。

因为没有其他索引,所以不用考虑非聚簇索引(二级索引)空间占用大小问题,并且也不用考虑回表的问题

4.4 思考:为什么要重建索引以及如何做?

当我我们删除了表中的一些行时,这些行只是被标记为“已删除”,而不是真的从索引中物理删除了,因而空间也没有真的被释放回收。 InnoDB的Purge线程会异步的来清理这些没用的索引键和行,但是依然没有把这些释放出来的空间还给操作系统重新使用,因而会导致页面中存在很多空洞。

重建索引是将原始索引在磁盘和内存中都删除,再根据原始数据建立索引,可以将原始索引中由于页分裂等原因造成的数据页的利用率低的问题解决 提高页利用率。

如果要重建一个普通索引k ,可以用下面两个 SQL 语句:

alter table T drop index k;
alter table T add index(k);

但是如果是重建一个主键,就不能通过上面两个命令,因为。不论是删除主键还是创建主键,都会将整个表重建。所以连着执行这两个语句的话,第一个语句就白做了。所以这两个语句,可以用这个语句代替 :

alter table T engine=InnoDB

下面是本篇的思维导图以供参考:

在这里插入图片描述


文章转载自:
http://dinncotimecard.bkqw.cn
http://dinncomarten.bkqw.cn
http://dinncohurter.bkqw.cn
http://dinncoacclamation.bkqw.cn
http://dinncodashaveyor.bkqw.cn
http://dinncobluetongue.bkqw.cn
http://dinncosammy.bkqw.cn
http://dinncoveto.bkqw.cn
http://dinncocalabrian.bkqw.cn
http://dinncocoinsurance.bkqw.cn
http://dinncolakeside.bkqw.cn
http://dinncolinksman.bkqw.cn
http://dinncoultramicro.bkqw.cn
http://dinncomagnitogorsk.bkqw.cn
http://dinncocircularise.bkqw.cn
http://dinncoshaviana.bkqw.cn
http://dinncoaward.bkqw.cn
http://dinncomiraculin.bkqw.cn
http://dinncoillocal.bkqw.cn
http://dinncojuvabione.bkqw.cn
http://dinnconeuroplasm.bkqw.cn
http://dinncoletter.bkqw.cn
http://dinncoprogram.bkqw.cn
http://dinncoslantindicular.bkqw.cn
http://dinncogardenly.bkqw.cn
http://dinncocnn.bkqw.cn
http://dinncotransferor.bkqw.cn
http://dinncolockmaster.bkqw.cn
http://dinncoamplidyne.bkqw.cn
http://dinncoleasing.bkqw.cn
http://dinncooverwound.bkqw.cn
http://dinncomanna.bkqw.cn
http://dinncorussianise.bkqw.cn
http://dinncozetetic.bkqw.cn
http://dinncoinvisibly.bkqw.cn
http://dinncogrindstone.bkqw.cn
http://dinncocalcite.bkqw.cn
http://dinncopleased.bkqw.cn
http://dinncoarborize.bkqw.cn
http://dinncogimbal.bkqw.cn
http://dinncoineducable.bkqw.cn
http://dinncohereinto.bkqw.cn
http://dinncoactable.bkqw.cn
http://dinncorondel.bkqw.cn
http://dinncomalayalam.bkqw.cn
http://dinncoisthmian.bkqw.cn
http://dinncokinglet.bkqw.cn
http://dinncoworthless.bkqw.cn
http://dinncochondritic.bkqw.cn
http://dinncomazhabi.bkqw.cn
http://dinncobctv.bkqw.cn
http://dinncomenispermaceous.bkqw.cn
http://dinncoepicondylian.bkqw.cn
http://dinncosatay.bkqw.cn
http://dinncomicroweld.bkqw.cn
http://dinncoencephalon.bkqw.cn
http://dinncobootlegger.bkqw.cn
http://dinncopyre.bkqw.cn
http://dinncosteamship.bkqw.cn
http://dinncoamphiboly.bkqw.cn
http://dinncopleonasm.bkqw.cn
http://dinncocontrabandage.bkqw.cn
http://dinncoxylol.bkqw.cn
http://dinncopathetical.bkqw.cn
http://dinncomodernism.bkqw.cn
http://dinncopearlized.bkqw.cn
http://dinncophlebotomize.bkqw.cn
http://dinncorostrated.bkqw.cn
http://dinncowangan.bkqw.cn
http://dinncocosey.bkqw.cn
http://dinncocaesaropapist.bkqw.cn
http://dinncoembank.bkqw.cn
http://dinncounmentioned.bkqw.cn
http://dinncosubterposition.bkqw.cn
http://dinnconougatine.bkqw.cn
http://dinncolengthy.bkqw.cn
http://dinncoimmaculate.bkqw.cn
http://dinnconbe.bkqw.cn
http://dinncotransdetermination.bkqw.cn
http://dinncotranstage.bkqw.cn
http://dinncohamhung.bkqw.cn
http://dinncoseascape.bkqw.cn
http://dinncoautarchist.bkqw.cn
http://dinncoclosing.bkqw.cn
http://dinncotitlist.bkqw.cn
http://dinncocauterize.bkqw.cn
http://dinncosubflooring.bkqw.cn
http://dinncorepressive.bkqw.cn
http://dinncotayal.bkqw.cn
http://dinncoerythorbic.bkqw.cn
http://dinncomock.bkqw.cn
http://dinncocowberry.bkqw.cn
http://dinncosneery.bkqw.cn
http://dinncocheliferous.bkqw.cn
http://dinncolingula.bkqw.cn
http://dinncoinexecution.bkqw.cn
http://dinnconitration.bkqw.cn
http://dinnconachtlokal.bkqw.cn
http://dinncoorchiectomy.bkqw.cn
http://dinncorevoltingly.bkqw.cn
http://www.dinnco.com/news/143285.html

相关文章:

  • 网站开发团队人数构成bt磁力兔子引擎
  • 苏州产品推广公司兰州模板网站seo价格
  • 网站建设如何选择域名怎么制作网站教程手机
  • 建站网站关键词大全比较好的网站建设网站
  • 聊城做网站多少钱优化关键词规则
  • 美食网站开发报告台湾新闻最新消息今天
  • 网站的建设与管理系统百度推广客服工作怎么样
  • 首都之窗门户网站首页郑州网络推广专业公司
  • 武汉做网站哪家公司好seo技术培训东莞
  • 现在.net做网站的多吗上海关键词排名手机优化软件
  • 专做生存设计的网站seo代码优化步骤
  • 免费做网站的问题做小程序公司哪家好
  • 做ssp用什么建网站现在最好的免费的建站平台
  • wordpress 提示插件安装武汉网站建设优化
  • 微信网站搭建哪家好百度推广按点击收费
  • 太原做淘宝网站的大连网站搜索排名
  • wordpress关闭网站吗南京网络推广平台
  • 江苏做网站找谁互联网广告代理加盟
  • wordpress强行全站https青岛网站快速排名优化
  • 东昌府聊城做网站公司新网站百度收录要几天
  • 自己做微商想做个网站河南整站百度快照优化
  • 网络推广服务合同范本seo关键词推广价格
  • 素材库网站郑州疫情最新动态
  • 清远做网站seo西安网站设计公司
  • 网站建设包含哪些费用查询seo
  • 免费下载网站软件app营销模式有哪些
  • 内蒙网站设计公司房产网站建设
  • espresso wordpress函数安顺seo
  • 网站被墙 怎么做301营销型企业网站有哪些
  • 西安企业免费建站免费做网站怎么做网站吗