Select*一定不走索引是否正确?
走索引指的是:SQL语句的执行计划用到了1、聚集索引查找 2、索引查找 ,并且查询语句中需要有where子句 根据where子句的过滤条件,去聚集索引或非聚集索引那里查找记录 一张表只有一列的情况: 聚集索引 USE [tempdb] GO CREATE TABLE t1 ( id INT ) GO CREA
走索引指的是:SQL语句的执行计划用到了1、聚集索引查找 2、索引查找 ,并且查询语句中需要有where子句
根据where子句的过滤条件,去聚集索引或非聚集索引那里查找记录
一张表只有一列的情况:
聚集索引

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
非聚集索引

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
只有一列,肯定会走索引的
一张表有多列的情况
分三种情况:
1、只有聚集索引
2、只有非聚集索引
3、有聚集索引和非聚集索引
只有聚集索引

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
小结:
只有聚集索引的表:如果where后面不包括创建聚集索引的时候的第一个字段,就会使用聚集索引扫描
下面SQL语句会使用聚集索引查找,因为包括了创建聚集索引的时候的第一个字段
1 |
|
只有非聚集索引

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
小结:
只有非聚集索引的表:如果where后面不包括创建非聚集索引的时候的第一个字段,就会使用表扫描或者索引扫描
下面SQL语句会使用非聚集索引查找,因为包括了创建非聚集索引的时候的第一个字段
1 |
|
有聚集索引也有非聚集索引



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
小结:
有聚集索引和非聚集索引的表:如果where后面包括创建聚集索引的时候的第一个字段,就会使用聚集索引查找
如果where后面包括创建非聚集索引的时候的第一个字段但不包括创建聚集索引的时候的第一个字段,就会使用索引查找
如果where后面不包括创建非聚集索引的时候的第一个字段和不包括创建聚集索引的时候的第一个字段,就会使用聚集索引扫描
1 |
|
总结
其实走不走索引,关键取决于where后面包括还是不包括
创建聚集索引的时候的第一个字段
创建非聚集索引的时候的第一个字段
跟select *没有关系的,select * 最大的影响就是额外的IO开销
像“键查找” ,“RID查找”这些运算符就是额外的开销
键查找:到聚集索引里找其他字段的值
RID查找:到堆表里找其他字段的值

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

AI Hentai Generator
Generate AI Hentai for free.

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



Oracle index types include: 1. B-Tree index; 2. Bitmap index; 3. Function index; 4. Hash index; 5. Reverse key index; 6. Local index; 7. Global index; 8. Domain index ; 9. Bitmap connection index; 10. Composite index. Detailed introduction: 1. B-Tree index is a self-balancing tree data structure that can efficiently support concurrent operations. In Oracle database, B-Tree index is the most commonly used index type; 2. Bit Graph index is an index type based on bitmap algorithm and so on.

Asynchronous processing method of SelectChannelsGo concurrent programming using golang Introduction: Concurrent programming is an important area in modern software development, which can effectively improve the performance and responsiveness of applications. In the Go language, concurrent programming can be implemented simply and efficiently using Channels and Select statements. This article will introduce how to use golang for asynchronous processing methods of SelectChannelsGo concurrent programming, and provide specific

jQuery is a popular JavaScript library that can be used to simplify DOM manipulation, event handling, animation effects, etc. In web development, we often encounter situations where we need to change event binding on select elements. This article will introduce how to use jQuery to bind select element change events, and provide specific code examples. First, we need to create a dropdown menu with options using labels:

The solutions are: 1. Check whether the index value is correct: first confirm whether your index value exceeds the length range of the array. The index of the array starts from 0, so the maximum index value should be the array length minus 1; 2. Check the loop boundary conditions: If you use the index for array access in a loop, make sure the loop boundary conditions are correct; 3. Initialize the array: Before using an array, make sure that the array has been initialized correctly; 4. Use exception handling: You can use the exception handling mechanism in the program to catch errors where the index exceeds the bounds of the array, and handle it accordingly.

How to improve the efficiency of data grouping and data aggregation in PHP and MySQL through indexes? Introduction: PHP and MySQL are currently the most widely used programming languages and database management systems, and are often used to build web applications and process large amounts of data. Data grouping and data aggregation are common operations when processing large amounts of data, but if indexes are not designed and used appropriately, these operations can become very inefficient. This article will introduce how to use indexes to improve the efficiency of data grouping and data aggregation in PHP and MySQL, and improve

This article will explain in detail how PHP returns the string from the start position to the end position of a string in another string. The editor thinks it is quite practical, so I share it with you as a reference. I hope you will finish reading this article. You can gain something from this article. Use the substr() function in PHP to extract substrings from a string. The substr() function can extract characters within a specified range from a string. The syntax is as follows: substr(string,start,length) where: string: the original string from which the substring is to be extracted. start: The index of the starting position of the substring (starting from 0). length (optional): The length of the substring. If not specified, then

The basic syntax of slicing in Python is to use the [start:end:step] syntax for slicing operations, where start represents the starting position of the slice, end represents the end position of the slice, and step represents the slicing step. If start is omitted, it means slicing from the beginning of the list or string; if end is omitted, it means slicing to the end of the list or string; if step is omitted, it means the step size is 1. For example: my_list=[1,2,3,4,5]#Cut from the 2nd element to the 4th element (excluding the 4th element) sub_list=my_list[1:4]#[2,3,4 ]#Start from the first element until the end of the list sub_li

Bluetooth headset is one of the indispensable devices in modern people's daily life. Its wireless connection and portable design allow us to enjoy a more free and convenient experience when enjoying music, calls and sports. However, many people have a common question when using Bluetooth headsets, and that is how to charge Bluetooth headsets correctly to ensure their performance and lifespan. Below, I will introduce you to the correct charging method of Bluetooth headsets in detail. First, it’s important to choose the right charging adapter and cable. Bluetooth headsets usually come with a charging box that has a charging
