I have a SQL Server 2012 table with 2697 records and the table is not indexed. In the future, data will increase to 100 thousand records. I do not join any other table to get entries. First, I created a custom function to retrieve records from a table.
Later I found out that the view will be faster than the user-defined function, and so I created a view for this table.
To find out the performance of the query, I included the codes below to get the processor time and elapsed time of my UDF, VIEW, and direct SQL statement.
SET STATISTICS IO ON; SET STATISTICS TIME ON;
When I pulled the data directly from my table using the select query, I got below CPU time and elapsed time
SELECT [CollegeName] ,[CandidateID] ,[age] ,[race] ,[sex] ,[ethnic] ,[arm] ,[Weeknum] ,[siteid] ,[country] ,[Region] ,[SubRegion] ,[SNAME] ,[UID] FROM [testdata]
---- Result
Scan count 1, logical reads 1338, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. SQL Server Execution Times: CPU time = 31 ms, elapsed time = 4381 ms.
When I used VIEW, I got CPU time and Elapsed time as
CREATE VIEW vw_testdata AS SELECT [CollegeName] ,[CandidateID] ,[age] ,[race] ,[sex] ,[ethnic] ,[arm] ,[Weeknum] ,[siteid] ,[country] ,[Region] ,[SubRegion] ,[SNAME] ,[UID] FROM [testdata]
- Result
Scan count 1, logical reads 1324, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. SQL Server Execution Times: CPU time = 15 ms, elapsed time = 5853 ms.
And my UDF is back as
CREATE FUNCTION [dbo].[fn_DocApproval] (@collegename nvarchar(30) = NULL) RETURNS TABLE AS RETURN ( SELECT [CollegeName] ,[CandidateID] ,[age] ,[race] ,[sex] ,[ethnic] ,[arm] ,[Weeknum] ,[siteid] ,[country] ,[Region] ,[SubRegion] ,[SNAME] ,[UID] FROM [testdata] WHERE CollegeName = ISNULL(@collegename, collagename) )
- Result
Scan count 1, logical reads 1338, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. SQL Server Execution Times: CPU time = 203 ms, elapsed time = 785 ms.
UDF has much less past time than direct sql and view, however CPU time is longer.
However, processor time is shorter compared to direct SQL and UDF.
I want to know which one we need to determine the query performance.
Also, why does the time and time of the processor and elapsed time change each time the same request is started?
My schema and Fiddle data examples
I have 2697 lines, and I canβt load them all in a script.