Home Database Mysql Tutorial 从索引技术谈数据库查询索引建立和查询条件书写

从索引技术谈数据库查询索引建立和查询条件书写

Jun 07, 2016 pm 03:42 PM
Advantage Establish technology database condition Inquire index

索引的优势当然是提高检索速度,但并不是说数据库建立了索引就真的会提高检索速度.为什么呢? 我们知道,索引本身是有序的,索引查找的时候一般是多分查找,(当然在内存用数组实现的索引则可以做到随机查找,但数据库一般很少会采用这种方式组织,一般都是利用B树),

       索引的优势当然是提高检索速度,但并不是说数据库建立了索引就真的会提高检索速度.为什么呢?

        我们知道,索引本身是有序的,索引查找的时候一般是多分查找,(当然在内存用数组实现的索引则可以做到随机查找,但数据库一般很少会采用这种方式组织,一般都是利用B+树),所以索引的查找一般不会是常数级,由于索引本身数据量问题,也不是一次就能将所有索引数据加载在内存里,所以也可能会引起多次磁盘读,加上定位到目标索引后还需要常数级的具体数据块磁盘读写,因此一次索引定位需要的磁盘读写可以控制在常数级别.因此索引查找的速度会在对数级别.但这并不等同于数据库查询时具体的查询速度,下面来分析一下:

1)只有建立索引的字段作为条件才会启用索引查询,提高速度;
2)如果索引字段的条件和其它非索引字段的条件是or关系,也会启用索引,但不会提高速度,因为这是的检索速度取决于慢条件;
3)索引字段在等值查询时效率最高(等条件),大于,小于等带范围的查询条件的速度是否能提高要看数据库索引本身的实现技术,因此数据库一般也不会采用B树而采用类似B+树的原因,因为B+树的卫星数据都在叶子节点上,可以实现范围读,提高范围查询的效率;
4)对于模糊查询,要看具体的数据库,一般是不会启用索引(Oracle 会做一定的优化,会用索引,具体可看后面的测试数据);
5)IS NULL,IS NOT NUL等条件也是一样.

因此,在数据库索引实际应用时要根据实际需要进行:
1)如果某个字段,或者某几个字段频繁单独作为条件查询时,可以建立索引;
2)如果一般字段多使用模糊查询,则不要建立索引;
3)索引字段条件和非索引字段只有在逻辑关系为与的情况下,索引字段条件才会真正有意义,否则还是全表扫描;

下面是Oracle的索引是否有效的一些条件:
1) 一般比较都会启用索引,In,between也会启用索引.
2)Like比较特殊,实际上Oracle会对第1个模糊匹配符号前面的部分串作索引定位匹配,具体的可参见后面的测试数据;
3) is null,is not null不会启用索引;

4)索引字段与常量表达式时Or关系时,常量表达式不会影响结果,但变量和参数化就会全表扫描;

下面是我测试的结果:数据量进2亿,服务器就是普通的PC机,Cards字段是索引字段,batchno字段是非索引字段,下面是结果:

select count(*) from cards;-- >52s
select count(1) from cards;-- >52s
select * from cards where cards='1';--不存在,毫秒
select * from cards where cards='994595942';--存在,毫秒级
select * from cards where cards>='999999999';--毫秒级,但也与返回数据量大小有关.索引有效.
select * from cards where cards>='9999' and  cards select * from cards where cards in('994595942','999906236');--毫秒秒,索引有效.
select * from cards where cards in( select '994595942' from dual);--毫秒级索引有效.
select * from cards where cards in('999906236');--0.156秒,索引有效.

select * from cards where cards > '999999999';--毫秒级,索引有效.
select * from cards where cards like '999999%';-->毫秒级,索引有效.但也与返回数据量大小有关
select * from cards where cards like '_999999_';--分钟级,索引无效
select * from cards where cards like '999999_';--毫秒级,索引有效.但也与返回数据量大小有关
select * from cards where cards like '_999999';--分钟级索引无效
select * from cards where cards like '999_999';--分钟级索引无效

select * from cards where cards is null;--分钟级索引无效
select * from cards where cards='994595942' or 1>1;--毫秒级,索引有效
update cards set batchno=batchno  where cards='994595942';--毫秒级,索引有效.
update cards set batchno=batchno  where cards='994595942' or i>1;--分钟级索引无效 i是变量
update cards set batchno=batchno  where cards='994595942' or :V >1;--分钟级索引无效 v是占位符号,其实就是一般参数化查询.

select * from cards where cards between '994595942' and '994595942';--毫秒级索引有效.
select * from cards where cards='994595942' or batchno='222';--索引有效,但没意义,还是全表扫描;
select * from cards where cards='994595942' and batchno='222';--索引有效,毫秒级
select * from cards where  batchno='222' and cards='994595942';--索引有效,毫秒级,说明Oracle有先进行索引字段处理的优化.


select * from cards where (batchno,cards) in (select '994595942','994595942' from dual);--毫秒级,索引有效.

PS:从上面的测试来看,Oracle的优化,特别是Like的优化非常到位,因为我原来还认为数据库不会对模糊查询利用索引.当然从上述测试也可以反推出一些Oracle索引存放的一些技术。

 

 

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)

Hot Topics

Java Tutorial
1657
14
PHP Tutorial
1257
29
C# Tutorial
1229
24
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

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

More than just 3D Gaussian! Latest overview of state-of-the-art 3D reconstruction techniques More than just 3D Gaussian! Latest overview of state-of-the-art 3D reconstruction techniques Jun 02, 2024 pm 06:57 PM

Written above & The author’s personal understanding is that image-based 3D reconstruction is a challenging task that involves inferring the 3D shape of an object or scene from a set of input images. Learning-based methods have attracted attention for their ability to directly estimate 3D shapes. This review paper focuses on state-of-the-art 3D reconstruction techniques, including generating novel, unseen views. An overview of recent developments in Gaussian splash methods is provided, including input types, model structures, output representations, and training strategies. Unresolved challenges and future directions are also discussed. Given the rapid progress in this field and the numerous opportunities to enhance 3D reconstruction methods, a thorough examination of the algorithm seems crucial. Therefore, this study provides a comprehensive overview of recent advances in Gaussian scattering. (Swipe your thumb up

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.

Revolutionary GPT-4o: Reshaping the human-computer interaction experience Revolutionary GPT-4o: Reshaping the human-computer interaction experience Jun 07, 2024 pm 09:02 PM

The GPT-4o model released by OpenAI is undoubtedly a huge breakthrough, especially in its ability to process multiple input media (text, audio, images) and generate corresponding output. This ability makes human-computer interaction more natural and intuitive, greatly improving the practicality and usability of AI. Several key highlights of GPT-4o include: high scalability, multimedia input and output, further improvements in natural language understanding capabilities, etc. 1. Cross-media input/output: GPT-4o+ can accept any combination of text, audio, and images as input and directly generate output from these media. This breaks the limitation of traditional AI models that only process a single input type, making human-computer interaction more flexible and diverse. This innovation helps power smart assistants

How to use database callback functions in Golang? How to use database callback functions in Golang? Jun 03, 2024 pm 02:20 PM

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.

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.

How to save JSON data to database in Golang? How to save JSON data to database in Golang? Jun 06, 2024 am 11:24 AM

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

See all articles