473,671 Members | 2,206 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Most efficient SQL to select newest related record

TheSmileyCoder
2,322 Recognized Expert Moderator Top Contributor
I have a table of observations. This table is loaded as a recordset, and processed to be used in a Treeview control.

Part of this process is to determine which icon to display for the record in the treeview. The icon is determined by the status, and the status is determined by the latest related record in tbl_Updates.

Table Date:

tbl_Obs
PK_Obs (Autonumber)
tx_Obs (Text)
... (other fields)

tbl_Status
PK_Status (Autonumber)
tx_Status (Text)

tbl_Updates
PK_Update (Autonumber)
ID_Obs (Foreign key to PK_Obs, Long)
tx_Update
ID_Status (Foreign key to PK_Status)
dt_Date (Date/Time Timestamp of record creation)
... (other fields)

The Program flow:
A observation is created into tbl_Obs. At various points during the lifetime of a observation it will get updated and a new record is created in the update table each time. Update records are new er deleted or overwritten.

An example could be:
Observation (1):
Alignment of tracks is off by 2 cm

Updates:
2012-11-10 Open - Documentation Requested from supplier
2012-11-11 Open - Documentation Received from supplier
2012-11-13 Closed - Documentation evaluated and found satisfying.

The status of the observation is always determined by the latest update. So for the point of loading my information into the treeview I need a query giving me the PK_Obs, tx_Obs and the LATEST (based on date) ID_Status from the observations table.

Sure I can get this done by the use of multiple queries or subqueries, but I wonder what what would be the most efficient way to retrieve this? And why exactly is it the most efficient way?




The (a) subquery way:
Expand|Select|Wrap|Line Numbers
  1. SELECT tbl_Obs.PK_Observation, 
  2.        tbl_Obs.mem_Observation, 
  3.        (SELECT TOP 1 U.ID_Status 
  4.             from tbl_ObsUpdate AS U 
  5.             WHERE U.ID_Obs=PK_Observation 
  6.             ORDER BY U.dt_Created DESC) AS Status
  7.        FROM tbl_Obs

Creating an extra query:
I could create an extra query with the syntax:
Expand|Select|Wrap|Line Numbers
  1. SELECT   tbl_ObsUpdate.ID_Obs, 
  2.          Last(tbl_ObsUpdate.ID_Status) AS LastOfID_Status
  3. FROM     tbl_ObsUpdate
  4. GROUP BY tbl_ObsUpdate.ID_Obs
  5. ORDER BY Max(tbl_ObsUpdate.dt_Created) DESC;
  6.  
And then join this to my main query.
Nov 30 '12 #1
15 3190
NeoPa
32,569 Recognized Expert Moderator MVP
I would expect the most efficient way to be the use of the subquery.

If my understanding of the question is correct in that it boils down to :
Is the subquery or the separate, pre-optimised, QueryDef the more efficient?

My reasoning for this is that with the subquery, the pre-optimisation of the SQL is all done together. That way, it is aware of the whole requirement and is not making assessments based on a scenario that is not its main requirement. In other words, when the QueryDef object is being optimised it will be done without any appreciation of what it's mainly used for. Does that make sense?

Ultimately, time and usage will tell of course ;-)
Nov 30 '12 #2
TheSmileyCoder
2,322 Recognized Expert Moderator Top Contributor
I am getting confused now. The Subquery I wrote is giving me trouble. The recordset loads fine, but when I reach the point where I loop through the recordset by using recordset.FindF irst I run into trouble with error 3354 "At Most One Record ca be returned by this Subquery).

I thought my subquery was returning just 1 record...
Nov 30 '12 #3
NeoPa
32,569 Recognized Expert Moderator MVP
Not necessarily Smiley ;-)

The TOP n predicate actually returns n records in most circumstances, but where there are more than one nth records it will return all nth records as well as all others higher in the order.
Nov 30 '12 #4
TheSmileyCoder
2,322 Recognized Expert Moderator Top Contributor
Any recommendation on how to modify the subquery in the most efficient way?
Nov 30 '12 #5
NeoPa
32,569 Recognized Expert Moderator MVP
If you have the possibility of multiple observations for the same date then I would definitely advise you to use a TimeStamp (Date and Time) field rather than a simple Date field. That obviates the problem (Causes it not to be an issue).
Nov 30 '12 #6
Rabbit
12,516 Recognized Expert Moderator MVP
I have to disagree. I believe the separate query will be quicker. When the query that joins to the MAX query, I believe the engine will then be able to optimize them together.

The issue with the subquery in the SELECT clause is that it references the outer query. This means the subquery must be run multiple times, at least once for each distinct value referenced in the subquery.

Because you can have multiple updates with the same date, you would do something like this:
Expand|Select|Wrap|Line Numbers
  1. SELECT MAX(PK) AS MaxPK
  2. FROM someTable a
  3. INNER JOIN (
  4.    SELECT 
  5.       groupField,
  6.       MAX(dateField) AS dt
  7.    FROM someTable
  8.    GROUP BY groupField
  9. ) t ON
  10.    a.groupField = t.groupField AND
  11.    a.dateField = t.dt
Nov 30 '12 #7
NeoPa
32,569 Recognized Expert Moderator MVP
NeoPa:
If my understanding of the question is correct in that it boils down to :
Is the subquery or the separate, pre-optimised, QueryDef the more efficient?
Since my first response I noticed that this is certainly not a correct understanding in that the two different approaches are different in more ways than simply that. It seems Rabbit's correct in this, for the reason he explained, but frankly knowing him on SQL generally I'd take that as read anyway.
Nov 30 '12 #8
TheSmileyCoder
2,322 Recognized Expert Moderator Top Contributor
Sorry, my bad. The datetime field IS stored as date & time(though formatted to only display date).
I can't test the other replies now, but willl test it when I get back to my PC.
Nov 30 '12 #9
NeoPa
32,569 Recognized Expert Moderator MVP
If it's already a TimeStamp then I would expect it to be very rare that the subquery would produce duplicates. See if you have any.
Smiley:
I need a query giving me the PK_Obs, tx_Obs and the LATEST (based on date) ID_Status from the observations table.
No wonder I was confused earlier. Can I take this to mean [ID_Status] should be from [tbl_ObsUpdate]?

As the subquery (that one particularly) approach is probably not the most efficient one anyway, I would consider something different :

Expand|Select|Wrap|Line Numbers
  1. SELECT   [ID_Obs]
  2.        , Max([dt_Created]) AS [MaxUpdateDate]
  3. FROM     tbl_ObsUpdate
  4. GROUP BY [ID_Obs]
The table and field names might be awry. I checked with your table layouts, but the code you posted didn't match, so I used my best guesses. I mainly went with the code. This is very unlike you Smiley :-D Almost like a noob poster.

Anyway, moving on, with the query above, and I doubt it matters greatly whether it's a subquery or a separately defined QueryDef, you can link up to your main data thus (We'll refer to the above query as [subQ] for now.) :

Expand|Select|Wrap|Line Numbers
  1. SELECT tbl_Obs.*
  2.      , tbl_ObsUpdate.*
  3. FROM   ([tbl_Obs]
  4.        INNER JOIN
  5.        [tbl_ObsUpdate]
  6.   ON   tbl_Obs.PK_Observation=tbl_ObsUpdate.ID_Obs)
  7.        LEFT JOIN
  8.        [subQ]
  9.   ON   tbl_ObsUpdate.ID_Obs=subQ.ID_Obs
  10.  AND   tbl_ObsUpdate.dt_Created=subQ.MaxUpdateDate
Hope this helps :-)
Nov 30 '12 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
3880
by: Belmin | last post by:
Hi all, Wanted to know what is the most efficient way of doing a select query for mysql that only returns one value. For example: $mysqli->query('select count(*) from log'); $temprec = $result->fetch_assoc(); $count = $temprec; That doesn't seem efficient. How should I do it? Or is this as efficient
0
3196
by: elvin | last post by:
Okay - apologize in advance for the length, but I want to make sure all you knowledgeable and helpful people have all the details you need to hopefully point my newbie rear in the right direction. I've got a fairly complex (a lot more complex than originally intended) database with three main tables. The database is designed to track information about certain files. The tables are set up with one to one relationships, primarily because...
4
4047
by: amywolfie | last post by:
I've been trying to do something for about 3 days – I get close, but not 100%. I am trying to: Open frmRevisionHistory from a button on frmFeeInput. If there is NO RELATED RECORD, then I need to create one in frmRevisionHistory, and pass the PK (PK_Fees) on frmFeeInput to FK_Fees on the 2nd form.
4
1819
by: Anders | last post by:
Hi, I was wondering what is most efficient of the two. Is it more efficient to add server controls within the Itemtemplate and use OnItemDataBound to manipulate and databind the servercontrols. Or is better to send the DataItems via. a method/function from within the Itemtemplate (<%# call function %>) Id love to hear your thoughts. Anders
2
1555
by: booksnore | last post by:
..eh I was stuck thinking up a subject title for this post for a while.. So I am processing a really big file (scary big). Each record is fixed length, I need to test conditions on certain fields in the record. At the moment the most efficient way I've found to process the data is a series of nested if/else statements so like below. My question is does anyone know of a better way to process this kind of logic. I can't use a switch...
0
1089
by: kesk | last post by:
Hi, I am a learner in vb.net who is designing a database project. I dont have formal vb.net training, i learned from database books only, to the most extent its good. I have seen on dbl-clicking a datarow value, another form opens with the related record details. I would like to emulate this behaviour in my project, but i dont know the basics of doing this. Whatever examples i have doesnt explain.
2
1431
by: Dan | last post by:
I've got a "Select all" button under a listbox. What's the most efficient way to select all the items in the listbox?
13
2694
by: chrisben | last post by:
Hi, I need to insert more than 500,000 records at the end of the day in a C# application. I need to finish it as soon as possible. I created a stored procedure and called it from ADO to insert one by one. It is kind of slow (seems slower than using a DTS package to import from a file). Just a general question, in ADO, what will be the MOST efficient way to do this work. I normally do it as I described. I am using .NET framework 1.1
1
5730
by: Maxwell2006 | last post by:
Hi, I am working with strongly typed datatables. What is the most efficient way to build a new DataTAble based on the result of DataTable.Select? At this point I use a foreach loop to do the work manually. I am looking for an automated way. Thank you, Max
1
3884
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
Using .NET 2.0 is it more efficient to copy files to a single folder versus spreading them across multiple folders. For instance if we have 100,000 files to be copied, Do we copy all of them to a single folder called 'All Files' Do we spread them out and copy them to multiple folders like Folder 000 - Copy files from 0 to 1000 Folder 001 - Copy files from 1000 to 2000 Folder 002 - Copy files from 2000 to 2999
0
8909
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
8667
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...
0
7428
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6222
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
4221
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
4399
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2806
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
2048
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1801
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.