SQL - Query Execution Time

I am querying a SQL server 2008 database table having more than 500 million records. The query I fired to move around 10 million records to temp table took 9 hours. Similar query I fired which has been going for more than 10 hours.

How to know
1- time it will take to complete the running query
2- time it will take to complete query before execution

Many thanks

This question and answers originated from www.stackoverflow.com
Question by (12/6/2010 5:37:21 AM)

Answer

Certain statements report the percent complete in sys.dm_exec_requests percent_complete column:

Percentage of work completed for the following commands:

  • ALTER INDEX REORGANIZE
  • AUTO_SHRINK option with ALTER DATABASE
  • BACKUP DATABASE
  • DBCC CHECKDB
  • DBCC CHECKFILEGROUP
  • DBCC CHECKTABLE
  • DBCC INDEXDEFRAG
  • DBCC SHRINKDATABASE
  • DBCC SHRINKFILE
  • RECOVERY
  • RESTORE DATABASE,
  • ROLLBACK
  • TDE ENCRYPTION

For other statement, you'll have to wait it out and see.

First of all you'd have to check whether the 'query' is making any progress or is just sitting idle, blocked b something else. The same DMV mentioned above will immediately reveal this, the wait_time, wait_type and wait_resource are a dead give away: if they change, its making progress, if they stay fixed (and wait_time is growing) the query is stuck and the blocking _session_id is the blocker.

If the query was indeed making progress, and is one single query that is moving the rows, I gotta say: one does not go around and blindly moves 10M rows. If you did this in one single transaction, then your transaction log has been growing over the past 10 hours continuously. Every log growth event is a complete freeze of the database as it waits for the log to expand to zero out the new allocated disk space. Then the activity will resume, only to hit a new growth event in a short bit. By now, your database log must had grown several hundred times likely. This happens under all recovery models, including SIMPLE.

You could abort the query, but you'd have to wait until the 10 hours of the transaction will roll back. A 10h transaction can take another 10h to roll back, or more, or less, it depends on many many factors.

You could shutdown the server in panic and restart it, only to face the recovery of the database that is trying to undo the 10h transaction. Recovery will last, guaranteed, longer than rolling back the transaction.

So this is why one does never move 10M rows in a single 'query'. Batching the move in smaller sets is a must. Of course, this requires a carefully written program to do it.

Some users are lucky, they learn by stumbling on posts like this and making a note to self 'don't move 10M rows in one single operation'. Other learn the hard way... but they (hopefully) know better next time.

Answer by

Find More Answers
Related Topics  sql  sql-server  query  time  execution
Related Questions
  • MySql query execution time

    I am using MySQL Workbench on Windows. How do I find out the time taken to execute a query like Select * from employers where employerid > 200 Is there a statement I can enter that returns…
  • oracle query execution time

    I would like to get my guery execution time from Oracle DB. I dont want the time when Oracle finish to print results. In MySQL it is easy to get execution time from shell, but in SQL Plus there are …
  • mysql query execution time | reduce query

    I have a query which let me to change users order. here is my query: update test set orderID = case orderID when (select orderID from ( select * from test where orderID > ( select …
  • Measuring Query Performance : "Execution Plan Query Cost" vs "Time Taken"

    I'm trying to determine the relative performance of two different queries and have two ways of measuring this available to me: 1. Run both and time each query 2. Run both and get "Query Cost" from…
  • Same query, different execution plans

    I am trying to find a solution for a problem that is driving me mad... I have a query which runs very fast in a QA Server but it is very slow in production. I realized that they have different ex…
  • mysql execution time

    Is there a way to get the execution time of the last executed query in mysql?
  • Real-time query execution plans while debugging a T-SQL script

    I have SQL Server 2008 and SSMS 2008, and I'm debugging a script. I can step through the script with no problems at all, but if I click the toolbar button for "Include Actual Execution Plan" (the on…
  • Question on Query execution

    In the below query if the Patients table has 1000 records how many times TableValueFunction executes? Only once or 1000 time? This is a query in a Stored Procedure, do you have a better idea to i…
  • Access 2000 Query Execution Time

    I have two tables like Parent-Child. In Parent table, there are 7211 records. In Child, there are 169498 records. Between these two tables, there is no relationship (means haven't set Foreign Key, b…
  • Reducing execution time for the following query

    How can I reduce time for the following query? SELECT * FROM (`ci_session`) WHERE `session_id` = '8011e978d8692846a22bc8f7f806af4d' AND `user_agent` = 'Mozilla/5.0 (Windows NT 6.1; rv:2.0) Gecko/…