SQL Server select gets blocked when using ADO - sql-server

I have a very old Delphi app that connects to SQL Server 2008 with ADO that always performed very well. In the last 5 days the app started to hung when querying data using an standard SELECT w/joins, causing a timeout in the application.
The problem is this: if I execute the same query in SSMS the query runs just fine (no wait, no hung).
Nothing has changed in the app, in the code, or in the servers (except the latest fixes from Windows Update).
I tried this:
If I regenerate the indexes for all tables used in the SELECT, the app starts to work again (i.e. the query does not block) an minutes later it begins to block again.
I checked sp_lock and the are no exclusive locks on the tables (Only S, IS and Sch-S locks).
I tried setting the ADO cursors to READ-ONLY. This improved the performance but the query will blocks from time to 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.

Does SQL Server share sessions?

In SQL Server 2014 I open 3 sessions on the same database. In the first session I run Update Statistics A. I time this to take around 1 minute.
In my 2nd and 3rd sessions I run an Update Statistics B (one at a time). Each takes about 1 minute as well.
I then run Update Statistics A on session 1, and Update Statistics B on session 2, both at the same time. Each query finishes in around 1 minute, as expected.
I then run Update Statistics A on window 1, and Update Statistics B on window 3, both at the same time. Each query takes close to 2 minutes now.
I checked sp_who2 and can see 3 distinct sessions here. What could be a possible cause for this?
Also, when I check the query status I noticed in the scenario where I run queries on windows 1 and 3, one status is always running while the other is either runnable or suspended. In the other scenario where I run on windows 1 and 2 both are always running.
Sessions are always different. You can see the SPID in the bottom of the SQL Server Management Studio Window or you can do a:
SELECT ##SPID
to see the session number. SQL Server is a multiple access simultaneous system which manages it own sessions and threads. The number of active sessions is dependent on what the sessions are doing and long a session has been doing something and how much resources the machine that SQL Server is running on - typically more memory/CPUS will result in more active simultaneous sessions. Of course there could be a few heavy sessions that use all the resource of the machine.
Sessions are somewhat isolated but depending on the locking being used the sessions are somewhat, or very, isolated from each other. SQL Server will decide when to temporarily suspend a session so other sessions can have a turn using the CPU/Memory. If a whole bunch of sessions are all updating the same table there may be contention that will slow SQL Server down. What you are asking is how long does it take to drive a 100 miles. Depends of course.
If you have three sessions doing the same thing SQL Server will not fold up the three actions into one. It will do each one (simultaneously if possible) or serially.

Why does READPAST work in SSMS but not via OLEDB?

We're trying to use READPAST in a SQL select statement to extract data from a SQL Server 2008 database using QlikView, which is set up to use OLEDB connection to the database.
The reason for this being that we want to avoid being locked by other processes but also don't want to read any uncommitted data - otherwise we'd be using NOLOCK.
We tested the approach in SSMS initially - starting a transaction, adding a row, then separately querying the table with READPAST. This didn't return the uncommitted row as we'd want
We then added this to our OLEDB SQL query (same query, same database) in QlikView and ran the code. This time it waited for the transaction to be closed (committed or rolled back) before it finished the query.
We also tried with ODBC and SQL Native Client that are both supported by QlikView but got the same results.
We also tried with NOLOCK as the hint instead and this performs as expected - it returns the uncommitted row in both SSMS and QlikView.
Any idea why this would work in SSMS and not via OLEDB/ODBC/SQLNC?
Is there a configuration option on the database or the connection that needs changing?

sql query notification service issue

We are getting timeout issues on our databases. So I trurned on SQL Server Profiler and see SQLQueryNotificationService running every second with long duration. I checked the Service Broker and there are bunch of SQLQueryNotificationService queues created. I don't think we created any of these queues also there are bunch of stored procedures like these SqlQueryNotificationStoredProcedure-15c5b84b-42b0-4cfb-9707-9b1697f44127. Could you please let me know how to drop them? If I drop them is there any impact on the database? Please let me know. I appreciate any suggestions.
Do you have an ASP.Net web site running or another application that creates Sql Server Cache dependencies? It is Sql Server Service Broker queues, it executes that WAITFOR ... statement which waits for around one minute (60000 msecs), then executes again next second. Shouldn't normally cause problems, it shouldn't block or delay your "normal" queries or stored procedures.
However, I saw it causing issues for me once - one of the stored procedures, when executed from the same web application that established the cache dependency, did timeout (or rather came back in 120 secs which is not acceptable). Exactly the same stored procedure, executed under the same account with same parameters, but from Management Studio, ran fine without any issues. It was SQL Server 2005 SP4.
SQL Profiler showed that in the middle of execution of my stored procedure (and always after the same INSERT INTO ... statement), its execution was interrupted and instead of its statement there was that WAIT FOR .... from Sql Query Notification, completed in one minute, then another WAIT FOR... starting and again, completed in 59 secs - and only after that the Profiler showed me my stored procedure completed. With the duration of 119000, which is almost exactly two minutes.
It was if that query notifications were joining the transaction within my stored procedure.
What helped: recompiled the offending stored procedure. I simply changed its script, did ALTER statement with some minor syntax changes. No problems after that.

Stored procedure/query executing forever (it does not complete or timeout)

I am using SQL server 2005, SQL server studio client. I am having a long stored procedure (it does a bunch of table joins, some delete, some insert and some updates) periodically running (approximately every other 2 minutes).
After I have this sp, I noticed that my database is sometimes not responding (it happens a few times when the SP is not running, and many times during the SP is running). When the DB is not responding, i can't open new connection from the SQL server studio client, if i run a query/sp the status will become running and stays that forever, until i manually reset the SQL service from control panel admin-tools.
Have you seen similar problems?
Is it perhaps because the SP I newly created is doing too much things and cause the DB to crash?
A few more quick tips:
You can use the sp_who2 command to view current sessions on the DB and determine if the SP is blocked, or blocking other processes.
Check the estimated execution plan for the SP, and look for sources of slowness, like table scans.
Your SP might be making too many changes in a single transaction and filling up the transaction log or causing it to grow. Inspect your DB's recovery mode, and whether backups are occurring regularly so that transaction log space can be reused. Consider batching large modifications into smaller chunks.
It sounds like your SP might be causing locks. You could use SQL Profiler to try and dig deeper into what might be happening. Here's a link providing further info -
http://www.simple-talk.com/sql/learn-sql-server/how-to-track-down-deadlocks-using-sql-server-2005-profiler/

Resources