SQL deadlock when running select query - sql-server

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?

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.

Related

How does SQL deal with a long running query via Linked Server if there’s a PC reboot?

I have a SQL Server database and have a linked server connection to an Oracle DB.
I had the following query running on my SQL Server:
INSERT INTO dbo.my_table_on_sql_server
SELECT *
FROM OPENQUERY (linkedservername, ‘SELECT * FROM target_table’)
The target_table has 50 million rows and I'm aware the query takes time to execute but has successfully completed before.
This time though, my PC had an automatic restart in the middle of the query. SSMS 2017 automatically reopened as soon as the PC fired back up, but I could not longer see the query running. my_table_on_sql_server has no data.
I'd like to understand what happens in SQL Server in the event of such a situation. Am I correct in assuming that the query was killed / rolled back? Is there any query running in the background? I've seen some related answers on this forum but wanted to specifically understand this for linked servers, as I use them a lot to retrieve data from other DBs for my job.
I'm more concerned about the Oracle DB as I don't want my query to impact any performance upstream. I only have a read-only access permission to the Oracle DB.
Thank you!
On shutdown the query will be aborted, and the INSERT rolled back. The rollback may happen during shutdown, or after restart, and may take some time to complete.
There's no automatic retry or anything that will access the linked server Oracle after the shutdown.

SQL query in Excel locking table?

I ran into an issue where no queries could be executed at all against a single table in our database. The table was in a complete deadlock. I checked sp_whoisactive for answers and found the following.
A simple SELECT on this table with session id 172, was waiting for a DELETE query on this table with session id 478, which was waiting for an INDEX REORGANIZE on this table with session id 207, which was waiting for a simple SELECT on this table with session id 598.
After killing session 598 everything completed immediately. Afterwards, executing that same query that was keeping the table in a deadlock in a separate window in SSMS only took 2 seconds. I asked around and this query is executed by an Excel file. Apparently we have lots of Excel files floating around which run queries against our SQL database. Obviously this is extremely bad practice as the connection string can be found inside, but as with everything legacy that's just the way it is now until it is fixed.
When googling I'm finding a lot of resources about Excel actually locking tables. Now as far as my limited understanding of locks goes if the query executed by session id 598 would actually lock the table it should only do so for the duration of the query. And since running the query separately only took a few seconds I don't understand how it was running for over 12 hours. If I can trust the results of sp_whoisactive it wasn't waiting on anything else. So why didn't it complete?
Before I suggest something like adding WITH(NOLOCK) to every query in Excel files, which is just a patch and not a solution I'd like to find out why this happened so that we can avoid this in the future. What causes a deadlock like this and how can it be avoided?
Excel will do a page lock for the duration of the query and until the query is released, which is basically when the spreadsheet is closed. So, if the spreadsheet was left open by a user overnight or for an entire day, then the table would be locked. We have the same issue and we are trying to get the offending spreadsheets replaced with SSRS reports but not having much luck with the take up from staff because the Excel files do a lot of other things that SSRS doesn't.

Sybase Select select statement getting Hang

In our system, we are dropping some DML statements in Flat File and with the help of executing SQL task i am hitting it to Sybase DB, sometime select statement get hang. Don't know how can i resolve, i am updating the statistics also but still facing problem, there are 1000(Approx) of DML hit every 2-5 mins to sybase.
Please let me know the resolution

Table without lock won't query unless nolock hint included

Microsoft SQL Server 2008 R2
I have a table that currently can not be queried on but seems to not have a lock against it.
Doesn't return:
SELECT * FROM myTable
Does return:
SELECT * FROM myTable with (nolock)
Inserts into the table also fail.
When I run sp_lock, I don't find any instances of locks on myTable. When I run the "Resource Locking Statistics by Object" report, I don't see any locks for myTable.
What other possibilities could there be that would keep a table from being acted upon?
Thanks.
Run sp_who2 and look at all the spids. See if you still have a connection to the database that is still thinking your performing an insert/update/delete to the table. If you find it, kill the spid and you should be able to query the table.

MS SQL Specific Tables Hanging at Queries

I have SQL Server 2008. I run a query in a table on a database. The weirdest thing keeps happening. I run a simple select statement on the table. I know there are 62 rows in the table but it gets stuck at row 48 and goes on "querying...". Waited already for hours and it didn't move on from there. I only know of two programs, and one reporting service connecting to that particular table and one other user. Does anyone have any idea on what could be causing this and how I could trace the source of the lock on that table?
As a side note, I noted that the logs only had a notice that Autogrow failed the day before I checked. Could this have something to do with it?
What if you do a
SELECT * FROM YourTable WITH(NOLOCK)
Does it still hang?
Additionally when it does appear to be blocked you can try running
exec sp_who2
And looking in the BlkBy column to see what process is blocking you.
If that doesn't shed any light this article gives some info on some DMVs that might help get some insight into reasons for waits.

Resources