Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

Suppose I run the following query:

declare @i integer
set @i=1
while @i < 2000000
BEGIN
insert into t1 (ID,DESC,date) values (@i%200,concat(NEWID(),newid()),GETDATE())
set @i = @i +1
END

Is the loop actually running on the client machine, and calling insert each time over the network, or is the loop running on the server and making only local calls?

On the same lines, is it better to run such queries by connecting to the server using SSMS, or by using RDP to connect to the server and using the local SSMS instance?

share|improve this question

migrated from superuser.com Nov 12 '12 at 8:24

This question came from our site for computer enthusiasts and power users.

2 Answers 2

up vote 3 down vote accepted

The SQL Server Management Studio only sends the query (or T-SQL prodcedure) to the server and waits for the result.

There's a difference between running it from LAN or directly on the server ONLY because results has to be transferred on your PC (so the difference is only the time to transfer data on LAN). (Also, in the first case, your PC has to be turned on for the entire duration of the query.)

share|improve this answer
    
So,just so I understand correctly, in a query such as the one I mentioned in the question, the loop is also running on the server, and 2 million insert calls are not being made over the network? –  Akash Nov 12 '12 at 9:38
1  
@Akash: The server processes the entire batch of instructions sent to it. Therefore, it would initiate all the inserts within the course of execution of the query. But there's also such a setting as NOCOUNT. It controls whether a DML statement sends a tiny report on the number of rows affected back to the client, and the setting is off by default, meaning the reports will be produced. Two million such reports will likely affect the overall execution time of your query considerably, so you might want to issue SET NOCOUNT ON somewhere before the loop (if looping is unavoidable, of course). –  Andriy M Nov 12 '12 at 13:03

An easy way to check this is to run SQL Profiler as you execute your statement. If you just run with the default trace settings, you'll see a couple of results related to this statement: a SQL:BatchStarting and a SQL:BatchCompleted. Both of those have as TextData the actual query. This means that SSMS is not doing the loop; SQL Server is. SSMS is, as AndreaCi points out, simply passing along the query text and telling the server to perform whatever action it needs to.

Regarding how to execute queries, I would recommend not logging directly into the server and running SSMS there (actually, I would recommend not having SSMS installed on the servers at all, but that's another topic for another question). The reason is that RDP and SSMS use server resources, so the RAM and CPU cycles that are being dedicated to running your client software aren't going to SQL Server, making legitimate server processing marginally slower for everybody.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.