我創建了一個函數。我想在查詢中使用相同的參數不同的地方。當我正常運行查詢時,我得到的查詢結果是31毫秒。當我在不返回結果的函數中使用參數時。查詢正在運行。我該如何解決?使用相同參數的Postgresql超過
查詢結果爲31毫秒。
SELECT t2.id, t2.sk, t2.sk_code, t2.yl,
st_distance(st_transform(st_setsrid(st_point(28.862966895103455, 41.02119524147813), 4326), 500000), t2.geom) AS mf, t2.rc::INTEGER
FROM public.rc t2
where st_intersects(st_buffer(st_transform(st_setsrid(st_point(28.862966895103455, 41.02119524147813), 4326), 500000),400),t2.geom)= true and
st_distance(st_transform(st_setsrid(st_point(28.862966895103455, 41.02119524147813), 4326), 500000), t2.geom) =
(SELECT distinct st_distance(st_transform(st_setsrid(st_point(28.862966895103455, 41.02119524147813), 4326), 500000), t2.geom) from
public.rc t2 where st_intersects(st_buffer(st_transform(st_setsrid(st_point(28.862966895103455, 41.02119524147813), 4326), 500000),400),t2.geom)
=true
group by st_distance(st_transform(st_setsrid(st_point(28.862966895103455, 41.02119524147813), 4326), 500000), t2.geom)
ORDER BY st_distance(st_transform(st_setsrid(st_point(28.862966895103455, 41.02119524147813), 4326), 500000), t2.geom) ASC
limit 1)
ORDER BY yl asc ,st_distance(st_transform(st_setsrid(st_point(28.862966895103455, 41.02119524147813), 4326), 500000), t2.geom) ASC;
函數不能返回任何剛纔運行的東西。
CREATE OR REPLACE FUNCTION public.getplaces(
IN latx double precision,
IN lngx double precision,
OUT id integer,
OUT sk character varying,
OUT sk_code double precision,
OUT yl double precision,
OUT mf double precision,
OUT rc integer)
RETURNS SETOF record AS
$BODY$
SELECT t2.id, t2.sk, t2.sk_code, t2.yl,
st_distance(st_transform(st_setsrid(st_point(getplaces.lngx, getplaces.latx), 4326), 500000), t2.geom) AS mf, t2.rc::INTEGER
FROM public.rc t2
where st_intersects(st_buffer(st_transform(st_setsrid(st_point(getplaces.lngx, getplaces.latx), 4326), 500000),400),t2.geom)= true and
st_distance(st_transform(st_setsrid(st_point(getplaces.lngx, getplaces.latx), 4326), 500000), t2.geom) =
(SELECT distinct st_distance(st_transform(st_setsrid(st_point(getplaces.lngx, getplaces.latx), 4326), 500000), t2.geom) from
public.rc t2 where st_intersects(st_buffer(st_transform(st_setsrid(st_point(getplaces.lngx, getplaces.latx), 4326), 500000),400),t2.geom)
=true
group by st_distance(st_transform(st_setsrid(st_point(getplaces.lngx, getplaces.latx), 4326), 500000), t2.geom)
ORDER BY st_distance(st_transform(st_setsrid(st_point(getplaces.lngx, getplaces.latx), 4326), 500000), t2.geom) ASC
limit 1)
ORDER BY yl asc ,st_distance(st_transform(st_setsrid(st_point(getplaces.lngx, getplaces.latx), 4326), 500000), t2.geom) ASC;
$BODY$
LANGUAGE sql VOLATILE
COST 1000
ROWS 1000;
找出兩個查詢的執行計劃。對於帶參數的查詢,使用'PREPARE'和'EXPLAIN''EXECUTE'六次創建一個帶有參數的準備好的語句,直到您看到計劃中出現$ 1'。如果後者的計劃實際上更糟糕,則使用動態SQL。 –
我用 EXPLAIN從developer.getplaces(40.944709258659690,29.160144542327885)中選擇*結果是「功能掃描getplaces(成本= 0.25..10.25行= 1000寬度= 162)」 – Hermes
另外我用查詢執行格式沒有改變任何東西 – Hermes