2016-05-16 45 views
0

我想在PL/pgSQL(Postgres 9.5)(通过动态执行查询)中创建一个函数以从两个表(广告和街道)返回一些所需的列,在PostgreSQL/postgis数据库中。表'ad'包含87行,而表'street'包含16060行。以下代码给出了我迄今为止所尝试的内容。此代码的执行时间为2.9秒。我知道可以有很多不同的方法来提高执行时间。以下是代码:在PL/pgsql函数中返回几何和其他属性列

Create or Replace Function str_con(ad geometry, street geometry) 
Returns Table(address_id integer, address_locations geometry, nearest_streets geometry, traf_speed numeric, street_type character varying) AS $$ Begin 
    Return Query 
    Execute 'Select 
      address_id, 
      address_locations, 
      nearest_streets, 
      traf_speed, 
      street_type 

     From 
      (
      Select 
       ad.gid As address_id, 
       ad.geom As address_locations, 
       st.geom As nearest_streets, 
       st.trafsp As traf_speed, 
       st.ospmstty As street_type 
      From   
       public.ad, public.st 

       Where ST_DWithin(ad.geom, st.geom, 50.0) 
       Order By address_id, St_Distance(st.geom, ad.geom) 

      ) As foo'; End; $$ Language 'plpgsql'; 

我正在通过此命令调用该函数。

Select str_con(ad.geom, st.geom) from ad 
Join st On st.gid = ad.gid; 

这两张表'ad'和'street'有几何列以及感兴趣区域的地址和街道的其他信息。我想要将几何和其他列作为输出。但是,我得到这个函数调用的输出是这样的:

enter image description here

表示该功能返回的记录集,而不是它是不是需要五个所需的列。有人可以请教我如何从表格广告和街道返回几何和属性的列?

回答

1

要从记录中获取字段,请使用.,后跟字段名称,例如,

SELECT (str_con(ad.geom, st.geom)).street_type FROM ... 

或者通过.*例如获得记录中的所有字段。

SELECT (str_con(ad.geom, st.geom)).* FROM ... 
+0

谢谢!解决了我的问题。 –