| Lesson 2 || Executing queries |
| Objective || Describe how to execute your queries. |
Executing SQL Server Queries
Most of the modules in this course are geared towards constructing different types of queries. Once you construct a query, you will need to know how to send it to SQL Server for processing, also known as executing
Although you can take a complicated approach to issuing a query, such as writing a Visual Basic or Visual C++ program, the easiest way to execute a query is to use Query Analyzer, which is installed automatically with SQL Server 2000-2005.
was an important tool because it gave you the ability to interactively analyze
the results of your queries and determine if the query could be optimized
Executing Query Analyzer
- Executing: The process of sending a query to SQL Server for processing. This is synonymous with Issuing.
- Issuing: The process of sending a query to SQL Server for processing. This is synonymous with Executing.
- Optimized: The process of determining the fastest possible way to execute a query.
Optimization can be simple or complex, depending on the complexity of the query or stored procedure being issued.
All of the query lessons in this course assume that you are not using Query Analyzer to issue your queries since it has been deprecated since SQL-Server 2005. In the next lesson, batch queries and how they compare to single queries will be discussed.
Analyzing a Query
In SQL Server 2008 and later, the following is used to analyze a query in place of Query Analyzer.
The SQL Server Database Engine can display how it navigates tables and uses indexes to access or process the data for a query or other DML statement, such as an update. This is a display of an execution plan. To analyze a slow-running query, it is useful to examine the query execution plan to determine what is causing the problem. For more information about how SQL Server creates and uses execution plans, see SQL Statement
Processing and Execution Plan Caching and Reuse.
You can display execution plans by using the following methods:
- SQL Server Management Studio :
Displays either an estimated graphical execution plan (statements do not execute) or an actual graphical execution plan (on executed statements), which you can save and view in Management Studio.
- Transact-SQL SET statement options: When you use the Transact-SQL SET statement options, you can produce estimated and actual execution plans in XML or text.
- SQL Server Profiler event classes:
You can select SQL Server Profiler event classes to include in traces that produce estimated and actual execution plans in XML or text in the trace results.