- PostgreSQL 11 Server Side Programming Quick Start Guide
- Luca Ferrari
- 317字
- 2021-06-10 19:23:07
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.
- Java編程全能詞典
- Dreamweaver CS3 Ajax網(wǎng)頁設計入門與實例詳解
- PostgreSQL 11 Server Side Programming Quick Start Guide
- 奇點將至
- Visual Studio 2010 (C#) Windows數(shù)據(jù)庫項目開發(fā)
- Web璀璨:Silverlight應用技術完全指南
- 電腦故障排除與維護終極技巧金典
- 從零開始學ASP.NET
- 輸送技術、設備與工業(yè)應用
- Flash 8中文版全程自學手冊
- Internet of Things with Raspberry Pi 3
- 網(wǎng)絡設備規(guī)劃、配置與管理大全(Cisco版)
- 淘寶網(wǎng)店頁面設計、布局、配色、裝修一本通
- Learning Kibana 7(Second Edition)
- 新手學Photoshop CS6數(shù)碼照片處理