views:

1067

answers:

5

I've got a really long running SQL query (data import, etc). It's crap - it uses cursors and it running slowly. It's doing it, so I'm not too worried about performance.

Anyways, can I pause it for a while (instead of canceling the query)?

It chews up a a bit of CPU so i was hoping to pause it, do some other stuff ... then resume it.

I'm assuming the answer is 'NO' because of how rows and data gets locked, etc.

I'm using Sql Server 2008, btw.

A: 

The best approximation I know for what you're looking for is

BEGIN
    WAITFOR DELAY 'TIME';
    EXECUTE XXXX;
END;
GO
Sheldon
nope. i don't want a pause IN the query .. but to pause the _entire_ query in the middle of it executing.
Pure.Krome
A: 

When working on similar situations, where I was trying to go through an entire list of data, which could be huge, and could tell which ones I have visited already, I would run the processing in chunks.

update or whatever
where (still not done)
limit 1000

And then I would just keep running the query until there are no rows being modified. This breaks the locks up into reasonable time chunks and can allow you to do thinks like move tens of millions of rows between tables while a system is in production.

Jacob

TheJacobTaylor
Note that according to msdn, LIMIT was not implemented in mssql http://blogs.msdn.com/sqlserver/archive/2006/10/25/limit-in-sql-server.aspx Also, LIMIT is usually applied **after** most of the work has been done, see your DBMS's query planner for reference.
Dana the Sane
Missed that point. long running SQL query did not imply SQL Server to me. I did not look at the tags. Thanks. For updates, there is work done ahead of time, but in a case where you are migrating lots of data, and index scan, followed by locking the table for a small number of file writes is still much better than index scan and locking the table while rewriting the entire thing. It does depend on what you are doing though.
TheJacobTaylor
+1 for excellent comment! Thanks.
TheJacobTaylor
+1  A: 

Click the debug button instead of execute. SQL 2008 introduced the ability to debug queries on the fly. Put a breakpoint at convenient locations

Chris Lively
damn. that's actually a good idea! a bit late now, after i've been .. 12+ hours into the import... :( but, that's an idea i do like :)
Pure.Krome
+1  A: 

Not only can you not pause it, doing so would be bad. SQL queries hold locks (for transactional integrity), and if you paused the query, it would have to hold any locks while it was paused. This could really slow down other queries running on the server.

Rather than pause it, I would write the query so that it can be terminated, and pick up from where it left off when it is restarted. This requires work on your part as a query author, but it's the only feasible approach if you want to interrupt and resume the query. It's a good idea for other reasons as well: long running queries are often interrupted anyway.

Sean Reilly
A: 

Instead of pausing the script, perhaps you could use resource governor. That way you could allow the script to run in the background without severely impacting performance of other tasks.

MSDN-Resource Governor

DavidStein