我的函数只有一个参数carAzimuth
。
CREATE OR REPLACE FUNCTION map.get_near_link(x numeric, y numeric, carAzimuth numeric)
在里面我有一些计算和这个查询
SELECT *
FROM map.vzla_seg S
WHERE
S.azimuth > carAzimuth - 30 and
S.azimuth < carAzimuth + 30 or
S.azimuth < carAzimuth - 330 or
S.azimuth > carAzimuth + 330;
我要分析查询性能。所以我必须把变量替换为常数。并且工作正常,EXPLAIN PLAN
告诉我是否使用了正确的索引。
EXPLAIN ANALYZE
SELECT *
FROM map.vzla_seg S
WHERE
S.azimuth > 345 - 30 and
S.azimuth < 345 + 30 or
S.azimuth < 345 - 330 or
S.azimuth > 345 + 330;
但麻烦的是,如果要测试不同的值,则更改每个变量。如果你试着
EXPLAIN ANALYZE
WITH param(carAzimuth) as (select 345)
SELECT *
FROM vzla_seg S, param
WHERE
S.azimuth > carAzimuth- 30 and
S.azimuth < carAzimuth + 30 or
S.azimuth < carAzimuth - 330 or
S.azimuth > carAzimuth + 330;
工作,但停止使用索引和更改的FULL SCAN
"Nested Loop (cost=0.01..208990.91 rows=2328905 width=4) (actual time=0.146..4138.882 rows=642115 loops=1)"
" Join Filter: (((s.azimuth > (p.carazimuth - 30)) AND (s.azimuth < (p.carazimuth + 30))) OR (s.azimuth < (p.carazimuth - 330)) OR (s.azimuth > (p.carazimuth + 330)))"
" Rows Removed by Join Filter: 3207719"
" CTE parameter"
" -> Result (cost=0.00..0.01 rows=1 width=0) (actual time=0.002..0.002 rows=1 loops=1)"
" -> CTE Scan on parameter p (cost=0.00..0.02 rows=1 width=4) (actual time=0.009..0.012 rows=1 loops=1)"
" -> Seq Scan on vzla_seg s (cost=0.00..93496.22 rows=3849822 width=4) (actual time=0.072..1380.356 rows=3849834 loops=1)"
"Total runtime: 4253.113 ms"
那么,我是否可以在没有函数的情况下创建变量并在解释中使用它呢?
我使用pgAdmin上的SQL编辑器来运行我的查询。
发布于 2016-07-14 06:43:16
假设WITH
子句是等价函数,这是错误的。子句WITH
与https://www.postgresql.org/docs/current/static/queries-with.html相关,与函数的关系为零。当您希望从函数中看到计划时,有两种可能性:
auto_explain.log_nested_statements
https://stackoverflow.com/questions/38365064
复制相似问题