According to the operation and experience of this article, there will be a basic and simple understanding of SQL optimization, other in-depth needs more information and practical learning:
1. Build a table:
Copy the Code code as follows:
CREATE TABLE Site_user
(
ID int IDENTITY (PRIMARY KEY),
[Name] varchar (20),
Code varchar (20),
Date datetime
)
2. Insert 80,000 data
Copy the Code code as follows:
DECLARE @m int
Set @m=1
While @m<80000
Begin
INSERT into [demo]. [dbo]. [Site_user]
(
[Name]
, [Code],date]
VALUES
(' Name ' +cast (@m as VARCHAR (20))
, ' Code ' +cast (@m as VARCHAR), getUTCDate ())
Select @[email protected]+1
END
--Tip: We recommend using tools like Sqlassist to improve the speed of writing SQL statements
3. Set settings to open some parameters
Copy the Code code as follows:
SET STATISTICS io on-view disk IO
Set STATISTICS TIME on-View SQL statements parsing compilation and execution times
SELECT * FROM Site_user--View effects
4. View the table index status:
Sp_helpindex Site_user
5. Execute SQL statements
Copy the Code code as follows:
SELECT * from Site_user su WHERE su.name= ' name1 ' table ' Site_user '.
Scan count 1, logic reads 446 times, physical reads 0 times, pre-read 0 times, lob logic reads 0 times, lob physical reads 0 times, LOB read-ahead 0 times
Ctrl+l shortcut key to view the execution plan:
6. Optimize the first step: the clustered index scan cost accounted for 100%, you can consider optimization for index lookup, set up a nonclustered index on the query condition name
Copy the Code code as follows:
Create INDEX Name_index on Site_user (name)
Sp_helpindex Site_user--more out of our newly built index
At this point, run the above query statement:
Copy the Code code as follows:
SELECT * from Site_user su WHERE su.name= ' name1 '
Table ' Site_user '. Scan count 1, logical read 4 times, physical read 0 times, read 0 times, LOB logic read 0 times, lob physical read 0 times, lob read 0 times.
The number of disk logical reads decreased significantly, and then the execution plan was viewed:
The newly created index has already played a role, but it still scans the clustered index of the primary key, which is even higher if the query can be performed on an index because the query
So consider further optimizations:
7. optimize the second step: set up a composite index
Copy the Code code as follows:
Create INDEX name_index4 on Site_user (Name,code,[date])
Table ' Site_user '. 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.
--The number of disk logical reads has dropped again
Then view the execution plan:
It's a lot quicker to go straight to index lookup, using index4
8. optimize the third step: we can also consider using the overlay index, the conditions used are written in the index brackets, the other query out of the field into the include ,
Copy the Code code as follows:
Create INDEX name_index5 on Site_user (name) include (id,code,[date]) table ' Site_user '.
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.
--there is no significant change in the number of disk logical reads and then view the execution plan:
Same Walk index lookup used index5
At this time: index4 and index5 How to choose?
Data analysis using DBCC :
Copy the Code code as follows:
DBCC show_statistics (' Site_user ', ' name_index4 ')
DBCC show_statistics (' Site_user ', ' name_index5 ')
As you can see, the same amount of data, average key length: covers the index index5, takes up less space, so we should first select the overlay index to optimize
In view of this easy, we can ask a lot of
SQL Optimization Basics using indexes (a small example)