I have an Azure SQL server (standard tier, S3) with tables. I have a specific query that takes very long time to be executed:
DELETE FROM MyTable WHERE ID=@ID
This table has 150K rows. The delete query takes +-10 minutes.
I tried to understand why. First check is how fast is locate this row in SELECT query. I run:
SELECT * FROM MyTable WHERE ID=@ID
The result: less then 2 seconds.
I read about this issue in this thread. I took the selected answer and extract a check list from it. See my answers inside.
Checklist:
- deleting a lot of records - This query deletes exactly 1 row.
- many indexes -
MyTable
has: 1 PK, 1 FK, 4 indexes and nothing else. So I belive this is not the problem. - missing indexes on foreign keys in child tables - My only FK has also INDEX on the other table.
- triggers - no triggers.
- cascade delete (those ten parent records you are deleting could mean millions of child records getting deleted) - I tried to delete a row that referenced in other tables. So, cascade delete shouldn't delete anything on other tables.
- Many Foreign keys to check - Not sure what it mean. I have only one FK that point to very small table (<10 rows + index).
- Transaction log needing to grow - How to check?
- deadlocks and blocking - How to check?
My guts feeling is that it somehow depends on the other tables that reference to this table (MyTable
is FK to them). Since I have +-15 tables, I don't know how to locate the problem.
Additional observation: I found something interesting. If you run the delete one after another, the first call is taking a lot of time (as reported above), the second call is faster and so on. If you run this query few times, it getting really fast (<3 seconds - which is great). After few hours - it's became slow again.
Questions:
- What more to check?
- Is this possible to run the
DELETE
query and profile it? Let assume thattable2
has a FK toMyTable
and the search for row@ID
is taking alot of time. Maybe I can have a tool that say: "99% of the execution time spend ontable2
?" - Any other tips will be welcome!
Thanks!