Home Database Mysql Tutorial Select*一定不走索引是否正确?

Select*一定不走索引是否正确?

Jun 07, 2016 pm 04:06 PM
select correct index

走索引指的是: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

USE [tempdb]

GO

CREATE TABLE t1 ( id INT )

GO

CREATE CLUSTERED INDEX CIX_T1 ON [dbo].[t1](ID ASC)

GO

 

 

DECLARE @I INT

SET @I = 1

WHILE @I < 1000

    BEGIN

        INSERT  INTO [dbo].[t1] ( [id] )

                SELECT  @I

        SET @I = @I &#43; 1

    END

 

SELECT * FROM [dbo].[t1] WHERE [id]=20

Copy after login
View Code

\

非聚集索引

\

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

USE [tempdb]

GO

 

CREATE TABLE t2 ( id INT )

GO

CREATE NONCLUSTERED INDEX IX_T2 ON [dbo].[t2](ID ASC)

GO

 

 

DECLARE @I INT

SET @I = 1

WHILE @I < 1000

    BEGIN

        INSERT  INTO [dbo].[t2] ( [id] )

                SELECT  @I

        SET @I = @I &#43; 1

    END

 

SELECT * FROM [dbo].[t2] WHERE [id]=20

Copy after login
View Code

\

只有一列,肯定会走索引的


一张表有多列的情况

分三种情况:

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

--只有聚集索引

USE [tempdb]

GO

CREATE TABLE Department 

(

  DepartmentID INT IDENTITY(1, 1) NOT NULL PRIMARY KEY,

  Name NVARCHAR(200) NOT NULL ,

  GroupName NVARCHAR(200) NOT NULL ,

  Company NVARCHAR(300) ,

  ModifiedDate DATETIME NOT NULL DEFAULT ( GETDATE() )

 

)

 

 

DECLARE @i INT

SET @i=1

WHILE @i < 100000

    BEGIN

        INSERT  INTO Department ( name, [Company], groupname )

        VALUES  ( '销售部'&#43;CAST(@i AS VARCHAR(200)), '中国你好有限公司XX分公司', '销售组' )

        SET @i = @i &#43; 1

    END

 

 

SELECT * FROM [dbo].[Department] WHERE [DepartmentID]=2

Copy after login
View Code

\

\

小结:

只有聚集索引的表:如果where后面不包括创建聚集索引的时候的第一个字段,就会使用聚集索引扫描

下面SQL语句会使用聚集索引查找,因为包括了创建聚集索引的时候的第一个字段

1

SELECT * FROM [dbo].[Department] WHERE [Company]='销售部12' AND [DepartmentID]=12

Copy after login
Copy after login

只有非聚集索引

\

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

--只有非聚集索引

 USE [tempdb]

 GO

 

 CREATE TABLE Department 

 (

   DepartmentID INT IDENTITY(1, 1) NOT NULL ,

   Name NVARCHAR(200) NOT NULL ,

   GroupName NVARCHAR(200) NOT NULL ,

   Company NVARCHAR(300) ,

   ModifiedDate DATETIME NOT NULL DEFAULT ( GETDATE() )

  

 )

  

 CREATE NONCLUSTERED INDEX IX_Department ON Department(DepartmentID ASC)

 

 DECLARE @i INT

 SET @i=1

 WHILE @i < 100000

     BEGIN

         INSERT  INTO Department ( name, [Company], groupname )

         VALUES  ( '销售部'&#43;CAST(@i AS VARCHAR(200)), '中国你好有限公司XX分公司', '销售组' )

         SET @i = @i &#43; 1

     END

  

SELECT * FROM [dbo].[Department] WHERE [Company]='销售部12' AND [DepartmentID]=12

Copy after login
View Code

\

\

小结:

只有非聚集索引的表:如果where后面不包括创建非聚集索引的时候的第一个字段,就会使用表扫描或者索引扫描

下面SQL语句会使用非聚集索引查找,因为包括了创建非聚集索引的时候的第一个字段

1

SELECT * FROM [dbo].[Department] WHERE [Company]='销售部12' AND [DepartmentID]=12

Copy after login
Copy after login


有聚集索引也有非聚集索引

\\ 复制代码

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

--有聚集索引和非聚集索引

 USE [tempdb]

 GO

  

 CREATE TABLE Department 

(

  DepartmentID INT IDENTITY(1, 1) NOT NULL PRIMARY KEY,

  Name NVARCHAR(200) NOT NULL ,

  GroupName NVARCHAR(200) NOT NULL ,

  Company NVARCHAR(300) ,

  ModifiedDate DATETIME NOT NULL DEFAULT ( GETDATE() )

  

)

  

CREATE NONCLUSTERED INDEX IX_Department ON Department(Company ASC)

  

 

 DECLARE @i INT

 SET @i=1

 WHILE @i < 100000

     BEGIN

         INSERT  INTO Department ( name, [Company], groupname )

         VALUES  ( '销售部'&#43;CAST(@i AS VARCHAR(200)), '中国你好有限公司XX分公司', '销售组' )

         SET @i = @i &#43; 1

     END

Copy after login
View Code

\

\

\

小结:

有聚集索引和非聚集索引的表:如果where后面包括创建聚集索引的时候的第一个字段,就会使用聚集索引查找

如果where后面包括创建非聚集索引的时候的第一个字段但不包括创建聚集索引的时候的第一个字段,就会使用索引查找

如果where后面不包括创建非聚集索引的时候的第一个字段和不包括创建聚集索引的时候的第一个字段,就会使用聚集索引扫描

1

1 SELECT * FROM [dbo].[Department] WHERE [GroupName]='销售组'

Copy after login

\


总结

其实走不走索引,关键取决于where后面包括还是不包括

创建聚集索引的时候的第一个字段

创建非聚集索引的时候的第一个字段

跟select *没有关系的,select * 最大的影响就是额外的IO开销

像“键查找” ,“RID查找”这些运算符就是额外的开销

键查找:到聚集索引里找其他字段的值

RID查找:到堆表里找其他字段的值

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
4 weeks 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)

What are the Oracle index types? What are the Oracle index types? Nov 16, 2023 am 09:59 AM

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 Select Channels Go concurrent programming using golang Asynchronous processing method of Select Channels Go concurrent programming using golang Sep 28, 2023 pm 05:27 PM

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

How to implement change event binding of select elements in jQuery How to implement change event binding of select elements in jQuery Feb 23, 2024 pm 01:12 PM

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:

How to solve the problem that the index exceeds the array limit How to solve the problem that the index exceeds the array limit Nov 15, 2023 pm 05:22 PM

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? How to improve the efficiency of data grouping and data aggregation in PHP and MySQL through indexes? Oct 15, 2023 am 11:39 AM

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

PHP returns the string from the start position to the end position of a string in another string PHP returns the string from the start position to the end position of a string in another string Mar 21, 2024 am 10:31 AM

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

Advanced applications of Python slicing and indexing: reveal hidden functions and explore the infinite possibilities of programming Advanced applications of Python slicing and indexing: reveal hidden functions and explore the infinite possibilities of programming Feb 19, 2024 pm 08:40 PM

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

How to charge Bluetooth headsets correctly How to charge Bluetooth headsets correctly Feb 22, 2024 pm 10:42 PM

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

See all articles