Home Database Mysql Tutorial 数据库系统概论-[04]索引

数据库系统概论-[04]索引

Jun 07, 2016 pm 03:59 PM
accelerate Establish database Inquire Overview system index speed

1、索引概述 建立索引是加快查询速度的有效手段,用户可以根据应用环境的需要,在基本表上建立一个或者多个索引,以提供多种存取路径,加快查找速度。 一般来说,建立与删除索引由数据库管理员DBA或者表的属主(owner),即建立表的人负责完成,系统在存取数据

1、索引概述

建立索引是加快查询速度的有效手段,用户可以根据应用环境的需要,在基本表上建立一个或者多个索引,以提供多种存取路径,加快查找速度。

一般来说,建立与删除索引由数据库管理员DBA或者表的属主(owner),即建立表的人负责完成,系统在存取数据时会自动选择合适的索引作为存取路径,用户不必也不能显示地选择索引。

2、索引优点和缺点

首先明白为什么创建索引,因为创建索引可以大大提高系统的性能。

通过创建唯一性索引,可以保证数据库表中每一行数据的唯一性。可以大大加快数据的检索速度,这也是创建索引的最主要的原因。可以加速表和表之间的连接,特别是在实现数据的参考完整性方面特别有意义。在使用分组和排序子句进行数据检索时,同样可以显著减少查询中分组和排序的时间。通过使用索引,可以在查询的过程中,使用优化隐藏器,提高系统的性能。增加索引有如此多的优点,为什么不对表中的每一个列创建一个索引呢?这种想法固然有其合理性,然而也有其片面性,虽然索引有许多的优点,但是,为表中的每一个列都增加索引,是非常不明智的,这是因为,增加索引也有许多不利的一个方面。 创建索引和维护索引要耗费时间,这种时间随着数据量的增加而增加。索引需要占物理空间,除了数据表占数据空间之外,每一个索引还要占一定的物理空间,如果要建立举聚簇索引,那么需要的空间就会更大。当对表中的数据进行增加、删除和修改的时候,索引也要动态的维护,这样就降低了数据的维护速度。索引是建立在数据库表中的某些列上,因此在创建索引的时候,应该仔细考虑在哪些列上可以创建索引,在哪些列上不能创建索引,一般来说,应该在这些列上创建索引:

1、在经常需要搜索的列上,可以加快搜索速度

2、在作为主键的列上,强制该列的唯一性和组织表中数据的排列结构。

3、在经常用在连接的列上,这些列主要是一些外键,可以加快连接的速度。

4、在经常需要根据范围进行搜索的列上创建索引,因为索引已经排序,其指定的范围是连续的。

5、在经常需要排序的列上创建索引,因为索引已经排序,这样查询可以利用索引的排序,加快排序查询时间。

6、在经常使用where子句中的列上面创建索引,加快条件的判断速度。 同样对于有些列是不应该创建索引的,一般来说,不应该创建索引的这些列具有下列特点:

1、对于那些在查询中很少使用或者参考的列不应该创建索引,这是因为,既然这些列很少使用到,因此有索引或者无索引,并不能提高查询速度,相反,由于增加了索引,反而降低了系统的维护速度和增加了空间需求。

2、对于那些只有很少数据值的列也不应该增加索引,这是因为,由于这些列的取值很少,比如人事表的性别列,在查询的结果中,结果集的数据行占了表中数据行的很大比例,即需要在表中搜索的数据行的比例很大,着呢国家索引,并不能明显加快检索速度。

3、对于那些定义为text、Image和bit数据类型的列不应该增加索引,这是因为,这些列的数据量要么相当大,要么取值很少。

4、当修改性能远远大于检索性能时,不应该创建索引,这是因为,修改性能和检索性能是互相矛盾的,当增加索引时,会提高检索性能,但是会降低修改性能,当减少索引时,会提高修改性能,降低检索性能,因此,当修改性能远远大于检索性能时,不应该创建索引。

3、创建索引 创建索引有多种方法,这些方法包括直接创建索引方法和间接创建索引的方法,直接创建索引,使用如下语句: CREATE [UNIQUE][CLUSTER][INDEX ON ([][,[]]......);说明:其中是要创建索引的基本表的名字,索引还可以建立在该表的一列或者多列上,各列之间用逗号分隔,每个后面还可以用指定索引值的排序次序,可选ASC(升序)或者DESC(降序),缺省值为ASC。 UNIQUE表明此索引的每一个索引值值对应唯一的数据记录。 CLUSTER表示要建立的索引是聚簇索引,所谓聚簇索引是指索引项的顺序与表中记录的物理顺序一致的索引组织。还有一种方法创建索引,在表中定义主键约束或者唯一约束时,同时也创建了索引,虽然,这两种方法都可以创建索引,但是它们创建索引的具体内容是有区别的:当在表上定义主键或者唯一约束时,如果表中已经有了使用CREATE INDEX语句创建的标准索引的,那么主键约束或者唯一性约束创建的索引覆盖以前创建的标准索引,也就是说主键约束或者唯一约束创建的索引的优先级高于使用CREATE INDEX语句创建的索引。 eg:为学生-课程数据库中的Student,Course,SC 3个表建立索引,其中Student表按学号升序建唯一索引,Course表按课程号升序建唯一索引,SC表按学号升序和课程号降序建唯一索引。 CREATE UNIQUE INDEX Stusno ON Student(Sno); CREATE UNIQUE INDEX Coucno ON Course(Cno); CREATE UNIQUE INDEX SCno ON SC(Sno ASC,Cno DESC);

4、删除索引 索引一经建立,就有系统使用和维护它,不需要用户的干预,建立索引是为了减少查询操作的时间,但如果数据增删改频繁,系统会花费许多时间来维护索引,从而降低了查询效率,这时可以删除一些不必要的索引。在SQL中,删除索引使用DROP INDEX 语句,其一般格式为: DROP INDEX ; eg:删除Student表的Stusname索引 DROP INDEX Stusname;删除索引时,系统会同时删除数据字典中删除有该索引的描述。

5、总结 在RDBMS中索引一般采用B+树、HASH索引来实现,B+树索引具有动态平衡的优点,HASH索引具有查找速度快的特点,索引是关系数据库的内部实现技术,属于内模式的范畴。用户使用CREATE INDEX语句定义索引时,可以定义索引是唯一索引、非唯一索引或者聚餐索引,至于某一个索引是采用B+树,还是HASH索引则由具体的RDBMS来决定。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Huawei's Qiankun ADS3.0 intelligent driving system will be launched in August and will be launched on Xiangjie S9 for the first time Huawei's Qiankun ADS3.0 intelligent driving system will be launched in August and will be launched on Xiangjie S9 for the first time Jul 30, 2024 pm 02:17 PM

On July 29, at the roll-off ceremony of AITO Wenjie's 400,000th new car, Yu Chengdong, Huawei's Managing Director, Chairman of Terminal BG, and Chairman of Smart Car Solutions BU, attended and delivered a speech and announced that Wenjie series models will be launched this year In August, Huawei Qiankun ADS 3.0 version was launched, and it is planned to successively push upgrades from August to September. The Xiangjie S9, which will be released on August 6, will debut Huawei’s ADS3.0 intelligent driving system. With the assistance of lidar, Huawei Qiankun ADS3.0 version will greatly improve its intelligent driving capabilities, have end-to-end integrated capabilities, and adopt a new end-to-end architecture of GOD (general obstacle identification)/PDP (predictive decision-making and control) , providing the NCA function of smart driving from parking space to parking space, and upgrading CAS3.0

12306 How to check historical ticket purchase records How to check historical ticket purchase records 12306 How to check historical ticket purchase records How to check historical ticket purchase records Mar 28, 2024 pm 03:11 PM

Download the latest version of 12306 ticket booking app. It is a travel ticket purchasing software that everyone is very satisfied with. It is very convenient to go wherever you want. There are many ticket sources provided in the software. You only need to pass real-name authentication to purchase tickets online. All users You can easily buy travel tickets and air tickets and enjoy different discounts. You can also start booking reservations in advance to grab tickets. You can book hotels or special car transfers. With it, you can go where you want to go and buy tickets with one click. Traveling is simpler and more convenient, making everyone's travel experience more comfortable. Now the editor details it online Provides 12306 users with a way to view historical ticket purchase records. 1. Open Railway 12306, click My in the lower right corner, and click My Order 2. Click Paid on the order page. 3. On the paid page

How to check your academic qualifications on Xuexin.com How to check your academic qualifications on Xuexin.com Mar 28, 2024 pm 04:31 PM

How to check my academic qualifications on Xuexin.com? You can check your academic qualifications on Xuexin.com, but many users don’t know how to check their academic qualifications on Xuexin.com. Next, the editor brings you a graphic tutorial on how to check your academic qualifications on Xuexin.com. Interested users come and take a look! Xuexin.com usage tutorial: How to check your academic qualifications on Xuexin.com 1. Xuexin.com entrance: https://www.chsi.com.cn/ 2. Website query: Step 1: Click on the Xuexin.com address above to enter the homepage Click [Education Query]; Step 2: On the latest webpage, click [Query] as shown by the arrow in the figure below; Step 3: Then click [Login Academic Credit File] on the new page; Step 4: On the login page Enter the information and click [Login];

Always new! Huawei Mate60 series upgrades to HarmonyOS 4.2: AI cloud enhancement, Xiaoyi Dialect is so easy to use Always new! Huawei Mate60 series upgrades to HarmonyOS 4.2: AI cloud enhancement, Xiaoyi Dialect is so easy to use Jun 02, 2024 pm 02:58 PM

On April 11, Huawei officially announced the HarmonyOS 4.2 100-machine upgrade plan for the first time. This time, more than 180 devices will participate in the upgrade, covering mobile phones, tablets, watches, headphones, smart screens and other devices. In the past month, with the steady progress of the HarmonyOS4.2 100-machine upgrade plan, many popular models including Huawei Pocket2, Huawei MateX5 series, nova12 series, Huawei Pura series, etc. have also started to upgrade and adapt, which means that there will be More Huawei model users can enjoy the common and often new experience brought by HarmonyOS. Judging from user feedback, the experience of Huawei Mate60 series models has improved in all aspects after upgrading HarmonyOS4.2. Especially Huawei M

How does Hibernate implement polymorphic mapping? How does Hibernate implement polymorphic mapping? Apr 17, 2024 pm 12:09 PM

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

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

Huawei will launch the Xuanji sensing system in the field of smart wearables, which can assess the user's emotional state based on heart rate Huawei will launch the Xuanji sensing system in the field of smart wearables, which can assess the user's emotional state based on heart rate Aug 29, 2024 pm 03:30 PM

Recently, Huawei announced that it will launch a new smart wearable product equipped with Xuanji sensing system in September, which is expected to be Huawei's latest smart watch. This new product will integrate advanced emotional health monitoring functions. The Xuanji Perception System provides users with a comprehensive health assessment with its six characteristics - accuracy, comprehensiveness, speed, flexibility, openness and scalability. The system uses a super-sensing module and optimizes the multi-channel optical path architecture technology, which greatly improves the monitoring accuracy of basic indicators such as heart rate, blood oxygen and respiration rate. In addition, the Xuanji Sensing System has also expanded the research on emotional states based on heart rate data. It is not limited to physiological indicators, but can also evaluate the user's emotional state and stress level. It supports the monitoring of more than 60 sports health indicators, covering cardiovascular, respiratory, neurological, endocrine,

An in-depth analysis of how HTML reads the database An in-depth analysis of how HTML reads the database Apr 09, 2024 pm 12:36 PM

HTML cannot read the database directly, but it can be achieved through JavaScript and AJAX. The steps include establishing a database connection, sending a query, processing the response, and updating the page. This article provides a practical example of using JavaScript, AJAX and PHP to read data from a MySQL database, showing how to dynamically display query results in an HTML page. This example uses XMLHttpRequest to establish a database connection, send a query and process the response, thereby filling data into page elements and realizing the function of HTML reading the database.

See all articles