web123456

Suddenly sqlserver queries become very slow

I came to work this morning to find out that the execution was supposed to be completed over the weekend.script executionBy now, relying on the self-built etl log table found that the query of a certain large table was particularly slow to modify .

Later restarted the database (restarted it in the control panel's services)sqlserver), just fine.

Guess the reason: may be because the system's temporary database tempdb full, or is blocked or so on, in the event piece of matter inside to see my process has been in the report RESOURCE_SEMAPHORE wait state , blocking his process is tempdb database, and then guess is not this reason. sqlserver every startup will re-create the tempdb table. The tempdb table is recreated every time sqlserver starts.

--20190305 Update

It's the temporary tables that are the problem, seeDo temporary tables in SQL Server need to be explicitly deleted? _sql server temporary tables do they need to be cleanly deleted - CSDN Blogs

reconsiderStarting the databaseAfter that, the disk space where the data is stored is nearly 100 g more. because I often don't write drop statement inside the query of ssms, may be this cause, before the connection pool is disconnected, the database won't help us to delete the temporary table, and we need to show the deletion. So it is better to show delete temporary table in future.

--

Detailed Description:

It was found that the job took a long time to complete, normally it can be completed in less than 2 minutes, but it took him almost 2 days.

Action at that time: stop the job, re-execute the job, and then find theetlThe logs indicate that it's not executing or that it's stuck. Checking disk io and memory, etc. shows that none of them are moving (this is important to show which statements are not being executed at all)

Here's how the troubleshooting went

1. Check production data for recent high volume data. (No)

2. Is the disk space is not enough. (Check the remaining space of the disk, there are 200G, not this reason)

2. Is there a deadlock (no)

consultationQuery whether a deadlock exists in sqlserver_sql server deadlock check-CSDNblog (loanword)

3. View the execution plan of the table

Since these queries are pretty much fixed queries and nothing was wrong with them before, you can only check with hope.

Here I found a strange thing, there is an index it doesn't go to the index, it favors a full table scan, and I read his suggestion that he wants me to see an index. But I didn't think it was necessary. Later it turned out that this wasn't the reason.

Then record some common check statements

  1. DBCC showcontig('[R_sy_jfrs_year_report]')
  2. --Concerns: Scan Density [Best Count:Actual Count], Logical Scan Fragmentation
  3. --When you find that, scanning density rows, the ratio of optimal counts to actual counts has become grossly out of whack, with a very large percentage of logical scanning debris, per page flat
  4. - - When the average number of available bytes is very large, it's a sign that your index needs to be reorganized
  5. -- then execute, -- rebuild the index
  6. DBCC DBREINDEX('Table_name'')
  1. -View the table that is currently being used, this is a good way to guess where the execution is going in the absence of real-time logs.
  2. select request_session_id spid,OBJECT_NAME(resource_associated_entity_id) tableName
  3. from sys.dm_tran_locks where resource_type='OBJECT'
  4. ORDER BY TABLENAME
  1. --View the indexing of the table
  2. SELECT  
  3. ,  
  4. ,  
  5. ,  
  6. FROM   sysindexes   a  
  7. JOIN   sysindexkeys   b   ON   =   AND   =  
  8. JOIN   sysobjects   c   ON   =  
  9. JOIN   syscolumns   d   ON   =   AND   =  
  10. WHERE     NOT IN (0,255)  
  11. -- and = 'U' and >0 -- check all users table
  12. AND   ='Fact_Trade_log' --Check the specified table
  13. --View database load
  14. SELECT
  15. substring (,0,20) as [database name].
  16. [Number of connections]= (SELECT COUNT(*)
  17. FROM master..sysprocesses b
  18. WHERE
  19. = ),
  20. [blocking process]= (SELECT COUNT(*)
  21. FROM master..sysprocesses b
  22. WHERE
  23. = AND
  24. blocked <> 0),
  25. [Total memory]= ISNULL((SELECT SUM(memusage)
  26. FROM
  27. master..sysprocesses b
  28. WHERE
  29. = ),0),
  30. [General IO]= ISNULL((SELECT SUM(physical_io)
  31. FROM
  32. master..sysprocesses b
  33. WHERE
  34. = ),0),
  35. [Total CPU]= ISNULL((SELECT SUM(cpu)
  36. FROM
  37. master..sysprocesses b
  38. WHERE
  39. = ),0),
  40. [Total waiting time]= ISNULL((SELECT SUM(waittime)
  41. FROM
  42. master..sysprocesses b
  43. WHERE
  44. = ),0)
  45. FROM a WITH (nolock)
  46. WHERE
  47. DatabasePropertyEx(,'Status') = 'ONLINE'
  48. ORDER BY [database name]
  1. --query statement being executed
  2. SELECT spid,
  3. blocked,
  4. DB_NAME() AS DBName,
  5. program_name,
  6. waitresource,
  7. lastwaittype,
  8. ,
  9. ,
  10. a.[Text] AS [TextData],
  11. SUBSTRING(, sp.stmt_start / 2,
  12. (CASE WHEN sp.stmt_end = -1 THEN DATALENGTH() ELSE sp.stmt_end
  13. END - sp.stmt_start) / 2) AS [current_cmd]
  14. FROM AS sp OUTER APPLY sys.dm_exec_sql_text (sp.sql_handle) AS A
  15. WHERE spid > 50

 ---20240611----------------------------------------------------------------------------------------------------------------

Feel free to follow my subscription to discuss

On how PMs grow