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

SQL Indexes(索引)

目录

Indexes

Using Clustered Indexes

Using Nonclustered Indexes

Declaring Indexes

Using Indexes

Finding Rows Without Indexes

Finding Rows in a Heap with a Nonclustered Index

Finding Rows in a Clustered Index

Finding Rows in a Clustered Index with a Nonclustered Index

总结:

Database Tuning


Indexes

  • Index = data structure used to speed access to tuples of a relation, given values of one or more attributes.(用于给定一个或者多个属性后加速对于关系的访问)
  • Could be a hash table, but in a DBMS it is always a balanced search tree with giant nodes (a full disk page) called a B+ tree.

Using Clustered Indexes

  • Each Table Can Have Only One Clustered Index(每张表只能有一个聚焦索引)
  • The Physical Row Order of the Table and the Order of Rows in the Index Are the Same(物理储存顺序与聚焦索引的顺序一样)
  • Key Value Uniqueness Is Maintained Explicitly or Implicitly(键值的维护是显式或者隐式的)

Using Nonclustered Indexes

  • Nonclustered Indexes Are the SQL Server Default(非聚焦索引是SQL_Server的默认索引类型)
  • Existing Nonclustered Indexes Are Automatically Rebuilt When:
  1. An existing clustered index is dropped
  2. A clustered index is created
  3. The DROP_EXISTING option is used to change which columns define the clustered index

以上情况会导致非聚焦索引会重建:

  1. 已存在的聚焦索引被删除(会导致非聚焦索引储存的数据指针失效,所以需要重建)
  2. 新的聚焦索引被创建
  3. 修改了聚焦索引的列(可能会导致物理储存顺序重新排列)

总结来说的话就是,影响了物理储存顺序就可能会导致非聚焦索引的顺序失效

Declaring Indexes

No standard!

Typical syntax:

CREATE INDEX BeerInd ON
Beers(manf);
CREATE INDEX SellInd ON
Sells(bar, beer);

Using Indexes

  • Given a value v, the index takes us to only those tuples that have v in the attribute(s) of the index.(通过给定的值v,能够快速找到包含v属性的元组)

  • Example: use BeerInd and SellInd to find the prices of beers manufactured by Pete’s and sold by Joe.

SELECT price FROM Beers, Sells
WHERE manf = ’Pete’’s’ AND
Beers.name = Sells.beer AND
bar = ’Joe’’s Bar’;
  • Use BeerInd to get all the beers made by Pete’s.

  • Then use SellInd to get prices of those beers, with bar = ’Joe’’s Bar’

Finding Rows Without Indexes

Finding Rows in a Heap with a Nonclustered Index

Finding Rows in a Clustered Index

Finding Rows in a Clustered Index with a Nonclustered Index

总结:

  • 非聚集索引的叶子结点存放的是聚集索引的关键字,聚集索引叶子结点存放的是数据本身
  • 所以使用非聚焦索引查询数据时,应该是先找到非聚焦索引的叶子结点上的聚焦索引的关键字,然后通过这个关键字,从聚焦索引找到存放了数据的叶子结点

Database Tuning

  • A major problem in making a database run fast is deciding which indexes to create.(一个主要的问题就是,要决定那些索引是需要被创建的)
  • Pro: An index speeds up queries that can use it.(优点:能够提升查询的速度)
  • Con: An index slows down all modifications on its relation because the index must be modified too.(缺点:是的关系上的修改效率降低,因为修改的同时索引也需要修改)
http://www.lryc.cn/news/2398739.html

相关文章:

  • Axure 基础入门
  • 结构型设计模式之Decorator(装饰器)
  • HCIP-Datacom Core Technology V1.0_3 OSPF基础
  • 工作自动化——工作自动提炼--智能编程——仙盟创梦IDE
  • go语言学习 第 2 章:变量与数据类型
  • 大语言模型评测体系全解析(上篇):基础框架与综合评测平台
  • Spring Event(事件驱动机制)
  • Fisher准则例题——给定类内散度矩阵和类样本均值
  • MySQL数据库中INNODB表数据的备份与恢复
  • 振动分析师(ISO18436-2)四级能力矩阵 - 简介
  • 生产环境MYSQL常见锁表场景
  • 结构性设计模式之Composite(组合)
  • Java面试八股--04-MySQL
  • 日语学习-日语知识点小记-构建基础-JLPT-N4阶段(31):そう
  • 设计模式——访问者设计模式(行为型)
  • 实验设计与分析(第6版,Montgomery著,傅珏生译) 第10章拟合回归模型10.9节思考题10.1 R语言解题
  • 《对象创建的秘密:Java 内存布局、逃逸分析与 TLAB 优化详解》
  • LeetCode 高频 SQL 50 题(基础版) 之 【高级查询和连接】· 下
  • Java并发编程:读写锁与普通互斥锁的深度对比
  • Spring Boot Actuator未授权访问漏洞修复
  • 机器学习——SVM
  • 【音视频】FFmpeg 硬件(NVDIA)编码H264
  • 贪心算法应用:超图匹配问题详解
  • OpenCV CUDA模块结构分析与形状描述符------计算指定阶数的矩(Moments)所需的总数量函数:numMoments
  • 【Web应用】若依框架:基础篇13 源码阅读-前端代码分析
  • [java八股文][JavaSpring面试篇]SpringCloud
  • 深度学习篇---face-recognition的优劣点
  • 基于分布式状态机的集装箱智能道口软件架构方法
  • Oracle的Hint
  • 手动事务的使用