相关疑难解决方法(0)

Joining a function and a view

我有一个功能 get_sa001 and a view Axis_RefCustomer.

当我get_sa001在一段时间内单独执行我的函数时,比如说 2016 - 2017,执行时间约为 6 秒。

SELECT d."Selling_date",  d."Value_in_EUR", d."Value_in_currency", d."Site"
FROM report.get_sa001('2016-01-01'::date, '2017-03-31'::date, 32) AS d
Run Code Online (Sandbox Code Playgroud)

当我在视图上执行选择时Axis_RefCustomer,它运行大约 1 秒。

Select a."Selling_currency" FROM report."Axis_RefCustomer" AS a
Run Code Online (Sandbox Code Playgroud)

当我将它们连接在一起时,执行时间约为 39 秒!

SELECT d."Selling_date",
a."Selling_currency", 
d."Value_in_EUR", 
d."Value_in_currency", 
d."Site"
FROM report.get_sa001('2016-01-01'::date, '2017-03-31'::date, 32) AS d 
LEFT JOIN report."Axis_RefCustomer" 
AS a ON d."Site" = a."Site" 
AND d."Internal_reference" = a."Reference_internal" 
AND d."Customer_code" = a."Customer_code"
Run Code Online (Sandbox Code Playgroud)

Is there anyway to reduce the amount of time my query …

postgresql performance execution-plan plpgsql

5
推荐指数
1
解决办法
169
查看次数

如何将时间四舍五入到任意时间间隔的上倍数?

例子:

  • 如果当前时间为 2018-05-17 22:45:30 且所需时间间隔为INTERVAL '5 minute',则所需输出为 2018-05-17 22:50:00。
  • 如果当前时间为 2018-05-17 22:45:30 且所需时间间隔为INTERVAL '10 minute',则所需输出为 2018-05-17 22:50:00。
  • 如果当前时间为 2018-05-17 22:45:30 并且所需的时间间隔为INTERVAL '1 hour',则所需的输出为 2018-05-17 23:00:00。
  • 如果当前时间是 2018-05-17 22:45:30 并且期望的间隔是INTERVAL '1 day',那么期望的输出是 2018-05-18 00:00:00。

postgresql datetime interval date-math

5
推荐指数
1
解决办法
3613
查看次数

带有 SELECT 的 SQL 函数与带有 RETURN QUERY SELECT 的 PLPGSQL 函数?

执行 SQL 查询的普通 SQL 函数之间是否有区别:

create function get_sports() returns setof sport as
$body$
    select * from sport;
$body$
language sql stable;
Run Code Online (Sandbox Code Playgroud)

和 PLPGSQL 函数返回相同的 SQL 查询:

create function get_sports() returns setof sport as
$body$
begin
    return query select * from sport;
end
$body$
language plpgsql stable;
Run Code Online (Sandbox Code Playgroud)

关于性能?在什么情况下应该使用哪个版本?

如果我们传递参数,那会改变什么吗?如:

create function get_sports(status int) returns setof sport as
$body$
    select * from sport where status = $1;
$body$
language sql stable;

create function get_sports(status int) returns setof sport as
$body$
begin
    return …
Run Code Online (Sandbox Code Playgroud)

postgresql performance plpgsql functions postgresql-performance

5
推荐指数
1
解决办法
1915
查看次数

什么时候使用存储过程/用户定义函数?

我对 SP(存储过程)和 UDF(用户定义函数)的使用感到困惑。通常,也可以在数据库之外的程序中编写代码。

是否有任何一般建议来决定何时使用它们?

postgresql stored-procedures functions

4
推荐指数
1
解决办法
9419
查看次数

使用动态 SQL 函数中的参数进行通配符搜索

在使用动态 SQL 的函数中使用参数时,在 PostgreSQL 中实现通配符搜索的正确方法是什么?

作为起点,以下是 Erwin Brandstetter 在 stackoverflow 上回答不同问题的示例:

/sf/answers/843309421/

CREATE OR REPLACE FUNCTION report_get_countries_new (starts_with text
                                                   , ends_with   text = NULL)
RETURNS SETOF lookups.countries AS
$func$
DECLARE
   sql text := 'SELECT * FROM lookups.countries WHERE country_name >= $1';
BEGIN
   IF ends_with IS NOT NULL THEN
      sql := sql || ' AND country_name <= $2';
   END IF;

   RETURN QUERY EXECUTE sql
   USING starts_with, ends_with;
END
$func$ LANGUAGE plpgsql;
Run Code Online (Sandbox Code Playgroud)

假设country_name您想要进行前导和尾随通配符搜索。

例如,不使用参数,AND country_name LIKE '%ic%'.

在这种情况下,在否定 …

postgresql sql-injection dynamic-sql plpgsql

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

如何创建一个返回值的 postgres 函数

我正在尝试将一些网络应用程序逻辑转移到 postgres 函数中。但是我在创建一个非常基本的插入函数时遇到了一些错误。

这是我正在尝试创建的功能;

CREATE OR REPLACE FUNCTION create_user(IN email EMAIL, password TEXT, thumb TEXT)
RETURNS text AS 
$BODY$
BEGIN
   insert into users (unqid, thumb, email, password) 
   values (gen_random_uuid(), thumb, email, password)
   returning unqid ;
END;
$BODY$
  LANGUAGE plpgsql
  VOLATILE
Run Code Online (Sandbox Code Playgroud)

如果插入成功,我试图让函数返回项目的 uuid。我这样称呼它;

select * from create_user('newuser@mail.com', 'passpopcorn', 'thumbelinaurl');
Run Code Online (Sandbox Code Playgroud)

收到此错误;

SQL Error [42601]: ERROR: query has no destination for result data
  Where: PL/pgSQL function create_user(email,text,text) line 3 at SQL statement
Run Code Online (Sandbox Code Playgroud)

从我的谷歌搜索看来,当查询中没有 return 语句时,似乎会出现此错误。但在我的插入查询中,我确实有一个返回语句。

额外问题;对于简单的插入语句(例如这里的这个语句,或者带有几个选择后跟一个插入的语句),函数或过程会是更好的选择吗?

postgresql insert functions

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