Database stuck in executing query or slow SQL Server - sql

UPDATE : I tried re-creating another table in that the same database and was able to run select statement just fine. I also tried
SELECT * FROM [UNNIK].[dbo].[leads] WITH(NOLOCK) ORDER BY id DESC ;
which now works. But this is a temporary solution I want to know what is going on?
We have a Database Server which has 50 plus Databases in it. Some are more than 50GB of size but this morning there is this one Database that is running slow or not responding in the query.
This is the only database that doesn't work. Others works fine and this database is just small with 20,000 records only.
When I run a query like
SELECT * FROM [UNNIK].[dbo].[leads] ORDER BY id DESC;
It will be just stuck in executing query status with the circle progress animation.
Even limiting it to 1 query doesn't show anything.
SELECT TOP 1 * FROM [UNNIK].[dbo].[leads] ORDER BY id DESC;
What could be the problem? I am no expert in Database trouble shooting. I am a PHP Developer and most of the knowledge I know in Database is just those queries related to my PHP scripting. I tried inserting a record in the database and it works fine. Just the select statement doesn't work or queries that has something to do with viewing.

Related

Select on SqlDbx and SQL Server throwing differente results, what could be the cause?

I have a service which is synchronizing 2 databases (software provider database, and our website database).
The website never changes the information, it only reads.
We have access to the software provider database connecting with SqlDbx (which I'm barely familiar with). The service synchronizing the databases is somewhat complicated, we have some parameters to see what tables gets synced and how. The service puts all the data into our SQL Server database and works fine. If we drop a table in SQL Server, the service rebuilds the whole table again.
But there is this case which is really weird.
I have this select on the SQL Server database which returns 4 rows:
SELECT SERIAL_NO
FROM TABLE
WHERE ID = 'D00357'
-- I get 4 rows from that.
-- let's say SERIAL1, SERIAL2, SERIAL3 and SERIAL4
But if I go to SqlDbx and run that same query:
SELECT SERIAL_NO
FROM TABLE
WHERE ID = 'D00357'
-- I only get 2 rows
-- let's say SERIAL1 and SERIAL2
Even more mysteriously, if I try to find the other 2 missing rows, by SERIAL_NO, they are there.
SELECT ID
FROM TABLE
WHERE SERIAL_NO = 'SERIAL3' OR SERIAL_NO = 'SERIAL4'
-- this results in 2 rows with 'D000357'
So, the software we using only shows the 2 rows we see from the first SELECT in SqlDbx. But the website shows the 4 rows from SQL Server, so they don't match.
We have dropped the table a few times and the syncing process always syncs back 4 rows.
Any ideas on how could I approach this? What can be causing this? Any suggestions will be very much appreciated.
EDIT
Adding photo link, hope it works.

Simple SQL query is taking too long to respond

I am facing an issue in my SQL server 2008 R2 version previously it was good on executing everything. But from 2 days it not even responding for a small select queries. I didn't do any update or changed any thing but it is now throwing an issue and I couldn't find where is the issue.
I have a table which contains record count of 36 581.
When I am write the simple select query for that table:
SELECT * FROM [TABLE NAME]
It is showing the first 152 records and after that it is not showing any record but taking soo much time which I can say as infinite time as I have seen the time elapsed is around 30 minutes but there is no records extra showed in the result query except those 152 which shown at first.
Try running DBCC CHECKDB on your database like
DBCC CHECKDB('#databasename')

Azure Get Live Queries

I'm looking for a query to get the current running queries in Azure SQL. All of the T-SQL I've found do not show the running queries when I test them (for instance, run a query in one window, then look in another window at the running queries). Also, I'm not looking for anything related to the time, CPU, etc, but only the actual running query text.
When I run ...
SELECT * FROM Table --(takes 2 minutes to load)
... and run a standard information query (like from Pinal Dave or this), I don't see the above query (I assume there's another way).
select * from sys.dm_exec_requests should give you what other sessions are doing.You can join this with sys.dm_exec_sql_text to get the text if needed. sys.dm_tran_locks gives the locks hold / waiting. If this is V12 server you can also use dbcc inutbuffer. Make sure that the connection you are running is dbo / server admin

System.OutOfMemoryException when querying large SQL table

I've written a SQL query that looks like this:
SELECT * FROM MY_TABLE WHERE ID=123456789;
When I run it in the Query Analyzer in SQL Server Management Studio, the query never returns; instead, after about ten minutes, I get the following error: System.OutOfMemoryException
My server is Microsoft SQL Server (not sure what version).
SELECT * FROM MY_TABLE; -- return 44258086
SELECT * FROM MY_TABLE WHERE ID=123456789; -- return 5
The table has over forty million rows! However, I need to fetch five specific rows!
How can I work around this frustrating error?
Edit: The server suddenly started working fine for no discernable reason, but I'll leave this question open for anyone who wants to suggest troubleshooting steps for anyone else with this problem.
According to http://support.microsoft.com/kb/2874903:
This issue occurs because SSMS has insufficient memory to allocate for
large results.
Note SSMS is a 32-bit process. Therefore, it is limited to 2 GB of
memory.
The article suggests trying one of the following:
Output the results as text
Output the results to a file
Use sqlcmd
You may also want to check the server to see if it's in need of a service restart--perhaps it has gobbled up all the available memory?
Another suggestion would be to select a smaller subset of columns (if the table has many columns or includes large blob columns).
If you need specific data use an appropriate WHERE clause. Add more details if you are stuck with this.
Alternatively write a small application which operates using a cursor and does not try to load it completely into memory.

updating temp table from linked server using top clasue

I am trying to update a #temp table from a linked server using a top clause but am getting a very odd result - it seems to be ignoring the order by in my code.
I can fix the specific problem programatically but would like to know if this is a one off issue or there is a general problem with the way i have linked the servers.
The query is being run on a 2005 SQL Server (9.0.3042) linked to a 2008 SQL Server R2 (10.50.279) linked via the Microsoft OLE DB Provider for SQL Server.
The query looks like this - i have already created #TempTable which has the columns Id, Date and PrevDate and inserted data into the Id and Date columns.
update #TempTable
set PrevDate =
(select top 1
d.Date
from
linkedserver.DB.dbo.Date as d
where
d.Id = #TempTable.Id
and d.Date < #TempTable.Date
order by
d.Date desc)
The select is not picking the top 1, it appears to be picking the first date entered into the table for the specific Id and ignoring the order by clause.
When I just do a select, it works fine
When I put in the PrevDate via the inital insert, it works fine.
When I use a Max rather than a Top, it works fine.
When I run the exact same query/scenario on the 2008 SQL Server via a link (same provider) back to the 2005 SQL Server, it works fine
I am not looking for a fix for this specific query but would like to know if this is an isolated incedent or some fundamental problem with 2005 to 2008 linking that is going to manifest itself in very hard to find ways.
Thanks,
Tim
I have some bad news. I have replicated this issue here. It looks to me to be an issue with SQL Server 2005's query plan (I tested with a later version too, v 9.0.4035):
Image: Query plan for update statement
(sorry I can't embed images here, not enough reputation)
The details for that remote query:
Image: Remote Query details
There is no ORDER BY statement sent to the remote server, so the results that come back are indeed just the TOP (1) in an arbitrary order, which is likely to be in the order of the primary key. It seems to me that SQL Server should have emitted an ORDER BY Date desc there.
Therefore, I have to conclude that other queries like this, in this environment, could easily have the same issue (so, not an isolated incident).
By the way, there is a Microsoft Connect issue already raised for this one, from 2009: connect.microsoft.com/SQLServer/feedback/details/446017/missing-order-by-in-remote-query-with-update-statement