Postgresql 中文操作指南
F.47. tsm_system_time — the SYSTEM_TIME sampling method for TABLESAMPLE #
tsm_system_time 模块提供了表格采样方法 SYSTEM_TIME ,可以在 TABLESAMPLE 命令的 SELECT 子句中使用。
The tsm_system_time module provides the table sampling method SYSTEM_TIME, which can be used in the TABLESAMPLE clause of a SELECT command.
此表采样方法接受一个浮点参数,该参数是读取表时要花费的最大毫秒数。这使你可以直接控制查询花费的时间,代价是样本大小难以预测。结果样本将包含在指定时间内可以读取的行数,除非已经完全读取了整个表。
This table sampling method accepts a single floating-point argument that is the maximum number of milliseconds to spend reading the table. This gives you direct control over how long the query takes, at the price that the size of the sample becomes hard to predict. The resulting sample will contain as many rows as could be read in the specified time, unless the whole table has been read first.
与内置 SYSTEM 采样方法类似,SYSTEM_TIME 执行块级采样,因此样本不是完全随机的,但可能受到聚类效应的影响,尤其是在仅选择很少行的情况下。
Like the built-in SYSTEM sampling method, SYSTEM_TIME performs block-level sampling, so that the sample is not completely random but may be subject to clustering effects, especially if only a small number of rows are selected.
SYSTEM_TIME 不支持 REPEATABLE 条款。
SYSTEM_TIME does not support the REPEATABLE clause.
此模块被认为是“受信任的”,也就是说,它可以由在当前数据库上具有 CREATE 权限的非超级用户安装。
This module is considered “trusted”, that is, it can be installed by non-superusers who have CREATE privilege on the current database.
F.47.1. Examples #
下面是使用 SYSTEM_TIME 选择表样本的示例。首先安装扩展:
Here is an example of selecting a sample of a table with SYSTEM_TIME. First install the extension:
CREATE EXTENSION tsm_system_time;
然后,你可以在 SELECT 命令中使用它,例如:
Then you can use it in a SELECT command, for instance:
SELECT * FROM my_table TABLESAMPLE SYSTEM_TIME(1000);
此命令将返回 my_table 可以在 1 秒内(1000 毫秒)读取的最大样本。当然,如果可以在 1 秒内读取整个表,则将返回其所有行。
This command will return as large a sample of my_table as it can read in 1 second (1000 milliseconds). Of course, if the whole table can be read in under 1 second, all its rows will be returned.