0

I have Powershell script which runs the below SQL query. This script is scheduled to run every 5 minutes. Could you please tell me whether querying database every 5 minutes will cause database deadlock. Could someone please throw light on how deadlock works in SQL.

SELECT TOP 1000 * FROM TABLE1

I am not concerned about the data which I get from select query. I am pulling this data to check the response time. Does the parameter WITH(NOLOCK) prevent deadlock?

POSH Guy
  • 1,798
  • 2
  • 11
  • 15
  • 1
    Possible duplicate of [what is deadlock in a database?](https://stackoverflow.com/questions/2774935/what-is-deadlock-in-a-database) – Igor Jun 27 '19 at 18:40
  • 1
    Or you can search for it, there are plenty of good results. [sql server what is a deadlock](https://duckduckgo.com/?q=sql+server+what+is+a+deadlock). – Igor Jun 27 '19 at 18:41
  • Perhaps your colleague meant block rather than deadlock. In that case, you add a `NOLOCK` hint to avoid that, with the understanding the result may be transactionally inconsistent, including skipped or duplicated rows in some cases. – Dan Guzman Jun 27 '19 at 18:52
  • 1
    Allow me to suggest that you ignore all further comments in this area from this colleague. As described, you ask a theoretical question because someone said this. Do NOT try to solve problems that do not exist. TOP without an order by clause is not wise - but that's a very different and unrelated issue. – SMor Jun 27 '19 at 19:29
  • I deleted my answer with using no lock and SMor was correct. It is unwise to suggest playing with locks with no understanding of the side effects or explaining what the side effects are. – Ross Bush Jun 27 '19 at 19:47
  • I rolled back your question edit because you removed all contextual information. – Nick.Mc Nov 09 '19 at 09:02

1 Answers1

0

This script alone will never deadlock your table because it is just a SELECT. Check your SQL Activity Monitor in SSMS to if any other processes are updating the table, because a deadlock happens when you select and update the record at the same time.

michiel Thai
  • 547
  • 5
  • 16