【问题标题】:PREPARE SQL plan for Postgresql - with bind varible list为 Postgresql 准备 SQL 计划 - 带有绑定变量列表
【发布时间】:2017-08-23 10:19:15
【问题描述】:

我有一个 SQL 命令要与 ​​PREPARE 语句一起使用 谷歌搜索我发现语法是:

PREPARE sqlPlan (bigint) AS
select * from employees 
where employee id in ($1);
EXECUTE usrrptplan2(123);

我有 2 个问题:

  1. 如何在绑定变量部分使用列表/数组(大小未知)?

  2. 这个 Sqls 可以完美运行 - 但我可以使用 "Prepare plan if not exists ?"

自从第二次运行后我得到:ERROR: prepared statement "sqlPlan" already exists

【问题讨论】:

    标签: sql postgresql sql-execution-plan


    【解决方案1】:

    https://www.postgresql.org/docs/current/static/sql-prepare.html

    PREPARE IF NOT EXISTS 不起作用 - 你必须 https://www.postgresql.org/docs/current/static/sql-deallocate.html DEALLOCATE它先。

    关于in参数的数组,例如:

    t=# prepare a(text[]) as select * from pg_class where relname = ANY($1);
    PREPARE
    t=# execute a ('{pg_tables,pg_indexes}');
      relname   | relnamespace | reltype | reloftype | relowner | relam | relfilenode | reltablespace | relpages | reltuples | relallvisible | reltoastrelid | reltoastidxid | relhasindex | relisshared | relpersistence | relkind | relnatts | relchecks | relhasoids |
     relhaspkey | relhasrules | relhastriggers | relhassubclass | relispopulated | relfrozenxid | relminmxid |    relacl     | reloptions
    ------------+--------------+---------+-----------+----------+-------+-------------+---------------+----------+-----------+---------------+---------------+---------------+-------------+-------------+----------------+---------+----------+-----------+------------+
    ------------+-------------+----------------+----------------+----------------+--------------+------------+---------------+------------
     pg_tables  |           11 |   11075 |         0 |       10 |     0 |       11074 |             0 |        0 |         0 |             0 |             0 |             0 | f           | f           | p              | v       |        7 |         0 | f          |
     f          | t           | f              | f              | t              |            0 |          0 | {=r/postgres} |
     pg_indexes |           11 |   11083 |         0 |       10 |     0 |       11082 |             0 |        0 |         0 |             0 |             0 |             0 | f           | f           | p              | v       |        5 |         0 | f          |
     f          | t           | f              | f              | t              |            0 |          0 | {=r/postgres} |
    (2 rows)
    

    【讨论】:

    • 使用绑定变量创建 SQL 计划的全部想法是重用该计划。无论如何,如果存在我可以重复使用?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-11
    • 2018-10-18
    • 2013-04-16
    • 1970-01-01
    • 2018-02-01
    • 1970-01-01
    相关资源
    最近更新 更多