Why does a query executed from VB run more slowly than when run from Query Analyzer?


I have a VB-based application that uses SQL Server as its back-end. I have noticed that if I run the exact Transact-SQL queries (taken from the VB app) in Query Analyzer, that they run must faster than they do from within the VB application. Why is this?


There can be many reasons why the exact same query runs much faster from within Query Analyzer(or Management Studio) than it does from a VB application. Here are some reasons, although this is not a comprehensive list.

  • VB can introduce additional overhead to Transact-SQL that does not occur in Query Analyzer. And how you call the query in your VB program affects performance differently. For example, using a bound control to execute the query code can be slow. Also, calling the code directly within a recordset object, and not from a stored procedure , can also slow down the code.
  • Is the VB app using DAO to access SQL Server ? This is very slow. ADO or .NET is faster.
  • Are you using ODBC or OLEDB to connect? OLEDB offers better performance.
  • Is SET NOCOUNT ON being used in your application to reduce network traffic between the application and SQL Server? If not, this can reduce performance.
  • What is your setting for the CacheSize property of the ADO Recordset object? If you are using the default value of 1, performance can be very slow due to unnecessary network traffic generated.

This list could go on and on. The main point I want to make is that while SQL Server may perform its tasks quickly (you saw this when you ran the query in Query Analyzer), it is very easy to slow it down with unnecessary overhead from your VB application.

There is another lesson to be learned here also. If you are trying to performance tune a VB application that accesses SQL Server, one of the best ways to help identify if it is the VB app, or SQL Server, causing the problem, is to run each of the queries in your VB application from within Query Analyzer or Management Studio. If the queries run faster in Query Analyzer or Management Studio, then your VB application is the most likely cause of the performance problem. But if the query runs slow in Query Analyzer, then you can blame the query (perhaps it is written poorly), or the indexes (you may have poor or no useful indexes), or perhaps some other functionality of SQL Server is responsible. But until you perform this testing, you really don’t know the cause of the performance problem.


Leave a comment

Your email address will not be published.