数据库索引及基本优化入门
数据库索引及基本优化入门 2013-7-26 一前言 经常在 面试中发现很多人工作了好多年了,项目经验也不少,用过各种数据库,但大都不知道这些SQL语句背后的基本原理,更别说数据库优化了。平时做项目只知道实现功能,懒得学习,懒得思考,懒得看书(其实本人也
数据库索引及基本优化入门
2013-7-26
一 前言
经常在面试中发现很多人工作了好多年了,项目经验也不少,用过各种数据库,但大都不知道这些SQL语句背后的基本原理,更别说数据库优化了。平时做项目只知道实现功能,懒得学习,懒得思考,懒得看书(其实本人也是,不要找借口说这是China国情,项目是给boss做的,但技术和成长是你自己的)。
本篇文章主要讲述数据库索引的基本原理,及基本的数据库优化的知识。所有知识均为本人自己学习的总结以及网络。此篇文章主要是为公司内部人员培训所用的,整理出来只是希望和大家分享、交流,因本人技术有限,若有遗漏、错误,希望多多指正、交流。
二.基础知识 2.1 页
数据库文件存储是已页为存储单元的,一个页是8K(8192Byte),一个页就可以存放N行数据。我们常用的页类型就是数据页和索引页。一个页中除了存放基本数据之外还需要存放一些其他的数据,如页的信息、偏移量等,如下图所示。
虽然SQLServer是以页为单位存储数据,但是其分配空间是以一个盘区为单位的(8个页=64K),这样做的目的主要是为提高I/O的性能。
B树即二叉搜索树,所有非叶子节点最低拥有两个子节点,基本信息如下图所示。都是小的元素放左边,大的元素放右边。比如说要查找某个元素,其时间复杂度就对应该元素的深度,如要查询9,从根节点开始,只要比较三次就找到他了,其查询效率是非常高的。
子节点:最多两个子节点(指针分别指向Left和Right)
阶数(节点子节点个数):2
深度:就是层数,各个叶子节点不一定一样,如节点21的深度为4,40的深度为3
B-树是一中多路搜索树,其阶数可以自定义(>2),是很多数据及文件系统应用的一种索引结构,基本特征如:
1) 阶数(M)>2,即孩子数量大于2个
2) 每个结点存放至少M/2-1(取上整)和至多M-1个关键字;(至少2个关键字)
3) 非叶子结点上的多个关键字是按照顺序排列的:K[1], K[2], …, K[M-1];且K[i]
4) 所有叶子节点都位于同一层,因此叶子节点的深度都是一样的
5) 非叶子结点的关键字个数=指向儿子的指针个数-1;
6) 非叶子结点的指针:P[1], P[2], …, P[M];其中P[1]指向关键字小于K[1]的子树,P[M]指向关键字大于K[M-1]的子树,其它P[i]指向关键字属于(K[i-1], K[i])的子树;
如下图是一个三阶的B-树,节点[18]有两个指针分别指向其2个子节点。
这时如果要插入一个值17,其处理步骤:
1) 从根节点进入,17小于22,进入左边的节点[18];
2) [18]不是叶子节点,继续向下搜索,17小于18,进入其左边的子节点[12,16];
3) [12,16]为叶子节点,插入到该节点;
4) 节点[12,16,17]元素大于2了(3阶树的节点关键字数量应>3/2-1,
5) 12,17分裂成了两个子节点了;
分裂后的效果如下图
以上图片效果来自一个外国大学里面的的在线版B-树的测试,网站:~galles/visualization/BTree.html ,大家可以去这个网站测试,效果很直观,外国人就是牛。本人以前用C#+GDI实现过类似的效果,结果还是可以的,就是当树太大的时候,布局不好处理了。
2.3 B+树
B+树是B-树的变体,也是一种多路搜索树,一棵m 阶的B+树和m 阶的B-树的差异在于:
l 非叶子节点的子节点和其关键字相同,即节点有三个元素(关键字),他就肯定有三个子节点;
l 非叶子节点的子节点P[i],指向关键字值属于[K[i], K[i+1])的子树(B-树是开区间);
l 所有叶子节点增加一个链指针;
l 所有关键字的数据都在叶子节点中;
如下图所示,图片来自网络()。
三 索引存储
B+树和B-树是数据库广发应用的索引存储结构,它可以极大的提高数据查找的效率。关于B-树、B+树的原理与应用的详细可以参考文档:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Kimi: In just one sentence, in just ten seconds, a PPT will be ready. PPT is so annoying! To hold a meeting, you need to have a PPT; to write a weekly report, you need to have a PPT; to make an investment, you need to show a PPT; even when you accuse someone of cheating, you have to send a PPT. College is more like studying a PPT major. You watch PPT in class and do PPT after class. Perhaps, when Dennis Austin invented PPT 37 years ago, he did not expect that one day PPT would become so widespread. Talking about our hard experience of making PPT brings tears to our eyes. "It took three months to make a PPT of more than 20 pages, and I revised it dozens of times. I felt like vomiting when I saw the PPT." "At my peak, I did five PPTs a day, and even my breathing was PPT." If you have an impromptu meeting, you should do it

In the early morning of June 20th, Beijing time, CVPR2024, the top international computer vision conference held in Seattle, officially announced the best paper and other awards. This year, a total of 10 papers won awards, including 2 best papers and 2 best student papers. In addition, there were 2 best paper nominations and 4 best student paper nominations. The top conference in the field of computer vision (CV) is CVPR, which attracts a large number of research institutions and universities every year. According to statistics, a total of 11,532 papers were submitted this year, and 2,719 were accepted, with an acceptance rate of 23.6%. According to Georgia Institute of Technology’s statistical analysis of CVPR2024 data, from the perspective of research topics, the largest number of papers is image and video synthesis and generation (Imageandvideosyn

We know that LLM is trained on large-scale computer clusters using massive data. This site has introduced many methods and technologies used to assist and improve the LLM training process. Today, what we want to share is an article that goes deep into the underlying technology and introduces how to turn a bunch of "bare metals" without even an operating system into a computer cluster for training LLM. This article comes from Imbue, an AI startup that strives to achieve general intelligence by understanding how machines think. Of course, turning a bunch of "bare metal" without an operating system into a computer cluster for training LLM is not an easy process, full of exploration and trial and error, but Imbue finally successfully trained an LLM with 70 billion parameters. and in the process accumulate

Editor of the Machine Power Report: Yang Wen The wave of artificial intelligence represented by large models and AIGC has been quietly changing the way we live and work, but most people still don’t know how to use it. Therefore, we have launched the "AI in Use" column to introduce in detail how to use AI through intuitive, interesting and concise artificial intelligence use cases and stimulate everyone's thinking. We also welcome readers to submit innovative, hands-on use cases. Video link: https://mp.weixin.qq.com/s/2hX_i7li3RqdE4u016yGhQ Recently, the life vlog of a girl living alone became popular on Xiaohongshu. An illustration-style animation, coupled with a few healing words, can be easily picked up in just a few days.

Retrieval-augmented generation (RAG) is a technique that uses retrieval to boost language models. Specifically, before a language model generates an answer, it retrieves relevant information from an extensive document database and then uses this information to guide the generation process. This technology can greatly improve the accuracy and relevance of content, effectively alleviate the problem of hallucinations, increase the speed of knowledge update, and enhance the traceability of content generation. RAG is undoubtedly one of the most exciting areas of artificial intelligence research. For more details about RAG, please refer to the column article on this site "What are the new developments in RAG, which specializes in making up for the shortcomings of large models?" This review explains it clearly." But RAG is not perfect, and users often encounter some "pain points" when using it. Recently, NVIDIA’s advanced generative AI solution

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.
