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

Question on SELECT TOP 3?

I would like to query a database for 3 children whose fathers are the
oldest. The answer isn't necessarily unique: if the oldest father has 6
kids, then any three of his children will do. My first shot was:

SELECT TOP 3 NAME
FROM KID, DAD
WHERE KID.FATHER=DAD.ID
ORDER BY DAD.BIRTHDATE

However, in the above example this yields at least 6 children, as the
top 3 birth dates cover at least the 6 children of the oldest father.

My second attempt was to have the order-by clause enforce distinct rows
by adding a primary key:

SELECT TOP 3 NAME
FROM KID, DAD
WHERE KID.FATHER=DAD.ID
ORDER BY DAD.BIRTHDATE, DAD.ID

This works, although it is a bit slow, due to the double sort.
However, it looks a bit clumsy: is there a better, standard, solution?

Also, when I incorporate this in my Java application, as opposed to
running it as a small test, query performance drops by a factor 50. It
is the one query I haven't been able to incorporate successfully in my
code, and I suspect I hit some special case of resources becoming
scarce. I checked and double checked that I'm closing ResultSets,
Statements and Connections everywhere, I tried to use a new Connection
for this one query, but it didn't make a difference. Are there any
known issues with horrible performance drops in jdbc?

--
Grinnikend door het leven...
Nov 13 '05 #1
4 1922
Adding the unique field to the ORDER BY clause is the appropriate solution.

You could probably get better performance by using an inner join between KID
and DAD instead of the Cartesian product with WHERE clause to thin it out.

Make sure there is an index on the BirthDate field.

Presumably you have created a relationship between KID and DAD, with
enforced referential integrity, so Access should already have the (hidden)
index on Kid.Father.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Izak van Langevelde" <ee******@xs4all.nl> wrote in message
news:ee****************************@news1.news.xs4 all.nl...
I would like to query a database for 3 children whose fathers are the
oldest. The answer isn't necessarily unique: if the oldest father has 6
kids, then any three of his children will do. My first shot was:

SELECT TOP 3 NAME
FROM KID, DAD
WHERE KID.FATHER=DAD.ID
ORDER BY DAD.BIRTHDATE

However, in the above example this yields at least 6 children, as the
top 3 birth dates cover at least the 6 children of the oldest father.

My second attempt was to have the order-by clause enforce distinct rows
by adding a primary key:

SELECT TOP 3 NAME
FROM KID, DAD
WHERE KID.FATHER=DAD.ID
ORDER BY DAD.BIRTHDATE, DAD.ID

This works, although it is a bit slow, due to the double sort.
However, it looks a bit clumsy: is there a better, standard, solution?

Also, when I incorporate this in my Java application, as opposed to
running it as a small test, query performance drops by a factor 50. It
is the one query I haven't been able to incorporate successfully in my
code, and I suspect I hit some special case of resources becoming
scarce. I checked and double checked that I'm closing ResultSets,
Statements and Connections everywhere, I tried to use a new Connection
for this one query, but it didn't make a difference. Are there any
known issues with horrible performance drops in jdbc?

--
Grinnikend door het leven...

Nov 13 '05 #2
Adding the unique field to the ORDER BY clause is the appropriate solution.

You could probably get better performance by using an inner join between KID
and DAD instead of the Cartesian product with WHERE clause to thin it out.

Make sure there is an index on the BirthDate field.

Presumably you have created a relationship between KID and DAD, with
enforced referential integrity, so Access should already have the (hidden)
index on Kid.Father.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Izak van Langevelde" <ee******@xs4all.nl> wrote in message
news:ee****************************@news1.news.xs4 all.nl...
I would like to query a database for 3 children whose fathers are the
oldest. The answer isn't necessarily unique: if the oldest father has 6
kids, then any three of his children will do. My first shot was:

SELECT TOP 3 NAME
FROM KID, DAD
WHERE KID.FATHER=DAD.ID
ORDER BY DAD.BIRTHDATE

However, in the above example this yields at least 6 children, as the
top 3 birth dates cover at least the 6 children of the oldest father.

My second attempt was to have the order-by clause enforce distinct rows
by adding a primary key:

SELECT TOP 3 NAME
FROM KID, DAD
WHERE KID.FATHER=DAD.ID
ORDER BY DAD.BIRTHDATE, DAD.ID

This works, although it is a bit slow, due to the double sort.
However, it looks a bit clumsy: is there a better, standard, solution?

Also, when I incorporate this in my Java application, as opposed to
running it as a small test, query performance drops by a factor 50. It
is the one query I haven't been able to incorporate successfully in my
code, and I suspect I hit some special case of resources becoming
scarce. I checked and double checked that I'm closing ResultSets,
Statements and Connections everywhere, I tried to use a new Connection
for this one query, but it didn't make a difference. Are there any
known issues with horrible performance drops in jdbc?

--
Grinnikend door het leven...

Nov 13 '05 #3
In article
<42***********************@per-qv1-newsreader-01.iinet.net.au>,
"Allen Browne" <Al*********@SeeSig.Invalid> wrote:
Adding the unique field to the ORDER BY clause is the appropriate solution.

You could probably get better performance by using an inner join between KID
and DAD instead of the Cartesian product with WHERE clause to thin it out.

Make sure there is an index on the BirthDate field.

Presumably you have created a relationship between KID and DAD, with
enforced referential integrity, so Access should already have the (hidden)
index on Kid.Father.


The indices are there, and the inner join shows some improvement; thanks
for your suggestions.

In the meantime I learnt the query isn't really the problem, as it takes
between less than one second and more than 40 seconds, depending on the
location in the Java source.

I suspect the jdbc odbc bridge driver plays jokes, and I'll spend some
more time isolating the problem.

--
Grinnikend door het leven...
Nov 13 '05 #4
In article
<42***********************@per-qv1-newsreader-01.iinet.net.au>,
"Allen Browne" <Al*********@SeeSig.Invalid> wrote:
Adding the unique field to the ORDER BY clause is the appropriate solution.

You could probably get better performance by using an inner join between KID
and DAD instead of the Cartesian product with WHERE clause to thin it out.

Make sure there is an index on the BirthDate field.

Presumably you have created a relationship between KID and DAD, with
enforced referential integrity, so Access should already have the (hidden)
index on Kid.Father.


The indices are there, and the inner join shows some improvement; thanks
for your suggestions.

In the meantime I learnt the query isn't really the problem, as it takes
between less than one second and more than 40 seconds, depending on the
location in the Java source.

I suspect the jdbc odbc bridge driver plays jokes, and I'll spend some
more time isolating the problem.

--
Grinnikend door het leven...
Nov 13 '05 #5

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

Similar topics

11
by: Markus Breuer | last post by:
I have a question about oracle commit and transactions. Following scenario: Process A performs a single sql-INSERT into a table and commits the transaction. Then he informs process B (ipc) to...
3
by: ricksql | last post by:
#temptable got order,fname and age. trying to find two high maxes per each order. query returns (1) but (2) is correct answer. supposedly, max(age2) < max(age1). **** select d.order,case d.t...
1
by: Scott | last post by:
The following is the XML I have to work with. Below is the question <Table0> <CaseID>102114</CaseID> <CaseNumber>1</CaseNumber> <DateOpened>2005-06-14T07:26:00.0000000-05:00</DateOpened>...
5
by: Sue | last post by:
I wrote a script that uses the sp_refreshviews. The script will be part of a larger one that is automatically run in multiple databases where different views exist. Question: My understanding...
8
by: Együd Csaba | last post by:
Hi All, how can I improve the query performance in the following situation: I have a big (4.5+ million rows) table. One query takes approx. 9 sec to finish resulting ~10000 rows. But if I run...
3
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I...
2
by: Eitan | last post by:
Hello, I want a solutions for a compicateds sql select statments. The selects can use anything : views, stored procedures, analytic functions, etc... (not use materialized view, unless it is...
10
by: Robert | last post by:
I am an attorney in a non-profit organization and a self-taught programmer. I'm trying to create a client db that will allow me to search for potential conflicts of interest based either on Social...
5
by: wugon.net | last post by:
question: db2 LUW V8 UNION ALL with table function month() have bad query performance Env: db2 LUW V8 + FP14 Problem : We have history data from 2005/01/01 ~ 2007/05/xx in single big...
25
by: bonneylake | last post by:
Hey Everyone, Well i am not sure if my question needs to be here or in coldfusion. If i have my question is in the wrong section i am sorry in advance an will move it to the correct section. ...
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: 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...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.