473,765 Members | 1,963 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Mysterious performance of query because of plsql function in where condition

Hi there,

i have a problem with a query that uses the result of a plsql function
In the where clause:

SELECT
assignments.ass ignment_id,
assignments.pac kage_id AS package_id,
assignments.tit le AS title,
COUNT(*) AS Count
FROM
assignments INNER JOIN submissions ON
(assignments.as signment_id=sub missions.assign ment_id)
WHERE
package_id=9495 89 AND
submission_stat us(submissions. submission_id)= 'closed'
GROUP BY
assignments.ass ignment_id, assignments.pac kage_id, assignments.tit le
ORDER BY
assignments.tit le;

Postgres seems to execute the function "submission_sta tus" for every row
of the submissions table (~1500 rows). The query therefore takes quite a
lot time, although in fact no row is returned from the assignments table
when the condition package_id=9495 89 is used.

QUERY PLAN

------------------------------------------------------------------------
------------------------------------------------------
Sort (cost=41.21..41 .21 rows=1 width=35) (actual
time=4276.978.. 4276.978 rows=0 loops=1)
Sort Key: assignments.tit le
-> HashAggregate (cost=41.19..41 .20 rows=1 width=35) (actual
time=4276.970.. 4276.970 rows=0 loops=1)
-> Hash Join (cost=2.40..41. 18 rows=1 width=35) (actual
time=4276.966.. 4276.966 rows=0 loops=1)
Hash Cond: ("outer".assign ment_id =
"inner".assignm ent_id)
-> Seq Scan on submissions (cost=0.00..38. 73 rows=9
width=4) (actual time=10.902..42 76.745 rows=38 loops=1)
Filter: (submission_sta tus(submission_ id) =
'closed'::text)
-> Hash (cost=2.40..2.4 0 rows=2 width=35) (actual
time=0.058..0.0 58 rows=0 loops=1)
-> Seq Scan on assignments (cost=0.00..2.4 0
rows=2 width=35) (actual time=0.015..0.0 52 rows=2 loops=1)
Filter: (package_id = 949589)
Total runtime: 4277.078 ms
(11 rows)

I therefore tried to rephrase the query, to make sure that the function
is only used for the rows returned by the join but not even the
following does help (the subselect t1 does not return a single row):

select * from (
SELECT
a.assignment_id , a.package_id, a.title, s.submission_id ,
COUNT(*) AS Count
FROM
assignments a INNER JOIN submissions s ON
(a.assignment_i d=s.assignment_ id)
WHERE
a.package_id=94 9589
GROUP BY
a.assignment_id , a.package_id, a.title, s.submission_id
) t1
where
submission_stat us(t1.submissio n_id)='closed'
order by
title;

QUERY PLAN

------------------------------------------------------------------------
--------------------------------------------------------------
Sort (cost=41.21..41 .22 rows=1 width=188) (actual
time=4114.251.. 4114.251 rows=0 loops=1)
Sort Key: title
-> Subquery Scan t1 (cost=41.20..41 .20 rows=1 width=188) (actual
time=4114.242.. 4114.242 rows=0 loops=1)
-> HashAggregate (cost=41.20..41 .20 rows=1 width=39) (actual
time=4114.238.. 4114.238 rows=0 loops=1)
-> Hash Join (cost=2.40..41. 18 rows=1 width=39) (actual
time=4114.235.. 4114.235 rows=0 loops=1)
Hash Cond: ("outer".assign ment_id =
"inner".assignm ent_id)
-> Seq Scan on submissions s (cost=0.00..38. 73
rows=9 width=8) (actual time=7.179..411 3.984 rows=38 loops=1)
Filter: (submission_sta tus(submission_ id) =
'closed'::text)
-> Hash (cost=2.40..2.4 0 rows=2 width=35) (actual
time=0.100..0.1 00 rows=0 loops=1)
-> Seq Scan on assignments a
(cost=0.00..2.4 0 rows=2 width=35) (actual time=0.045..0.0 94 rows=2
loops=1)
Filter: (package_id = 949589)
Total runtime: 4114.356 ms
(12 rows)

The function is nevertheless executed for every row in the submissions
table. A simple "select *, submission_stat us(submission_i d) from
submissions" takes about the same time as the 2 queries stated above.

The whole database has been vacuum analysed right before the explain
analyse output has been captured.

What can I do to reduce the time this query takes? And why is the
function executed although there is no row in the result set of t1 in my
rephrased query?

TIA, peter

--
pe***********@w u-wien.ac.at Tel: +43/1/31336/4341
Abteilung für Wirtschaftsinfo rmatik, Wirtschaftsuniv ersitaet Wien,
Austria

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postg resql.org so that your
message can get through to the mailing list cleanly

Nov 23 '05 #1
1 2015
Do you have any indexes created on the submissions table? If not
postgresql has no choice but read every row in the table.

If you do they are not being used. The tuning masters would really need
to see definition of the table if the indexes are not being used.

You might find a lot of pointers in the pgsql-performance mailing list
instead of this one.

Mike
On Thu, 2004-07-01 at 06:52, Peter Alberer wrote:
Hi there,

i have a problem with a query that uses the result of a plsql function
In the where clause:

SELECT
assignments.ass ignment_id,
assignments.pac kage_id AS package_id,
assignments.tit le AS title,
COUNT(*) AS Count
FROM
assignments INNER JOIN submissions ON
(assignments.as signment_id=sub missions.assign ment_id)
WHERE
package_id=9495 89 AND
submission_stat us(submissions. submission_id)= 'closed'
GROUP BY
assignments.ass ignment_id, assignments.pac kage_id, assignments.tit le
ORDER BY
assignments.tit le;

Postgres seems to execute the function "submission_sta tus" for every row
of the submissions table (~1500 rows). The query therefore takes quite a
lot time, although in fact no row is returned from the assignments table
when the condition package_id=9495 89 is used.

QUERY PLAN

------------------------------------------------------------------------
------------------------------------------------------
Sort (cost=41.21..41 .21 rows=1 width=35) (actual
time=4276.978.. 4276.978 rows=0 loops=1)
Sort Key: assignments.tit le
-> HashAggregate (cost=41.19..41 .20 rows=1 width=35) (actual
time=4276.970.. 4276.970 rows=0 loops=1)
-> Hash Join (cost=2.40..41. 18 rows=1 width=35) (actual
time=4276.966.. 4276.966 rows=0 loops=1)
Hash Cond: ("outer".assign ment_id =
"inner".assignm ent_id)
-> Seq Scan on submissions (cost=0.00..38. 73 rows=9
width=4) (actual time=10.902..42 76.745 rows=38 loops=1)
Filter: (submission_sta tus(submission_ id) =
'closed'::text)
-> Hash (cost=2.40..2.4 0 rows=2 width=35) (actual
time=0.058..0.0 58 rows=0 loops=1)
-> Seq Scan on assignments (cost=0.00..2.4 0
rows=2 width=35) (actual time=0.015..0.0 52 rows=2 loops=1)
Filter: (package_id = 949589)
Total runtime: 4277.078 ms
(11 rows)

I therefore tried to rephrase the query, to make sure that the function
is only used for the rows returned by the join but not even the
following does help (the subselect t1 does not return a single row):

select * from (
SELECT
a.assignment_id , a.package_id, a.title, s.submission_id ,
COUNT(*) AS Count
FROM
assignments a INNER JOIN submissions s ON
(a.assignment_i d=s.assignment_ id)
WHERE
a.package_id=94 9589
GROUP BY
a.assignment_id , a.package_id, a.title, s.submission_id
) t1
where
submission_stat us(t1.submissio n_id)='closed'
order by
title;

QUERY PLAN

------------------------------------------------------------------------
--------------------------------------------------------------
Sort (cost=41.21..41 .22 rows=1 width=188) (actual
time=4114.251.. 4114.251 rows=0 loops=1)
Sort Key: title
-> Subquery Scan t1 (cost=41.20..41 .20 rows=1 width=188) (actual
time=4114.242.. 4114.242 rows=0 loops=1)
-> HashAggregate (cost=41.20..41 .20 rows=1 width=39) (actual
time=4114.238.. 4114.238 rows=0 loops=1)
-> Hash Join (cost=2.40..41. 18 rows=1 width=39) (actual
time=4114.235.. 4114.235 rows=0 loops=1)
Hash Cond: ("outer".assign ment_id =
"inner".assignm ent_id)
-> Seq Scan on submissions s (cost=0.00..38. 73
rows=9 width=8) (actual time=7.179..411 3.984 rows=38 loops=1)
Filter: (submission_sta tus(submission_ id) =
'closed'::text)
-> Hash (cost=2.40..2.4 0 rows=2 width=35) (actual
time=0.100..0.1 00 rows=0 loops=1)
-> Seq Scan on assignments a
(cost=0.00..2.4 0 rows=2 width=35) (actual time=0.045..0.0 94 rows=2
loops=1)
Filter: (package_id = 949589)
Total runtime: 4114.356 ms
(12 rows)

The function is nevertheless executed for every row in the submissions
table. A simple "select *, submission_stat us(submission_i d) from
submissions" takes about the same time as the 2 queries stated above.

The whole database has been vacuum analysed right before the explain
analyse output has been captured.

What can I do to reduce the time this query takes? And why is the
function executed although there is no row in the result set of t1 in my
rephrased query?

TIA, peter

--
pe***********@w u-wien.ac.at Tel: +43/1/31336/4341
Abteilung für Wirtschaftsinfo rmatik, Wirtschaftsuniv ersitaet Wien,
Austria

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postg resql.org so that your
message can get through to the mailing list cleanly


---------------------------(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

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

Similar topics

3
3254
by: Shankar | last post by:
Hello, I am seeing huge performance problems on the queries executed against 9i database. I am not too familiar with 9i, But I would like to ask the DBA to check whether all the parameters are set right to gain optimum performance. Currently the default optimizer is set to first_rows. Most of the queries executed against this database have group by clause
0
2503
by: SteveS | last post by:
Can anyone help with a mysterious problem that has arisen since 'upgrading' from 8 to 9.2.0.4? The situation is this: Queries that worked fine under 8 are now producing *really* strange results under 9.2.0.4. The queries all involve functions, including built-in functions like NVL. The strangeness is that less than the expected number of rows is returned *or* and ORA-01422 is generated. Here's an example:
6
3786
by: Jason | last post by:
I have a function which performs a query and returns a table. The one parameter that can get passed in is a date which defaults to NULL. There is an IF statement in the function that will set the paramter to an actual date if null. If I call the function while passing in a date the function comes back a second or 2 later. But if I pass in DEFAULT to the function, the same query takes 8 minutes. See code below and sample call below. ...
3
5224
by: Paul Mateer | last post by:
Hi, I have been running some queries against a table in a my database and have noted an odd (at least it seems odd to me) performance issue. The table has approximately 5 million rows and includes the following columns: DocID (INTEGER, PRIMARY KEY, CLUSTERED) IsRecord (INTEGER, NONCLUSTERED)
3
1793
by: Jim Geissman | last post by:
I have function that returns a table of information about properties. The data comes from three different tables -- addresses (called PropertyID), property characteristics, and events concerning those properties (sales, appraisals, etc.), plus a table that maps one representation of property types into another. The records are selected on the basis of location (longitude & latitude), property type, event type, and a range of event dates...
2
1316
by: Robin Tucker | last post by:
This: SELECT MAX(TheDate) FROM MyTable or this: SELECT TOP 1 TheDate FROM MyTable ORDER BY TheDate DESC As a follow up question to save me having to post, if I want a different
7
2886
by: Ion | last post by:
Hi all, I have a query that takes almost 1 hour to complete. This is acceptable in certain situations, but unacceptable when no rows should qualify. Something like: Select list >From LargeTable (200 mil rows) Join ExpensiveTableFunction (based on LargeTable)
23
82046
by: Brian | last post by:
Hello All - I am wondering if anyone has any thoughts on which is better from a performance perspective: a nested Select statement or an Inner Join. For example, I could do either of the following: SELECT supplier_name FROM supplier
4
2055
by: bhbgroup | last post by:
I have a query on one large table. I only add one condition, i.e. a date (the SQL reads like 'where date > parameterdate'. This query is rather quick if 'parameterdate' is either explicitly written into the query or if it is a 'normal' access parameter value that is entered during the query. If I however create a separate parameter table that contains nothing but the date I want to use in the query and then refer to this table (the query...
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10163
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9835
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7379
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5276
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.