Home Database Mysql Tutorial Oracle 索引简单总结

Oracle 索引简单总结

Jun 07, 2016 pm 05:34 PM
oracle index database

建立索引时先进行排序,逻辑上分为Root(根块),Branch(茎块),leaf(叶子块)。leaf中存在索引列的值、长度、和所在rowid,茎块存了

建立索引时先进行排序,逻辑上分为Root(根块),Branch(茎块),leaf(叶子块)。leaf中存在索引列的值、长度、和所在rowid,茎块存了指向具体茎块的指针,root块同理。这是逻辑上的划分实际上根据表中数据量的多少可能会存在多层,但是索引整体上层次较低,例如一张500G的表数据量达到几百亿条,这时候它的索引只有6层。

在查询结果返回多的情况下使用索引会浪费更多的代价,另外如果一张表有五个字段,我们在三个字段上建上btree索引。那样效率只会更低。可考虑使用位图索引

索引的三大特点:
1.索引数的高度一般较低
2.索引由索引列存储的值及rowid组成
 索引SELECT * FROM T WHERE ID = 1会导致索引回表的产生,若不需要看全部数据可用SELECT ID FROM T WHERE ID= 1或可建多列的复核索引,但是复核索引最好不要超过3列的复核。在更新操作不频繁的情况下可考虑使用索引组织表
3.索引本身是有序的
 减少ORDER BY、DISTINCT排序所浪费的COST
聚合因子:
建立索引的列的顺序与索引自动排序的列的顺序的不对应度被称为聚合因子,聚合因子越大索引回表读越浪费时间(索引回表读不可避免的情况下)
Oracle 执行计划分类:
1、TABLE ACCESS FULL  全表扫描
2、INDEX FAST FULL SCAN  索引快速扫描  (不考虑排序COUNT(ID), SUM(ID), AVG(ID),列必须非空或IS NOT NULL)
3、INDEX FULL SCAN  索引全扫  (SELECT ID FROM T WHERE ID = 120)
4、INDEX FULL SCAN(MIN/MAX)  最大值最小值索引全扫  (SELECT MAX(ID) FROM T)
5、TABLE ACCESS BY INDEX ROWID  索引回表读  (SELECT * FROM T WHERE ID = 1)
6、INDEX RANGE SCAN  索引范围扫描  (SELECT * FROM T WHERE ID 7、BITMAP INDEX FAST FULL SCAN  位图索引快速扫描  (不考虑排序COUNT(ID), SUM(ID), AVG(ID),位图快速索引扫描速度非常快将近是普通索引的百倍.列不必非空)
 
btree索引优化简介
T表  字段:OBJECT_ID, OBJECT_NAME
单列索引:
CREATE INDEX IDX1_OBJECT_ID ON T(OBJECT_ID);
COUNT(*)、SUM、AVG优化:
改为COUNT(OBJECT_ID)查询,但是必须保证OBJECT_ID列非空。可用如下方法告知oracle可走IDX1_OBJECT_ID索引:
1、SELECT COUNT(OBJECT_ID) FROM T WHERE OBJECT_ID IS NOT NULL;
2、设置字段非空
MAX/MIN优化:
根据第三个特性有序排列,所以MAX/MIN的查询代价会非常小。
SELECT MAX(OBJECT_ID) FROM T; 不需加上IS NOT NULL; 使用执行计划:INDEX FULL SCAN(MIN/MAX);
ORDER BY、DISTINCT排序优化:
SELECT * FROM T WHERE OBJECT_ID 未建立索引的情况下会进行排序产生TEMPSPC;
建立索引的情况下不需要产生排序 会使用IDX1_OBJECT_ID索引
 
 
位图索引
创建语法:
CREATE BITMAP INDEX IDX_BITM_T_STATUS ON T(STATUS);
适用在更新非常少的表,建立在重复度较高的列(性别)
存储结构:
位图索引存储的是比特位值
 
 
 
函数索引:
CREATE TABLE T AS SELECT * FROM DBA_OBJECT;
CREATE INDEX IDX_OBJECT_ID ON T(OBJECT_ID);
CREATE INDEX IDX_OBJECT_NAME ON T(OBJECT_NAME);
CREATE INDEX IDX_CREATED ON T(CREATED);
SELECT * FROM T WHERE UPPER(OBJECT_NAME) = 'T';
普通的BTREE索引,如果在对列做运算的条件下是无法使用索引查询的,,会使用TABLE ACCESS FULL;
创建语句:
CREATE INDEX IDX_UPPER_OBJ_NAME ON(UPPER(OBJECT_NAME));
SELECT INDEX_NAME, INDEX_TYPE FROM USER_INDEXES WHERE TABLE_NAME = 'T';
函数索引的TYPE是:FUNCTION-BASED NORMAL;
函数索引的cost比全表扫描要小,但是比普通的索引要大的多。
SELECT * FROM T WHERE OBJECT_ID - 10这时候如果在object_id列建立普通索引时无法使用的。oracle会默认使用全表扫描的方式进行查询。可有以下两个思路进行优化:
1、SELECT * FROM T WHERE OBEJCT_ID 2、在OBJECT_ID - 10上建立函数索引
写sql时要注意规范,很多语句是等价的。
SELECT * FROM T WHERE SUBSTR(OBJECT_NAME,1,4) = 'CLUS'  =    SELECT * FROM T WHERE OBJECT_NAME LIKE 'CLUS%';
SELECT * FROM T WHERE TRUNC(CREATED) >= TO_DATE('2012-10-02', 'YYYY-MM-DD') AND TRUNC(CREATED) =
SELECT * FROM T WHERE CREATED >= TO_DATE('2012-10-02', 'YYYY-MM-DD') AND CREATED

相关阅读:

由Oracle索引来理解ArcSDE索引

Oracle索引技术之如何建立最佳索引

Oracle索引列NULL值引发执行计划该表的测试示例

Oracle索引 主键影响查询速度

Oracle索引扫描

linux

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

Video Face Swap

Video Face Swap

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

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)

How does Go language implement the addition, deletion, modification and query operations of the database? How does Go language implement the addition, deletion, modification and query operations of the database? Mar 27, 2024 pm 09:39 PM

Go language is an efficient, concise and easy-to-learn programming language. It is favored by developers because of its advantages in concurrent programming and network programming. In actual development, database operations are an indispensable part. This article will introduce how to use Go language to implement database addition, deletion, modification and query operations. In Go language, we usually use third-party libraries to operate databases, such as commonly used sql packages, gorm, etc. Here we take the sql package as an example to introduce how to implement the addition, deletion, modification and query operations of the database. Assume we are using a MySQL database.

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

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.

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

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())

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.

How to handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

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.

Tips and practices for handling Chinese garbled characters in databases with PHP Tips and practices for handling Chinese garbled characters in databases with PHP Mar 27, 2024 pm 05:21 PM

PHP is a back-end programming language widely used in website development. It has powerful database operation functions and is often used to interact with databases such as MySQL. However, due to the complexity of Chinese character encoding, problems often arise when dealing with Chinese garbled characters in the database. This article will introduce the skills and practices of PHP in handling Chinese garbled characters in databases, including common causes of garbled characters, solutions and specific code examples. Common reasons for garbled characters are incorrect database character set settings: the correct character set needs to be selected when creating the database, such as utf8 or u

How to connect to remote database using Golang? How to connect to remote database using Golang? Jun 01, 2024 pm 08:31 PM

Through the Go standard library database/sql package, you can connect to remote databases such as MySQL, PostgreSQL or SQLite: create a connection string containing database connection information. Use the sql.Open() function to open a database connection. Perform database operations such as SQL queries and insert operations. Use defer to close the database connection to release resources.

See all articles