相关疑难解决方法(0)

为什么PostgreSQL在函数中以不同的方式处理我的查询?

我有一个非常简单的查询,它并不复杂:

select *
from table_name
where id = 1234
Run Code Online (Sandbox Code Playgroud)

...运行时间不到50毫秒.

接受该查询并将其放入函数中:

CREATE OR REPLACE FUNCTION pie(id_param integer)
RETURNS SETOF record AS
$BODY$
BEGIN
    RETURN QUERY SELECT *
         FROM table_name
         where id = id_param;
END
$BODY$
LANGUAGE plpgsql STABLE;
Run Code Online (Sandbox Code Playgroud)

执行此功能select * from pie(123);需要22秒.

如果我硬编码整数代替id_param,则该函数在50毫秒内执行.

为什么我在where语句中使用参数会导致我的函数运行缓慢?


编辑以添加具体示例:

CREATE TYPE test_type AS (gid integer, geocode character varying(9))

CREATE OR REPLACE FUNCTION geocode_route_by_geocode(geocode_param character)
  RETURNS SETOF test_type AS
$BODY$
BEGIN
RETURN QUERY EXECUTE
    'SELECT     gs.geo_shape_id AS gid,     
        gs.geocode
    FROM geo_shapes gs
    WHERE …
Run Code Online (Sandbox Code Playgroud)

postgresql postgresql-9.1

3
推荐指数
1
解决办法
2945
查看次数

标签 统计

postgresql ×1

postgresql-9.1 ×1