Stop queries

From SQLZoo
Jump to navigation Jump to search

Here you are shown how to find long running queries

and to kill them stopping them from using up database resources and

therefore allowing the database to respond quicker.


It is surprisingly easy to produce long running queries. They can be produced mainly through incorrectly written queries such as ones missing a JOIN function when it is needed. Luckily though Oracle, Postgres, MySQL and SQLServer have commands that allow the user to find and terminate these processes.

SELECT username, sid, serial#, TO_CHAR(sysdate, 'YYYY-MM-DD HH24:MI:SS')"CURRENT",
       TO_CHAR(logon_time,'YYYY-MM-DD HH24:MI:SS')"LOGON",
       (sysdate - logon_time)*24*60"MINS"
  WHERE (sysdate - logon_time)*24*60 > 1
    AND username is not NULL
USE master
SELECT spid, nt_username, 
       datediff(s, logon_time, GETDATE())
  FROM sysprocesses
SELECT datid, datname, usename, procpid,
  FROM pg_stat_activity