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: - SELECT tbl_Obs.PK_Observation,
-
tbl_Obs.mem_Observation,
-
(SELECT TOP 1 U.ID_Status
-
from tbl_ObsUpdate AS U
-
WHERE U.ID_Obs=PK_Observation
-
ORDER BY U.dt_Created DESC) AS Status
-
FROM tbl_Obs
Creating an extra query:
I could create an extra query with the syntax: - SELECT tbl_ObsUpdate.ID_Obs,
-
Last(tbl_ObsUpdate.ID_Status) AS LastOfID_Status
-
FROM tbl_ObsUpdate
-
GROUP BY tbl_ObsUpdate.ID_Obs
-
ORDER BY Max(tbl_ObsUpdate.dt_Created) DESC;
-
And then join this to my main query.
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 ;-)
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...
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.
Any recommendation on how to modify the subquery in the most efficient way?
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).
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: - SELECT MAX(PK) AS MaxPK
-
FROM someTable a
-
INNER JOIN (
-
SELECT
-
groupField,
-
MAX(dateField) AS dt
-
FROM someTable
-
GROUP BY groupField
-
) t ON
-
a.groupField = t.groupField AND
-
a.dateField = t.dt
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.
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.
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 : - SELECT [ID_Obs]
-
, Max([dt_Created]) AS [MaxUpdateDate]
-
FROM tbl_ObsUpdate
-
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.) : - SELECT tbl_Obs.*
-
, tbl_ObsUpdate.*
-
FROM ([tbl_Obs]
-
INNER JOIN
-
[tbl_ObsUpdate]
-
ON tbl_Obs.PK_Observation=tbl_ObsUpdate.ID_Obs)
-
LEFT JOIN
-
[subQ]
-
ON tbl_ObsUpdate.ID_Obs=subQ.ID_Obs
-
AND tbl_ObsUpdate.dt_Created=subQ.MaxUpdateDate
Hope this helps :-)
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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...
|
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.
|
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
|
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...
| |
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.
|
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?
|
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
|
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
|
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
|
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...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |