0

I hope someone can help me. I have a simple sql statement

delete from sometable 
where tableidcolumn in (...)

I have 500 records I want to delete and recreate. The table recently grew to over 1 mill records. The problem is the statement above is taking over 5 minutes without completing. I have a primary key and 2 non clustered non unique indexes. My delete statement is using the primary key.

Can anyone help me understand why this statement is taking so long and how I can speed it up?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

0

There are two areas I would look at first, locking and a bad plan.

Locking - run your query and while it is running see if it is being blocked by anything else "select * from sys.dm_exec_requests where blocking_session_id <> 0" if you see anything blocking your request then I would start with looking at:

https://www.simple-talk.com/sql/database-administration/the-dba-as-detective-troubleshooting-locking-and-blocking/

If there is no locking then get the execution plan for the insert, what is it doing? it it exceptionally high?

Other than that, how long do you expect it to take? Is it a little bit longer than that or a lot longer? Did it only get so slow after it grew significantly or has it been getting slower over a long period of time?

What is the I/O performance, what are your average read / write times etc etc.

Ed Elliott
  • 6,666
  • 17
  • 32
0

TL;DR: Don't do that (instead of a big 'in' clause: preload and use a temporary table).

With the number of parameters, unknown backend configuration (even though it should be fine by today's standards) and not able to guess what your in-memory size may be during processing, you may be hitting (in order) a stack, batch or memory size limit, starting with this answer. Also possible to hit an instruction size limit.

The troubleshooting comments may lead you to another answer. My pivot's the 'in' clause, statement size, and that all of these links include advice to preload a temporary table and use that with your query.

ǝɲǝɲbρɯͽ
  • 973
  • 6
  • 16