473,795 Members | 2,967 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1951
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******@xs4al l.nl> wrote in message
news:ee******** *************** *****@news1.new s.xs4all.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******@xs4al l.nl> wrote in message
news:ee******** *************** *****@news1.new s.xs4all.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*********@Se eSig.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*********@Se eSig.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
12722
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 read the new date. So process B starts "select ..." but does not get the previously inserted row. The timespan between commit and select is very short. (NOTE: two different sessions are used) Questions: 1.) Does commit when returning from call...
3
1849
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 when 1 then s.fname else null end as fname, case d.t when 1 then s.age else null end as age1, case d.t when 2 then s.fname else null end as fname, case d.t when 2 then s.age else null end as age2 from #temptable s
1
1789
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> <OnCallPerson /> <CallType>Exposure</CallType> <ExposureReason>General</ExposureReason> <OtherExposureReason>Unintentional</OtherExposureReason> <ClientName>Test Client</ClientName>
5
4457
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 of views is that by simply stating 'select * from myviewname where 0=1' that the view is recompiled. If so, what advantages are there in using sp_refreshviews? I couldn't handle the errors that sp_refreshview produced (I am sure due to my lack...
8
3272
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 simultaneously 4 similar queries it takes nearly 5 minutes instead of 4 times 9 seconds or something near of that. here is a sample query: select mertido, fomeazon, ertektipus, mertertek from t_me30 where fomeazon in (select distinct fomeazon...
3
6473
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 COULD be wrong... :) I've tried the access group...twice...and all I get is "Access doesn't like ".", which I know, or that my query names are too long, as there's a limit to the length of the SQL statement(s). But this works when I don't try to...
2
3250
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 neccessary). question 1) The selects can use anything : views, stored procedures, analytic functions, etc...
10
2112
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 Security # or on Last Name. I've created two different tables with the following fields in each table: ClientInfo Client# (primary key) First Name Middle Name Last Name
5
3847
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 table, we try separate this big table into twelve tables and create a view
25
5407
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. Basically i have a multiple select box. An it works except for this one part i want to add to it.The first box i have is called project members which shows the users you can choose to send an email to and the other box is called assignees which is the...
0
9519
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10213
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...
1
10163
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7538
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
5436
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
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3722
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
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.