更新时间:2026-07-28 GMT+08:00
操作指导
前提条件
- 开启PBE查询和存储过程中语句的执行计划进行缓存共享。将enable_global_plancache参数设置为on,具体设置请参见《参考》中“数据库运行参数说明 > GUC参数说明 > 查询规划 > 其他优化器选项”章节。
- 以上操作需要重启集群生效。
- 允许后续执行计划存入全局计划缓存。
SET disable_gpc_store = off;
本示例使用1CN+2DN分布式集群,其他配置结果可能略有不同。
操作步骤
- JDBC用例
- 准备数据。
gaussdb=> CREATE TABLE test_gpc_jdbc(col_a int, col_b int); NOTICE: The 'DISTRIBUTE BY' clause is not specified. Using 'col_a' as the distribution column by default. HINT: Please use 'DISTRIBUTE BY' clause to specify suitable data distribution column. CREATE TABLE gaussdb=> INSERT INTO test_gpc_jdbc VALUES (generate_series(1, 1000), generate_series(1, 1000)); INSERT 0 1000
- 执行JDBC。
// 以下用例以gsjdbc4.jar驱动为例,如果使用其他驱动包,仅需修改驱动类名和数据库连接的url前缀。 // gsjdbc4.jar: 主类名为“org.postgresql.Driver”,数据库连接的url前缀为“jdbc:postgresql”。 public static void main(String[] args) throws IOException { // 链接字符串中prepareThreshold = 1,表示从第一个语句开始就是named PBE。 String urls = "jdbc:postgresql://localhost:48100/postgres?prepareThreshold=1"; // 在环境变量USER、PASSWORD配置用户名和密码 String username = System.getenv("USER"); String passwd = System.getenv("PASSWORD"); Properties urlProps = new Properties(); urlProps.setProperty("user", username); urlProps.setProperty("password", passwd); try { Connection con = DriverManager.getConnection(urls, urlProps); con.setAutoCommit(true); Statement stmt = con.createStatement(); // 为方便快速查看计划存入缓存的情况,设置plan_cache_mode=force_generic_plan。 stmt.execute("set plan_cache_mode = 'force_generic_plan';"); // 调用同一个Connection接口创建prepareStatement并执行语句。 String selectsql = "select * from test_gpc_jdbc where col_a = ? and col_b = ?;"; PreparedStatement pstmt = con.prepareStatement(selectsql); pstmt.setInt(1, 100); pstmt.setInt(2, 200); pstmt.execute(); stmt.close(); pstmt.close(); con.close(); } catch (SQLException e) { e.printStackTrace(); System.exit(1); } } - 登录默认数据库postgres查看GPC视图,可查询到刚执行的语句,最后通过系统视图清理。
gaussdb=> SELECT query, refcount, valid FROM dbe_perf.global_plancache_status WHERE query LIKE '%test_gpc_jdbc%'; query | refcount | valid -------------------------------------------------------------+----------+------- select * from test_gpc_jdbc where col_a = $1 and col_b = $2 | 0 | t select * from test_gpc_jdbc where col_a = $1 and col_b = $2 | 0 | t (2 rows) gaussdb=> SELECT * FROM dbe_perf.global_plancache_clean(); global_plancache_clean ------------------------ t (1 row) gaussdb=> SELECT query, refcount, valid FROM dbe_perf.global_plancache_status WHERE query LIKE '%test_gpc_jdbc%'; query | refcount | valid -------+----------+------- (0 rows) - 删除表。
gaussdb=> DROP TABLE test_gpc_jdbc; DROP TABLE
- 准备数据。
- 函数用例
- 准备数据和函数。
gaussdb=> CREATE TABLE test_gpc_proc(col_a int); NOTICE: The 'DISTRIBUTE BY' clause is not specified. Using 'col_a' as the distribution column by default. HINT: Please use 'DISTRIBUTE BY' clause to specify suitable data distribution column. CREATE TABLE gaussdb=> INSERT INTO test_gpc_proc VALUES(generate_series(1, 1000)); INSERT 0 1000 gaussdb=> CREATE OR REPLACE FUNCTION count_beyond gaussdb-> ( gaussdb(> threshold int gaussdb(> ) gaussdb-> RETURNS int gaussdb-> AS $$ gaussdb$> DECLARE gaussdb$> count_number int; gaussdb$> BEGIN gaussdb$> select count(*) from test_gpc_proc where col_a > threshold into count_number; gaussdb$> return count_number; gaussdb$> END; gaussdb$> $$ LANGUAGE 'plpgsql'; CREATE FUNCTION gaussdb=> SET plan_cache_mode='force_generic_plan'; SET gaussdb=> SET enable_stream_operator=off; SET
- 调用函数并查看GPC视图。
gaussdb=> SELECT count_beyond(200); count_beyond -------------- 800 (1 row) gaussdb=> SELECT query, refcount, valid FROM dbe_perf.global_plancache_status WHERE query LIKE '%test_gpc_proc%'; query | refcount | valid ------------------------------------------------------------+----------+------- select count(*) from test_gpc_proc where col_a > threshold | 1 | t (1 row) - 删除函数及数据。
gaussdb=> DROP FUNCTION count_beyond; DROP FUNCTION gaussdb=> DROP TABLE test_gpc_proc; DROP TABLE
- 准备数据和函数。
父主题: 全局共享计划缓存