473,396 Members | 1,666 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,396 software developers and data experts.

Optimal Method for Getting Max Value

I have a non-performing query and am in need of tip or two to melt the
brain freeze I'm currently experiencing to get around it.

I have a large parent table, with an even larger child table (approx 5
rows per parent item). I want to join parent data with only one row
(the most current) from child table. The overall predicate for the
join is highly selective, but is very verbose and involves lots of
tables. I tried materializing the child's data several ways, but
always end up with a big table scan.

In its simplest form

TABLE PARENT
parent_id int (PK),
someinfo varchar(100)
TABLE CHILD
parent_id int (PK),
ts timestamp (PK)
otherinfo varchar(100)

select p.parent_id, p.someinfo, c.ts, c.otherinfo
from parent p
join (
select c.parent_id, max(c.ts) as max_ts
from child c
group by c.parent_id
) as max_child on max_child.parent_id = p.parent_id
join child c on max_child.parent_id = c.parent_id and max_child.max_ts
= c.ts
join <A BUNCH OF OTHER TABLES)
WHERE <LARGE PREDICATE>

The optimizer chooses a table scan for the inline table expression.
I've tried altering this same expression to use the rownumber() OLAP
function instead of the max() with similar slow results. I thought I
had seen, in past behavior, the optimizer's ability to push the
predicate down into the table expression. The only way I have found to
make this happen is to manually push the joins and predicates into the
table expression wherein the performance is restored.

So technically my problem is solved, but it makes for highly
unreadable verbose SQL. Isn't there another way represent that
functionality without the verbosity?

Thanks,
Evan

AIX 5.3 + DB2 8.2 FP16
Oct 29 '08 #1
3 4851
>I have a large Parents [referenced?] table, with an even larger child [referencing?] table (approx 5 rows per Parents [referenced?] item). <<
You are using terminology FROM network databases AND NOT RDBMS. A
table with a singular name will have only one row it in; if it were a
SET, THEN there would be a plural OR collective name. This is very
basic data modeling, so what you have makes it hard to understand
>I want to join Parents [sic] data with only one row (the most current) from child [sic] table. The overall predicate for the join is highly selective, but is very verbose and involves lots of tables. I tried materializing the child's [sic] data several ways, but always end up with a big table scan. <<
Let me try to clean up your personal pseudo-code and amek guesses
about DRI:

CREATE TABLE Parents
(parent_id INTEGER NOT NULL PRIMARY KEY,
some_info VARCHAR (100) NOT NULL);

CREATE TABLE Children
(parent_id INTEGER NOT NULL
REFERENCES Parents (parent_id)
ON UPDATE CASCADE
ON DELETE CASCADE,
event_time TIMESTAMP NOT NULL,
PRIMARY KEY (parent_id, event_time),
other_info VARCHAR(100) NOT NULL);

Did I guess right? Here is another guess, using the MAX() OVER()
option to get the desired child for each parent. Untested, of course.

SELECT X.parent_id, X.some_info, X.event_time, X.other_info
FROM (SELECT P.parent_id, P.some_info, C.event_time, C.other_info,
MAX(C.event_time)
OVER (PARTITION BY P.parent_id) AS event_time_max
FROM Parents AS P,
Children AS C,
<a bunch of other tables>
WHERE C.parent_id = P.parent_id
AND <some search conditions on other tables>)
AS X
WHERE X.event_time = X.event_time_max;
Nov 2 '08 #2
Thanks for the tip. Sorry about the confusing scenario. You surmised
correctly on all counts. The actual tables and queries were just too
gargantuan to post and easily digest, so I tried to simplify with
hopes that the gist was transferred. When using the MAX () OVER ()
functionality, the performance has greatly improved and the query is
much easier on the eyes as well. Thanks for enriching my arsenal of
SQL techniques.

Best,
Evan

On Nov 2, 3:10 pm, --CELKO-- <jcelko...@earthlink.netwrote:
>
Did I guess right? Here is another guess, using the MAX() OVER()
option to get the desired child for each parent. Untested, of course.

SELECT X.parent_id, X.some_info, X.event_time, X.other_info
FROM (SELECT P.parent_id, P.some_info, C.event_time, C.other_info,
MAX(C.event_time)
OVER (PARTITION BY P.parent_id) AS event_time_max
FROM Parents AS P,
Children AS C,
<a bunch of other tables>
WHERE C.parent_id = P.parent_id
AND <some search conditions on other tables>)
AS X
WHERE X.event_time = X.event_time_max;
Nov 5 '08 #3
> You surmised correctly on all counts. <<

That is a first :)!! I usually look for a Zebra instead of a horse.
>. When using the MAX () OVER () functionality, the performance has greatly improved and the query is much easier on the eyes as well. Thanks for enriching my arsenal of SQL techniques.<<
I am still trying to collect tricks with the new OVER() options for
another edition of SQL FOR SMARTIES in 4-5 years. If anyone comes up
with something, send it to me!! I have a feeling that there are some
good tricks that involve CASE expressions, DISTINCT aggregate
functions and some other stuff I have not found yet.
Nov 5 '08 #4

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

Similar topics

15
by: sara | last post by:
Hi I'm pretty new to Access here (using Access 2000), and appreciate the help and instruction. I gave myself 2.5 hours to research online and help and try to get this one, and I am not getting...
2
by: CDWaddell | last post by:
I have the following cod private void btnUpdate_Click(object sender, System.EventArgs e TextBox tbMembers = new TextBox() tbMembers = (TextBox) dlClubs.FindControl("tbMembers") string...
6
by: melanieab | last post by:
Hi, Easy question. It seems to me that I'm following the examples correctly, but apparently I'm not. I'm trying to retrieve the data from a row called "row" and a column called "File". This is...
5
by: velu | last post by:
Problem in getting value from textbox & radiobuttonlist to a valuable inside a datagid. I want to insert a record into a table thru datagrid. Here is the code (see below) Private Sub...
4
by: planetthoughtful | last post by:
Hi All, I have a relatively simple web user control I've included in a page that simply presents 3 drop down lists and a submit button, that I use as a date picker (ie one list for day, one for...
8
by: BB | last post by:
Hi, I am using FormView Contol, TextBox (Hidden ) and Submit button whose caption is "View Page" in my aspx page. Here is what I am tryin to do.When page loads I generate dynamic url in text box...
2
by: tony | last post by:
XslCompiledTransform xslt = new XslCompiledTransform(); ASCIIEncoding encodeToByteArray = new ASCIIEncoding(); MemoryStream inputStream = new MemoryStream(); MemoryStream outputStream = new...
4
by: bushi | last post by:
hi ! i have following code to display some text on a web form,after getting it from database. <asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1"> <ItemTemplate>...
1
by: renatois | last post by:
Getting value from GridView Hi guys, I have a GridView and I need to access a value of an ID field on an event. I converted this field into template and the name of the label control in...
1
by: Dharmendrapatel | last post by:
Hi All, I need to get $ACTRUN and others value defined here manully by running command like ACTRUN=`cat /tmp/HOSTSTATUS| tail -1 | awk '{print $2}'`; In short I want to cat file and...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.