During optimization, it is found that the execution of a stored procedure for 20 seconds through profiler capture mainly consumes a select * from table. The problem is that it took nearly 20 seconds to select tens of thousands of data? Troubleshooting: The program frontend uses datareader to obtain data. Therefore, datareader finds that a stored procedure is executed for 20 seconds during optimization and the profiler capture time is mainly used in a select * from table, so the question is, it took nearly 20 seconds to select tens of thousands of data?
After troubleshooting, the program frontend uses datareader to obtain data, what is the impact of datareader on the database? Let's test the experiment. First, create a test table and insert 200 data records.
1 create table [dbo]. [table_2] (
2 [a] [int] NULL,
3 [B] [datetime] NULL,
4 [c] [uniqueidentifier] NOT NULL
5)
6
7 insert into [table_2]
8 select 1, getdate (), newid ()
9 go 200
Write a simple C # applet and use datareader to read a select * from table. in the cycle of datareader, we set the thread to wait for 50 milliseconds. System. Threading. Thread. Sleep (50 );
Protected void Button1_Click (object sender, EventArgs e) {// SqlCommand sqlCmd = new SqlCommand ("p_datareader_test", con); // sqlCmd. connection = con; // sqlCmd. commandType = CommandType. storedProcedure; // Set the call type to stored procedure // SqlParameter sqlParme = new SqlParameter (); // sqlParme = sqlCmd. parameters. add ("@ p", SqlDbType. int); // sqlParme. direction = ParameterDirection. input; // sqlParme. value = 0; // SqlDataRe Ader reader = sqlCmd. executeReader (); SqlConnection con = new SqlConnection (); con. connectionString = "server = vpc-new3; database = replication; uid = sa; pwd = sa_123456"; DataTable dt = new DataTable (); // SQL direct query test --------------------------------- con. open (); SqlCommand com = new SqlCommand (); com. connection = con; com. commandType = CommandType. text; com. commandText = "select * from table_2"; SqlDataReader Reader = com. executeReader (); DataRow dtr = dt. newRow (); try {DataTable objDataTable = new DataTable (); int intFieldCount = reader. fieldCount; for (int intCounter = 0; intCounter <intFieldCount; ++ intCounter) {objDataTable. columns. add (reader. getName (intCounter), reader. getFieldType (intCounter);} objDataTable. beginLoadData (); object [] objValues = new object [intFieldCount]; while (reader. read () {// Wait for System. threading. thread. sleep (50); reader. getValues (objValues); objDataTable. loadDataRow (objValues, true);} reader. close (); objDataTable. endLoadData (); GV. dataSource = objDataTable; GV. dataBind ();} catch (Exception ex) {throw new Exception ("conversion error! ", Ex);} reader. Close (); con. Close ();}
Run the following test:
Click Start btnclick
Execution time 0 ms? Not as long as you think? Why?
Test continues! We will increase the number of data records and add 200 more records.
Insert into [table_2]
Select 1, getdate (), newid ()
Go 1, 200
The test results are the same as 0 ms.
Continue to add 200 more data entries. Currently, 600 data entries are added.
Let's take a look at the effect:
It takes 12 seconds to query 600 records !!
Once again, let's take a look. it's only 12 seconds before the execution is complete!
Select wait_type, brt. text from sys. dm_exec_requests br
Outer apply sys. dm_exec_ SQL _text (br. SQL _handle) AS brt
Where brt. text like '% table_2 %'
That's right. it's his legendary ASYNE_NETWORK_IO.
Q: According to our previous understanding, datareader only reads one record at a time. Why can't I wait for 400 records?
The default size of the network package sent by mssql is 4096.
Then we continue to use 200 pieces of data to test this increase in the table length.
Drop table table_2
Create table [dbo]. [table_2] (
[A] [int] NULL,
[B] [datetime] NULL,
[C] [uniqueidentifier] not null,
D char (4000)
)
Insert into [table_2]
Select 1, getdate (), newid (), 'test'
Go 1, 200
Test the observed results again
Okay, I guess ....
The above problems will not occur if you receive the returned results using dataset. Therefore, if you use datareader to receive the result set on the Internet, pay attention to avoid a large amount of time-consuming processing in the loop after receiving the results.