0

I use a WHERE restriction for one specific Integer column to do SELECT * FROM query, but it always times out without returning any results. But when I use other columns, the query runs normally. I wonder why?

I not sure whether it is cause the specific column is Integer?

SELECT [REQUEST_ID], [SIGNUM], [MATNR], [TIMESTAMP]
FROM [TICKET_INFO]
WHERE [REQUEST_ID] = 0

This does not work and always time out, but this query works normally:

SELECT [REQUEST_ID], [SIGNUM], [MATNR], [TIMESTAMP]
FROM [TICKET_INFO]
WHERE [SIGNUM] = '#'
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Eric Gong
  • 107
  • 1
  • 1
  • 10

3 Answers3

0

The issue might be related with cached query plan which is ok for some parameter versions but not for others. You can try steps to remedy the problem. First rebuild all indexes and ensure statistics on non-indexes are up to date. Try the following:

exec sp_msforeachtable "DBCC DBREINDEX('?')"
go

exec sp_msforeachtable "UPDATE STATISTICS ? WITH FULLSCAN, COLUMNS"
go

If this doesn't work, then try:

  1. Use optimize for
  2. Use WITH(RECOMPILE)
  3. Maybe try dynamic sql (A useful link here)

Reference here: SQL Server Query time out depending on Where Clause

Gauravsa
  • 6,330
  • 2
  • 21
  • 30
  • Still does not work. Which is strange that I cannot modify this field and run ' DELETE From Table WHERE Request_ID = 0 ' query. It seems this field is Blocked. – Eric Gong Apr 10 '19 at 07:04
0

I suspect that Request_ID will be a Character datatype and you are trying to compare it against integer. Due to that, it has to cast the data for every row and do a comparison.

I would suggest you to update the query this way.

SELECT [REQUEST_ID], [SIGNUM], [MATNR], [TIMESTAMP]
FROM [TICKET_INFO]
where [REQUEST_ID] = '0'

Also, read this stackoverflow answer Casting integer to Varchar

Venkataraman R
  • 12,181
  • 2
  • 31
  • 58
  • It still doesn't work, I have used '0' instead of 0 before. But I will try to change Filed ID to Varchar type. – Eric Gong Apr 10 '19 at 05:28
  • Which is strange that I cannot modify this field and run ' DELETE From Table WHERE Request_ID = 0 ' query. It seems this field is Blocked – Eric Gong Apr 10 '19 at 07:05
0

After checking whether it is locked by others, all connection to this server is sleeping, which means all connections are normal. Then I re-start the server by Configuration Management and It can query normally. So the method is RESTART SERVER.

Eric Gong
  • 107
  • 1
  • 1
  • 10