title: Postgresql Select view too slow with where clause
body: <p>The problem is different between view and table execution time.</p>
  
  <p>Why there are so many differences between the two queries ?</p>
  
  <p>The Query 2  query is getting faster with the "LIMIT" Clause but it is very slow now.(Like a Select * From ... limit 1000)</p>
  
  <p>Note: the functions I use in the VIEW does not do any hard work. 
  Language plpgsql.</p>
  
  <p>Query 1 : Execution time = 106.726 ms ;</p>
  
  <pre><code>   EXPLAIN ANALYZE SELECT * FROM dbo.ST_Foto    where     StationId='Sample Guid'  
          and  Tarih &lt;='2018-09-12'   -- Table
  </code></pre>
  
  <p>Query 1 Explain Analyze ;</p>
  
  <pre><code>  "Seq Scan on st_foto  (cost=0.00..4559.68 rows=103878 width=215) (actual time=0.036..81.046 rows=103938 loops=1)"
  "  Filter: ((tarih &lt;= '2018-09-12 00:00:00'::timestamp without time zone) AND (stationid = 'ac5a2189-f931-47c0-9845-d0ff8eac7cb7'::uuid))"
  "  Rows Removed by Filter: 20174"
  "Planning time: 0.359 ms"
  "Execution time: 106.726 ms"
  </code></pre>
  
  <p>Query 2 Execution Time =30562.223 ms ;</p>
  
  <pre><code>  EXPLAIN ANALYZE SELECT * FROM dbo.VWST_Foto    where     StationId='Sample Guid'  
              and  Tarih &lt;='2018-09-12'   -- VIEW
  </code></pre>
  
  <p>Query 2 Explain Analyze ;</p>
  
  <pre><code>"Seq Scan on st_foto  (cost=0.00..161415.46 rows=103878 width=439) (actual time=1.364..30510.830 rows=103938 loops=1)"
  "  Filter: ((tarih &lt;= '2018-09-12 00:00:00'::timestamp without time zone) AND (stationid = 'ac5a2189-f931-47c0-9845-d0ff8eac7cb7'::uuid))"
  "  Rows Removed by Filter: 20174"
  "Planning time: 0.517 ms"
  "Execution time: 30562.223 ms"
  </code></pre>
  
  <p>View :</p>
  
  <pre><code>  CREATE    VIEW dbo.vwst_foto AS
       SELECT id,
      created,
      changed,
      createdby,
      changedby,
      intid,
      hardwareid,
      stationid,
      zoom,
      tarih,
      tarihgmt2,
      gunesradyasyon,
      bitkiboyu,
      contrast,
      yesilorani,
      preset,
      rgb,
      fft,
      foto,
      dbo.fn_hardwarefullname(hardwareid) AS hardware_title,
      dbo.fn_stationfullname(stationid) AS station_title,
      dbo.fn_userfullname(createdby) AS createdby_title,
      dbo.fn_userfullname(changedby) AS changedby_title,
      ''::text AS fotograflink,
    /* the basic func */  dbo.fn_enumkeytodescription('ST_Foto'::character varying, 'Zoom'::character varying, zoom::character varying) AS zoom_title,
    /* the basic func */    dbo.fn_enumkeytodescription('ST_Foto'::character varying, 'Preset'::character varying, preset::character varying) AS preset_title
     FROM dbo.st_foto;
  </code></pre>
  
  <p>My Server has 32 gb ram.</p>
  
  <p>In Addition</p>
  
  <blockquote>
    <p>explain (analyze, buffers)</p>
  </blockquote>
  
  <p>analysis :</p>
  
  <p>For View : (Query 2)</p>
  
  <pre><code>"Seq Scan on st_foto  (cost=0.00..161415.46 rows=103878 width=439) (actual time=0.552..30695.394 rows=103938 loops=1)"
  "  Filter: ((tarih &lt;= '2018-09-12 00:00:00'::timestamp without time zone) AND (stationid = 'ac5a2189-f931-47c0-9845-d0ff8eac7cb7'::uuid))"
  "  Rows Removed by Filter: 20174"
  "  Buffers: shared hit=1297179"
  "Planning time: 0.489 ms"
  "Execution time: 30749.816 ms"
  </code></pre>
  
  <p>For Table : (Query 1)</p>
  
  <pre><code>"Seq Scan on st_foto  (cost=0.00..4559.68 rows=103878 width=215) (actual time=0.035..80.967 rows=103938 loops=1)"
  "  Filter: ((tarih &lt;= '2018-09-12 00:00:00'::timestamp without time zone) AND (stationid = 'ac5a2189-f931-47c0-9845-d0ff8eac7cb7'::uuid))"
  "  Rows Removed by Filter: 20174"
  "  Buffers: shared hit=2698"
  "Planning time: 0.352 ms"
  "Execution time: 107.618 ms"
  </code></pre>