Executing SQL query - getting timeout

SELECT TOP 1 * 
FROM URLForPosting WITH(nolock) 
WHERE status = 0 
ORDER BY newid()

This is a request when I run the code, I get a timeout, even when I run it on SQL Server, I get a timeout.

However, when I do this

SELECT TOP 1 * 
FROM URLForPosting WITH(nolock) 
WHERE status = 0 

It works great.

Also, the first query worked fine until there were records in the records for the first 6 that did not have a total of 8, now it gives time? I created an index by status.

Any suggestions?

+3
source share
2 answers

An alternative way to return a random entry is to use TABLESAMPLE. See how this is done:

SELECT TOP 1 * 
FROM URLForPosting TABLESAMPLE(1) WITH(nolock) 
WHERE status=0 
ORDER BY newid()

TABLESAMPLE Available in SQL Server 2005 and later.

+4
source

, 600K 800K, , . DBA ( - db_owner) DBCC DBREINDEX(URLForPosting).

2005 ALTER INDEX REBUID.

0

Source: https://habr.com/ru/post/1772437/


All Articles