473,385 Members | 1,555 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

View efficiency questions

Dear Experts,

I have a couple of questions about the efficiency of queries involving
views.

Say I have a large table T, and a view V that just adds some extra
columns to T, using for example some date-to-text formatting functions.
The functions are defined as immutable. Now I "select * from V where
pkey=xxxxx". My hope was that the "where" filter would run on the table
T and the functions would only run on the single row that is returned.
Instead it looks as if the functions are applied to every row, i.e. V is
completely built, and then the one row is selected. (In contrast, if I
don't use a view but put the functions in the select, I think that they
are run only for the selected row.)

Is this the expected behaviour? I can supply a more detailed example if
it would help.
The second case is similar though a little more complex. This time,
rather than immutable functions adding extra columns in the view, it is
joins. For example, T might have codes which are expanded to
human-readable descriptions by joining with a code-to-description table.
Again I select a single row using T's primary key, and hope that the
code-to-description is only done for that one row, but instead it seems
to be done for every row.

Thoughts, anyone?

Regards,

--Phil Endecott.
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 23 '05 #1
2 1538
Phil Endecott <sp**************************@chezphil.org> writes:
Instead it looks as if the functions are applied to every row, i.e. V is
completely built, and then the one row is selected.


This would depend very largely on the details of V and on the details of
the calling query ... not to mention which Postgres version you are using.
In general I would not expect a trivial case to act that way (see
attached counterexample), so I suspect you haven't told us everything.
Give us a concrete example, please ...

regards, tom lane

regression=# create function f1(int) returns int language plpgsql as '
regression'# begin
regression'# raise notice ''f1: %'', $1;
regression'# return $1;
regression'# end';
CREATE FUNCTION
regression=# select * from int4_tbl;
f1
-------------
0
123456
-123456
2147483647
-2147483647
(5 rows)

regression=# create view v1 as select *,f1(f1) as func from int4_tbl;
CREATE VIEW
regression=# select * from v1;
NOTICE: f1: 0
NOTICE: f1: 123456
NOTICE: f1: -123456
NOTICE: f1: 2147483647
NOTICE: f1: -2147483647
f1 | func
-------------+-------------
0 | 0
123456 | 123456
-123456 | -123456
2147483647 | 2147483647
-2147483647 | -2147483647
(5 rows)

regression=# select * from v1 where f1 = 0;
NOTICE: f1: 0
f1 | func
----+------
0 | 0
(1 row)

regression=#

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #2
Hi Tom,

Thanks for writing. On Saturday I had a query that took 3000 ms but
reduced to only 1200 ms when I took out the date formatting functions
from the views. Now it takes only 700 ms with or without the functions.
Perhaps an ANALYZE in the meantime has changed everything.

Anyway, thanks for your example. The "raise notice" is a good debugging
idea. I will post again if the problem comes back and I can construct a
repeatable test case.

Regards,

--Phil.

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Steve M | last post by:
1. Near the beginning of the document "Unifying types and classes in Python 2.2" by GvR, which can be found at http://www.python.org/2.2.2/descrintro.html, the author writes the following about...
13
by: Jeff Melvaine | last post by:
I note that I can write expressions like "1 << 100" and the result is stored as a long integer, which means it is stored as an integer of arbitrary length. I may need to use a large number of...
1
by: Hans-Jürgen Schönig | last post by:
hello ... i am running into a couple of questions regarding materialized view logs. my goal is to take data from an oracle database and replicate it to some remote data store (maybe some other...
2
by: Sara | last post by:
Hi - I've been reading the posts for a solution to my query, and realize that I should ask an "approch" question as well. We receive our production data from a third party, so my uers import...
92
by: Dave Rudolf | last post by:
Hi all, Normally, I would trust that the ANSI libraries are written to be as efficient as possible, but I have an application in which the majority of the run time is calling the acos(...)...
9
by: burningsunorama | last post by:
Hi guys! This is maybe a too 'academic problem', but I would like to hear your opinions, something like pros and cons for each approach.... ... Recently we've had at work a little talk about the...
0
by: connectrajesh | last post by:
INTERVIEWINFO.NET http://www.interviewinfo.net FREE WEB SITE AND SERVICE FOR JOB SEEKERS /FRESH GRADUATES NO ADVERTISEMENT
19
by: vamshi | last post by:
Hi all, This is a question about the efficiency of the code. a :- int i; for( i = 0; i < 20; i++ ) printf("%d",i); b:- int i = 10;
1
by: Itanium | last post by:
Hi all! I'm new to .NET Platform and got some simple questions about efficiency... To put you in situation, to say that I'm involved in the writing of a complex regex based lexer for use over...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.