官术网_书友最值得收藏!

Costs

It is possible to provide hints to the executor about the cost of a function. PostgreSQL query optimizer is a cost-based optimizer, which means it will carefully evaluate the cost of each operation and choose the execution path with the lowest cost. Explaining the cost system and the optimizer is out of the scope of this book, but it is important to know that functions can have a cost estimation.

There are two properties that provide this cost estimation:

  • COST is a positive number indicating how many CPU operations the function will use
  • ROWS is an estimation of the rows returned from a RETURNS SETOF (or RETURNS TABLE) function

The COST property is the more straightforward one: it expresses how many CPU operations the whole execution of the function will use. CPU operations have a cost of cpu_operator_cost each, which is, by default, 1/40 the cost of performing a sequential read from disk. If not specified, the COST for a PL/pgSQL or any non-C language is set to 100.

If the function returns a result set, COST is intended to be the cost of a single tuple from the function result set. Moreover, it is possible to provide the optimizer with a hint about how many tuples a function will return by specifying a positive ROWS number.

As a simple example, a function that performs a simple limited tag query can be declared to have a ROWS property set to the limit itself:

testdb=> CREATE FUNCTION get_ten_tags()
RETURNS SETOF tag AS $code$
BEGIN
RETURN QUERY SELECT *
FROM tags
LIMIT 10;
END $code$ LANGUAGE plpgsql
ROWS 10; -- it is clear this function cannot return more rows

Tuning cost properties can quickly become a complicated task, so it is recommended to add these properties only when they are really needed and to check the impact on query execution carefully.

主站蜘蛛池模板: 丘北县| 前郭尔| 济南市| 滨海县| 察哈| 万山特区| 清流县| 三河市| 德钦县| 邳州市| 安多县| 游戏| 吴川市| 彰化县| 宁阳县| 宣恩县| 板桥市| 酉阳| 高碑店市| 怀宁县| 广州市| 晋城| 孟津县| 呈贡县| 雷波县| 昭通市| 甘谷县| 刚察县| 张掖市| 柳林县| 尉氏县| 高安市| 随州市| 平江县| 白玉县| 康乐县| 遵义市| 青神县| 江山市| 横峰县| 白山市|