Have you ever wanted to insert more than one row of static text in to a SQL table using the VALUES argument? Well, this is actually pretty easy to do. To do this you just need to pass a comma delimited value list like in the example below.
One of the limitations of SQL Server Management Studio is that it shows query execution time in seconds. In most daily activities, this is accurate enough. But when it comes to query permanence tuning, it is helpful to have a more accurate measurement.
Here are a 3 different ways to show you an accurate query execution time:
To have your exact query time show in the separate messages tab, all you need to do is run the following queries.
-- Insert query here
Then you can view the time by clicking on the Messages tab.
Include Client Statistics
To get a lot of in-depth information about your query, you can turn on Client Statistics before running your query. The details of all the results will be shown in the Client Statistics tab after execution was completed.
Turn on client statistics by choosing the Include Client Statistics option from query menu.
You can then view the query time by looking under the Client Statistics tab by your query results
Inline Query Time Results
If you would like to just show the execution time in the standard results window, this query allows you to do this. It will also allow you to show exact time of execution of a specific code segment in a query.
To use this, just place the code that you would like to get the execution time for in the center of the below script. The exact time will be shown in the results.
Although SQL Server does not have a built in way to return your query results in random order, you can accomplish this in an easy way. One thing to note is that this way of returning the results is very expensive… so it could take a while.
In the following example, the magic happens in the order by clause. The NEWID() function will generate a unique GUID for every record. This will cause the results to come back randomized.
Most of the time that people connect to a remote database they use linked servers. But have you ever wanted to connect to another database server without creating a linked server? This post will show you how to connect to an ad hoc database server while executing a query.
To accomplish this, one method (samples below) that SQL Server provides is to use opendatasource. Opendatasource allows you to connect and query a remote data source by passing your connection parameters in to the command.
NOTE: Microsoft does not recommend using this method of connecting to a remote database server if this data source is accessed more than a few times. If the data source is frequently used, they recommend that you create a linked server.
Connect To Remote Database Using Windows Authentication
Option recompile is one of those hidden gems when it comes to increasing stored procedure performance. When used in the right spot, this can speed up a never ending stored procedure call to instantaneous. If you don’t know what option recompile is… it’s just a query hint that can be added to the end of a sql query.
What Option Recompile Does
When option recompile is added to the query hints, it does a couple things. The first thing that it does is it tells SQL Server to discard any stored execution plan for that query. This will cause SQL Server to rebuild the execution plan for this query. The next thing it does is the magical part. When SQL Server goes to build the execution plan, it will replace the local variables with the actual values in those variables. This allows SQL Server to pull the data SUPER fast.
Keep in mind that many times the slowdown in a stored procedure call is just one statement inside the stored procedure.
The easiest way that I have found to figure out if this will help a slow stored procedure is to decompose the stored procedure in to separate sql statements. I then run the sql statements to see what one is the slow one. If I run a statement that is slow and uses a local variable, this may be a good candidate. Next I replace the local variables in that statement with the explicit values. If I rerun the statement and it performs fast, I know that this could use option recompile.