Oscar
3/25/2007 3:49:00 PM
In case I run any SQL query within my VB application for the first time
after that I started my application, the performance of SQL server 2005 to
create the recordset is the slowest. For example it takes 30 seconds to
process the following query :
"SELECT ID_wrn FROM tblArbCtr WHERE Contract = 0 AND ((datumStart IS NULL
AND datumEind IS NULL ) OR (datumStart IS NULL AND datumEind >= " &
beginDate & ") OR (datumEind IS NULL AND datumStart <= " & eindDate & ") OR
(datumStart <= " & eindDate & " AND datumEind >= " & beginDate & ")) GROUP
BY ID_wrn"
OR
"SELECT cred FROM tblSalary WHERE ID_wrn=" & ID_emp & " AND year=" & yearnr
& " AND weeknr=" & weeknumber & " ORDER BY ID DESC")
However it shows that in case I ran the same query a couple of times again
and again, after the 3rd or 4th attempt, the processing time takes only 2
seconds, which is a very good performance improvement of course. However
when I continue to do other queries by navigating to another process and
return to the original process again, it takes about 30 seconds again to
perform the original query. Then when running the query again and again it
takes only 2 seconds to perform the query. So it looks like SQL server is
capable of finding an optimal way to process the query when the same query
is fired again and again, however, it forgets this optimalisation once it
receives another queries subsequently.
How can I investigate and find the way to have SQL Server perform such
queries always the best? Particularly, I want to find out how SQL finds out
a better way to improve the query in order to ensure the best performance
also in case of the first time the query has to be processed.
regards,
Oscar