search
HomeDatabaseMysql TutorialOracle 索引简单总结

建立索引时先进行排序,逻辑上分为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
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
What are the different storage engines available in MySQL?What are the different storage engines available in MySQL?Apr 26, 2025 am 12:27 AM

MySQLoffersvariousstorageengines,eachsuitedfordifferentusecases:1)InnoDBisidealforapplicationsneedingACIDcomplianceandhighconcurrency,supportingtransactionsandforeignkeys.2)MyISAMisbestforread-heavyworkloads,lackingtransactionsupport.3)Memoryengineis

What are some common security vulnerabilities in MySQL?What are some common security vulnerabilities in MySQL?Apr 26, 2025 am 12:27 AM

Common security vulnerabilities in MySQL include SQL injection, weak passwords, improper permission configuration, and unupdated software. 1. SQL injection can be prevented by using preprocessing statements. 2. Weak passwords can be avoided by forcibly using strong password strategies. 3. Improper permission configuration can be resolved through regular review and adjustment of user permissions. 4. Unupdated software can be patched by regularly checking and updating the MySQL version.

How can you identify slow queries in MySQL?How can you identify slow queries in MySQL?Apr 26, 2025 am 12:15 AM

Identifying slow queries in MySQL can be achieved by enabling slow query logs and setting thresholds. 1. Enable slow query logs and set thresholds. 2. View and analyze slow query log files, and use tools such as mysqldumpslow or pt-query-digest for in-depth analysis. 3. Optimizing slow queries can be achieved through index optimization, query rewriting and avoiding the use of SELECT*.

How can you monitor MySQL server health and performance?How can you monitor MySQL server health and performance?Apr 26, 2025 am 12:15 AM

To monitor the health and performance of MySQL servers, you should pay attention to system health, performance metrics and query execution. 1) Monitor system health: Use top, htop or SHOWGLOBALSTATUS commands to view CPU, memory, disk I/O and network activities. 2) Track performance indicators: monitor key indicators such as query number per second, average query time and cache hit rate. 3) Ensure query execution optimization: Enable slow query logs, record and optimize queries whose execution time exceeds the set threshold.

Compare and contrast MySQL and MariaDB.Compare and contrast MySQL and MariaDB.Apr 26, 2025 am 12:08 AM

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

How does MySQL's licensing compare to other database systems?How does MySQL's licensing compare to other database systems?Apr 25, 2025 am 12:26 AM

MySQL uses a GPL license. 1) The GPL license allows the free use, modification and distribution of MySQL, but the modified distribution must comply with GPL. 2) Commercial licenses can avoid public modifications and are suitable for commercial applications that require confidentiality.

When would you choose InnoDB over MyISAM, and vice versa?When would you choose InnoDB over MyISAM, and vice versa?Apr 25, 2025 am 12:22 AM

The situations when choosing InnoDB instead of MyISAM include: 1) transaction support, 2) high concurrency environment, 3) high data consistency; conversely, the situation when choosing MyISAM includes: 1) mainly read operations, 2) no transaction support is required. InnoDB is suitable for applications that require high data consistency and transaction processing, such as e-commerce platforms, while MyISAM is suitable for read-intensive and transaction-free applications such as blog systems.

Explain the purpose of foreign keys in MySQL.Explain the purpose of foreign keys in MySQL.Apr 25, 2025 am 12:17 AM

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

See all articles

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

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.