

 从补丁 198 开始，Amazon Redshift 将不再支持创建新的 Python UDF。现有的 Python UDF 将继续正常运行至 2026 年 6 月 30 日。有关更多信息，请参阅[博客文章](https://www.amazonaws.cn/blogs/big-data/amazon-redshift-python-user-defined-functions-will-reach-end-of-support-after-june-30-2026/)。

# STL\_WLM\_QUERY
<a name="r_STL_WLM_QUERY"></a>

包含在由 WLM 处理的服务类中每次尝试执行查询的记录。

STL\_WLM\_QUERY 对所有用户可见。超级用户可以查看所有行；普通用户只能查看其自己的数据。有关更多信息，请参阅 [系统表和视图中的数据可见性](cm_chap_system-tables.md#c_visibility-of-data)。

此表中的部分或全部数据也可以在 SYS 监控视图 [SYS\_QUERY\_HISTORY](SYS_QUERY_HISTORY.md) 中找到。SYS 监控视图中的数据经过格式化处理，便于使用和理解。我们建议您使用 SYS 监控视图进行查询。

## 表列
<a name="r_STL_WLM_QUERY-table-columns"></a>


| 列名称  | 数据类型  | 描述  | 
| --- | --- | --- | 
| userid | integer | 生成该条目的用户 ID。 | 
| xid  | integer  | 查询或子查询的事务 ID。 | 
| task  | integer  | 用于通过工作负荷管理器跟踪查询的 ID。可与多个查询 ID 关联。如果重新启动了某个查询，则会为该查询分配一个新的查询 ID 但不分配新的任务 ID。 | 
| query  | integer | 查询 ID。如果重新启动了某个查询，则会为该查询分配一个新的查询 ID 但不分配新的任务 ID。 | 
| service\_class  | integer  | 服务类的 ID。有关服务类 ID 的列表，请参阅 [WLM 服务类 ID](cm-c-wlm-system-tables-and-views.md#wlm-service-class-ids)。 | 
| slot\_count | 整数 | 根据为队列设置的并发级别，查询使用的 WLM 查询插槽的数量。默认值为 1。有关更多信息，请参阅 [wlm\_query\_slot\_count](https://docs.amazonaws.cn/redshift/latest/dg/r_wlm_query_slot_count.html)。 | 
| service\_class\_start\_time  | timestamp  | 将查询分配给服务类的时间。这个时间在 UTC 时区。 | 
| queue\_start\_time  | timestamp  | 查询进入服务类的队列的时间。这个时间在 UTC 时区。 | 
| queue\_end\_time  | timestamp  | 查询离开服务类的队列的时间。这个时间在 UTC 时区。 | 
| total\_queue\_time  | bigint  | 查询已在队列中消耗的总微秒数 | 
| exec\_start\_time  | timestamp  | 查询开始在服务类中执行的时间。这个时间在 UTC 时区。 | 
| exec\_end\_time  | timestamp  | 查询在服务类中完成执行的时间。这个时间在 UTC 时区。 | 
| total\_exec\_time  | bigint  | 查询进行执行所消耗的微秒数。 | 
| service\_class\_end\_time  | timestamp  | 查询离开服务类的时间。这个时间在 UTC 时区。 | 
| final\_state  | character(16)  | 保留供系统使用。 | 
| est\_peak\_mem | bigint | 保留供系统使用。 | 
| query\_priority | char(20) | 查询的优先级。可能的值为 n/a、lowest、low、normal、high 和 highest，其中 n/a 表示不支持查询优先级。 | 
| service\_class\_name | character(64) | 服务类名称。有关服务类的更多信息，请参阅 [WLM 系统表和视图](https://docs.amazonaws.cn/redshift/latest/dg/cm-c-wlm-system-tables-and-views.html)。 | 

## 示例查询
<a name="r_STL_WLM_QUERY-sample-queries"></a>

 **查看在队列中等待和执行的平均查询时间** 

以下查询显示 4 以上的服务类的当前配置。有关服务类 ID 的列表，请参阅 [WLM 服务类 ID](cm-c-wlm-system-tables-and-views.md#wlm-service-class-ids)。

以下查询返回针对每个服务类的每个查询在查询队列和执行过程中消耗的平均时间（单位为微秒）。

```
select service_class as svc_class, count(*),
avg(datediff(microseconds, queue_start_time, queue_end_time)) as avg_queue_time,
avg(datediff(microseconds, exec_start_time, exec_end_time )) as avg_exec_time
from stl_wlm_query
where service_class > 4
group by service_class
order by service_class;
```

此查询返回以下示例输出：

```
 svc_class | count | avg_queue_time | avg_exec_time
-----------+-------+----------------+---------------
         5 | 20103 |              0 |         80415
         5 |  3421 |          34015 |        234015
         6 |    42 |              0 |        944266
         7 |   196 |           6439 |       1364399
(4 rows)
```

 **查看在队列中等待和执行的最大查询时间** 

以下查询返回针对每个服务类的每个查询在任何查询队列和执行过程中消耗的总时间量（单位为微秒）。

```
select service_class as svc_class, count(*),
max(datediff(microseconds, queue_start_time, queue_end_time)) as max_queue_time,
max(datediff(microseconds, exec_start_time, exec_end_time )) as max_exec_time
from stl_wlm_query
where svc_class > 5  
group by service_class
order by service_class;
```

```
 svc_class | count | max_queue_time | max_exec_time
-----------+-------+----------------+---------------
         6 |    42 |              0 |       3775896
         7 |   197 |          37947 |      16379473
(4 rows)
```