The truth of select *: index coverage experiment results

Source: Internet
Author: User
Yesterday I saw Goodspeed (blog address http://goodspeed.cnblogs.com/) Post "select * Truth: index coverage" (http://www.cnblogs.com/goodspeed/archive/2007/07/20/index_coverage.html) I think incredible: the query fields are the same. Why is the query performance of select * worse than that of the displayed list field names? To verify this, I decided to give an example experiment.
I created a table in the database. The table creation script is as follows: 1 Create   Table   [ DBO ] . [ Performance ] (
2 [ PK ]   [ Int ]   Identity ( 1 , 1 ) Not   Null ,
3 [ Data ]   [ Int ]   Not   Null ,
4 [ Datetime ]   [ Datetime ]   Not   Null   Constraint   [ Df_performance_datetime ] Default ( Getdate ()),
5   Constraint   [ Pk_performance ]   Primary   Key   Clustered  
6 (
7 [ PK ]   ASC
8 ) With (Ignore_dup_key =   Off ) On   [ Primary ]
9 ) On   [ Primary ]

And then use Code The code for inserting 1000001 records is as follows: 1 Dim Com As   New Sqlclient. sqlcommand ( " Insert into performance (data) values (1) " )
2 Com. Connection = Con
3 For I As   Integer   =   0   To   1000000
4 Com. executenonquery ()
5 Next

Insert the data code and use the following query commands to query the code 10 times respectively (to avoid interference, remove the data echo ).
The query code using select * is as follows: 1 Dim Com As   New Sqlclient. sqlcommand ( " Select * from performance " )
2 Com. Connection = Con
3 Dim Reader As Sqldatareader = Com. executereader (commandbehavior. closeconnection)
4
5 Dim Time As Datetime = Datetime. Now
6 While Reader. Read
7 End   While

The query code for displaying the query is as follows: 1 Dim Com As   New Sqlclient. sqlcommand ( " Select PK, Data, datetime from performance " )
2 Com. Connection = Con
3 Dim Reader As Sqldatareader = Com. executereader (commandbehavior. closeconnection)
4
5 Dim Time As Datetime = Datetime. Now
6 While Reader. Read
7 End   While

Use the time-consuming table for query by display fields:

Times Time (MS)
1 468.756
2 468.756
3 484.3812
4 484.3812
5 484.3812
6 484.3812
7 484.3812
8 484.3812
9 484.3812
10 484.3812

Use select * to query the time-consuming time table:

Times Time (MS)
1 500.0064
2 484.3812
3 484.3812
4 484.3812
5 734.3844
6 750.0096
7 750.0096
8 734.3844
9 765.6348
10 750.0096

I felt that the time difference was a little big, but I did another test, and the time was basically the same. It gave me the feeling that the select * query performance was not as good as the query stability. If table scan is time-consuming, sometimes the time is the same.
I guess the reason is that SQL Server is more likely to be hit by the execution plan prepared for the query than the select * query. If you know the exact reason, please let me know.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.