New in v1.1:CANCEL QUERY语句取消正在运行的SQL查询。

注意事项

所需权限

root用户可以取消任何当前active的查询,而非root用户只取消他们自己当前acitve的查询。

概要

图片

参数

Parameter Description
query_id scalar表达式,用于生成要取消的查询的ID。

CANCEL QUERY接受单个查询ID。 如果使用子查询并返回多个ID,则CANCEL QUERY语句将失败。

响应

成功取消查询后,CockroachDB会向发出查询的客户端发送query execution canceled错误。

示例

通过查询ID取消查询

在这个例子中,我们使用SHOW QUERIES语句来获取查询的ID,然后将ID传递给CANCEL QUERY语句:

> SHOW QUERIES;
+----------------------------------+---------+----------+----------------------------------+----------------------------------+--------------------+------------------+-------------+-----------+
|             query_id             | node_id | username |              start               |              query               |   client_address   | application_name | distributed |   phase   |
+----------------------------------+---------+----------+----------------------------------+----------------------------------+--------------------+------------------+-------------+-----------+
| 14dacc1f9a781e3d0000000000000001 |       2 | mroach   | 2017-08-10 14:08:22.878113+00:00 | SELECT * FROM test.kv ORDER BY k | 192.168.0.72:56194 | test_app         | false       | executing |
+----------------------------------+---------+----------+----------------------------------+----------------------------------+--------------------+------------------+-------------+-----------+
| 14dacc206c47a9690000000000000002 |       2 | root     | 2017-08-14 19:11:05.309119+00:00 | SHOW CLUSTER QUERIES             | 127.0.0.1:50921    |                  | NULL        | preparing |
+----------------------------------+---------+----------+----------------------------------+----------------------------------+--------------------+------------------+-------------+-----------+
> CANCEL QUERY '14dacc1f9a781e3d0000000000000001';

通过子查询取消查询

在这个例子中,我们嵌套一个SELECT子句,它检索CANCEL QUERY语句中的查询ID:

> CANCEL QUERY (SELECT query_id FROM [SHOW CLUSTER QUERIES]
      WHERE client_address = '192.168.0.72:56194'
          AND username = 'mroach'
          AND query = 'SELECT * FROM test.kv ORDER BY k');

CANCEL QUERY接受单个查询ID。 如果使用子查询并返回多个ID,则CANCEL QUERY语句将失败。

See Also