开发者

Remove redundant function call for column and column's count

Problem

In the following query, plr_stations is called twice:

  1. once to limit the WHERE clause; and
  2. once to count the number of results it returned.

The code resembles:

  SELECT
 m.*,
 s.*,
 (
  SELECT
    count(1)
  FROM
    climate.plr_stations('48.开发者_如何学Go5146','-123.4447')
 ) AS count_stations
  FROM 
 climate.station s,
 climate.measurement m,
 (
  SELECT
    id
  FROM
    climate.plr_stations('48.5146','-123.4447')
 ) stations
  WHERE
    s.applicable AND
    s.id = stations.id AND
    m.station_id = s.id AND ...

The results of this query are then aggregated by a date query.

Solutions

Populate a temporary table, or an array variable, with the results from the function call.

Update #1

The function call will randomly select a sample of stations if too many stations are inside the spherical polygon defined by the parameters to the function call.

Update #2

The date query aggregation that starts the full query looks as follows:

        SELECT 
          extract(YEAR FROM m.taken) AS year_taken,
          avg(m.amount) AS amount,
          count(m.amount) AS count_measurements,
          md.count_stations,
          min(md.elevation) AS elevation_min,
          max(md.elevation) AS elevation_max
        FROM
          climate.measurement m, (
          SELECT
            m.*,
            s.*, ...

Question

How else can the redundant call be eliminated?

Thank you.


Doubtful that it would fold that given that it shouldn't be marked as IMMUTABLE (if I understand the intent as such).

Something along this line should work...depending on your requirements...

with R_stations as (
SELECT
     id,   
     count(1) over () c
      FROM
        climate.plr_stations('48.5146','-123.4447')
)
     SELECT
     m.*,
     s.*,
     stations.c count_stations
      FROM 
     climate.station s,
     climate.measurement m,
     R_stations stations
      WHERE
        s.applicable AND
        s.id = stations.id AND
        m.station_id = s.id AND ...

But it may be easier considering your aggregation to just do this...

 SELECT
 m.*,
 s.*,
 stations.c count_stations
  FROM 
 climate.station s,
 climate.measurement m,
 (SELECT
 id,   
 count(1) over () c
  FROM
    climate.plr_stations('48.5146','-123.4447')

) stations
      WHERE
        s.applicable AND
        s.id = stations.id AND
        m.station_id = s.id AND ...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜