The problem is different between view and table execution time.
Why there are so many differences between the two queries ?
The Query 2 query is getting faster with the "LIMIT" Clause but it is very slow now.(Like a Select * From ... limit 1000)
Note: the functions I use in the VIEW does not do any hard work. Language plpgsql.
Query 1 : Execution time = 106.726 ms ;
EXPLAIN ANALYZE SELECT * FROM dbo.ST_Foto where StationId='Sample Guid'
and Tarih <='2018-09-12' -- Table
Query 1 Explain Analyze ;
"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 <= '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"
Query 2 Execution Time =30562.223 ms ;
EXPLAIN ANALYZE SELECT * FROM dbo.VWST_Foto where StationId='Sample Guid'
and Tarih <='2018-09-12' -- VIEW
Query 2 Explain Analyze ;
"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 <= '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"
View :
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;
My Server has 32 gb ram.
In Addition
explain (analyze, buffers)
analysis :
For View : (Query 2)
"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 <= '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"
For Table : (Query 1)
"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 <= '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"