Original: Some small places that are easy to overlook in SQL Development (v)
background:
Index classification: It is well known that indexes are clustered and nonclustered indexes.
Index Advantage: Speed up data query.
Question: But do we really know the application of the index? Do you write a query statement that fully applies the index, or how do you design your index to make it more efficient?
Experience: Previously I only know the benefits of the index, but whether it can really make it work, and there is not much theory, for some I did some demo, to briefly explain under what circumstances to make full use of the index.
Case:
A student table is created here: there are fields below, and no indexes are established in the table.
CREATE TABLE [dbo]. [Student] (
[ID] [int] IDENTITY (a) not NULL,--student ID
[sUserName] [NCHAR] (COLLATE) chinese_prc_ci_as NULL,--student Name
[Saddress] [varchar] (COLLATE) Chinese_prc_ci_as NULL,--student Address
[ClassID] [INT] NULL,--student's class ID
[Create_date] [DateTime] NULL CONSTRAINT [df_student_create_date] DEFAULT (GETDATE ())--Time of entry
) on [PRIMARY]
Business Requirements:
Query the name and address of all students whose class ID is 9.
situation One:
--The field does not establish any index
Select Susername,saddress from Student
where classid=9
Execution plans such as:
Conclusion: In the absence of any index, the query selects a full table scan.
situation Two:
Create a clustered index for the ID increment column, which in many cases is the default, which is the clustered index on the primary key. The same query, different query plans, found that while there are no IDs in the output columns and conditions, the query selects the clustered query.
The execution plan diagram is the same as diagram one.
situation Three:Create a nonclustered index on ClassID.
Conclusion: Although the ClassID Index column appears in the Condition column, no index is created in the output column, and the clustered scan method is still used to query.
situation Four:Creating a nonclustered index on sUserName
Conclusion: Ibid.
situation Five:To continue creating a nonclustered index on saddress
Conclusion: Ibid.
situation Six:Establishing a federated nonclustered index of sUserName and saddress
Conclusion: Ibid.
situation Seven:Creating an index on ClassID and sUserName
Conclusion: Ibid.
Scenario Eight: Create a federated nonclustered index on classid,susername,saddress
The implementation plan diagram is as follows:
Conclusion: The full use of the index scan occurs when the column that appears in the condition and the output column and the Union index column match exactly.
condition Nine: Delete all indexes, keep the clustered index of ID. Query with a clustered index column as one of the criteria.
Select Susername,saddress from Student
where id=10021002
Or: Select Susername,saddress from Student
where id=10021002 and classid=9
Execution plan diagram:
Summary of all cases:
1: IO condition when using a nonclustered Index Scan: Table ' student '. Scan count 1, logical read 70 times, physical read 0 times, read 0 times, LOB logic read 0 times, lob physical read 0 times, lob read 0 times.
2: IO condition when using a clustered index scan (the clustered index column does not appear in the condition ): Table ' student '. Scan count 3, logical read 8,835 times, physical read 0 times, read 0 times, LOB logic read 0 times, lob physical read 0 times, lob read 0 times.
3: IO condition when using clustered Index Scan ( clustered index column in condition ): Table ' student '. Scan count 1, logical read 3 times, physical read 0 times, read 0 times, LOB logic read 0 times, lob physical read 0 times, lob read 0 times.
Conclusion:
1: In the absence of the correct index, the number of scans of the table is increased.
2: The data will look for a matching index at query time.
1): If a clustered index column appears in the Criteria column, the clustered index will be looked up (with a clustered index) regardless of whether the output column is indexed.
2): If the clustered index column does not appear in the Condition column, the matching nonclustered index is found, and if there is a matching index, the index is queried, otherwise the clustered index (with clustered index) is scanned.
3): Find a matching nonclustered index (no clustered index).
This article summarizes:
I simply wrote about the use of the index of the demo, in the actual development to be analyzed according to the actual situation, and sometimes can not fully use the index, but to allow the query to produce a minimum of IO read and the number of table scans.
Some small places that are easy to overlook in SQL Development (v)