SQL: Interrupting a query

你说的曾经没有我的故事 提交于 2019-12-04 04:07:37

Imho "interrupted" should be replaced by 'killed' or 'terminated'. The concept of interrupting can be confusing as one might presume it would allow the query to be resumed later.

The SQL standard does not supply a way to interrupt or terminate a running query, but every DBMS I know does implement a KILL-command or similar. For example, in MySQL a user can use the SHOW [FULL] PROCESSLIST to view all running queries (and their states, query IDs, etc). Users with the KILL privilege can then terminate a query.

Most KILLs happen because a query risks running too long or is blocking other queries, eg. the table is missing an index or the disk is full. When you don't care about the result (eg. user cancelled site navigation), often the webserver itself will abort the process and hence the query in itself (no manual or programmer interaction necessary)

All RDBMS access layers I've ever worked with provide a cancellation method to asynchronously cancel running queries. Check the documentation for whatever data access technology stack you are using. .NET/ADO/JDBC provide a 'cancel' method. ODBC - SQLCancel. Obviously underlying RDBMS vendors data access driver must also implement the method.

In terms of usefullness of cancellation I would tend to be critical of any scheme that made regular use of it. In my opinion better coordination and or design would tend to mitigate non-administrative need.

There is significant dependance on the internals of the RDBMS, nature of the transaction and isolation scheme. If the RDBMS uses an optimistic concurrency model (ie commit is essentially free) canceling a running query can involve a potentially expensive rollback operation. In a worst case a query running for an hour up to the point of cancellation may very well take another hour to rollback.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!