473,326 Members | 2,680 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,326 software developers and data experts.

A problem with UNION

Hi, I'm running a query unifying two relatively simple selections.
The problem is that each selection, on its own, completes
instantaneously, but, unified, the processing takes upwards of 30
seconds. Maybe I just don't understand correctly how a union is
processed, but if anyone can explain this (and, preferably, offer a
solution or alternative), I would appreciate it.

The query in question:
SELECT studyCentreRecords.mrn, PSA.patient, PSA.id, PSA.date,
PSA.totalPSA, PSA.detectionLimit
FROM PSA INNER JOIN studyCentreRecords ON
PSA.patient=studyCentreRecords.patient
WHERE PSA.detectionLimit<>'<' AND PSA.totalPSA IN (SELECT PSA.totalPSA
FROM PSA WHERE PSA.detectionLimit='<' AND PSA.totalPSA<>.1)

UNION SELECT studyCentreRecords.mrn, PSA.patient, PSA.id, PSA.date,
PSA.totalPSA, PSA.detectionLimit
FROM PSA INNER JOIN studyCentreRecords ON
PSA.patient=studyCentreRecords.patient
WHERE PSA.detectionLimit='<' AND PSA.totalPSA<>.1 AND
PSA.totalPSA<>.05;

studyCentreRecords contains about 1500 records
PSA contains about 9000 records

Again, each selection on its own is fine.

Thank you,

Adam Louis
Nov 12 '05 #1
2 1511
Adam Louis wrote:
Hi, I'm running a query unifying two relatively simple selections.
The problem is that each selection, on its own, completes
instantaneously, but, unified, the processing takes upwards of 30
seconds. Maybe I just don't understand correctly how a union is
processed, but if anyone can explain this (and, preferably, offer a
solution or alternative), I would appreciate it.

The query in question:
SELECT studyCentreRecords.mrn, PSA.patient, PSA.id, PSA.date,
PSA.totalPSA, PSA.detectionLimit
FROM PSA INNER JOIN studyCentreRecords ON
PSA.patient=studyCentreRecords.patient
WHERE PSA.detectionLimit<>'<' AND PSA.totalPSA IN (SELECT PSA.totalPSA
FROM PSA WHERE PSA.detectionLimit='<' AND PSA.totalPSA<>.1)

UNION SELECT studyCentreRecords.mrn, PSA.patient, PSA.id, PSA.date,
PSA.totalPSA, PSA.detectionLimit
FROM PSA INNER JOIN studyCentreRecords ON
PSA.patient=studyCentreRecords.patient
WHERE PSA.detectionLimit='<' AND PSA.totalPSA<>.1 AND
PSA.totalPSA<>.05;

studyCentreRecords contains about 1500 records
PSA contains about 9000 records

Again, each selection on its own is fine.

Thank you,

Adam Louis

By default, a union will try to eliminate duplicate entries from the
list, if you know there will be no duplicates then use UNION ALL.
--
But why is the Rum gone?
Nov 12 '05 #2
> > The query in question:


SELECT studyCentreRecords.mrn, PSA.patient, PSA.id, PSA.date,
PSA.totalPSA, PSA.detectionLimit
FROM PSA INNER JOIN studyCentreRecords ON
PSA.patient=studyCentreRecords.patient
WHERE PSA.detectionLimit<>'<' AND PSA.totalPSA IN (SELECT PSA.totalPSA
FROM PSA WHERE PSA.detectionLimit='<' AND PSA.totalPSA<>.1)

UNION SELECT studyCentreRecords.mrn, PSA.patient, PSA.id, PSA.date,
PSA.totalPSA, PSA.detectionLimit
FROM PSA INNER JOIN studyCentreRecords ON
PSA.patient=studyCentreRecords.patient
WHERE PSA.detectionLimit='<' AND PSA.totalPSA<>.1 AND
PSA.totalPSA<>.05;


Either I'm not reading carefully enough, or you're making this way
more complicated than it should be. Why not just OR together your
criteria?

SELECT studyCentreRecords.mrn, PSA.patient, PSA.id, PSA.date,
PSA.totalPSA, PSA.detectionLimit
FROM PSA INNER JOIN studyCentreRecords ON
PSA.patient=studyCentreRecords.patient
WHERE (PSA.detectionLimit<>'<' AND PSA.totalPSA IN (SELECT
PSA.totalPSA
FROM PSA WHERE PSA.detectionLimit='<' AND PSA.totalPSA<>.1))
OR (PSA.detectionLimit='<' AND PSA.totalPSA<>.1 AND
PSA.totalPSA<>.05);
Nov 12 '05 #3

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

Similar topics

2
by: News | last post by:
Folks, I need help with this task. I have a set of data that needs to be plotted on timeline chart. Example: Unit ProcStart ProcEnd Machine U1 5/5/03 6:01 5/5/03 6:04 M1 U2 ...
2
by: Jim | last post by:
Im getting way too many rows retured..what its trying to do is insert a 0 for revenue for months 7 - 12 (aka July through December) for each of these cost centers for each payor type..Im getting a...
3
by: dumbledad | last post by:
Hi All, I'm confused by how to replace a SELECT statement in a SQL statement with a specific value. The table I'm working on is a list of words (a column called "word") with an index int...
6
by: Jamal | last post by:
I am working on binary files of struct ACTIONS I have a recursive qsort/mergesort hybrid that 1) i'm not a 100% sure works correctly 2) would like to convert to iteration Any comments or...
73
by: Sean Dolan | last post by:
typedef struct ntt { int type; union { int i; char* s; }; }nt; nt n; n.i = 0;
0
by: chikas | last post by:
hallo all,I need help and any tips shall be appreciated! i want to implement a I2C API(from Beck SC12 microcontroller) in a 1 wire search C-code(use to search devices on 1 wire bus) sothat that i...
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...
10
by: MLH | last post by:
Gentlemen: I am having one heck of a time taking a DAO walk through the records in an SQL dynaset. I'm trying to walk a set of records returned by a UNION query. I'm attempting to filter the...
7
by: Peter Olcott | last post by:
Why can a union have a member with a copy constructor?
5
by: sonalshastry | last post by:
Hi i want to write a cursor where in branch name should be passed into the cursor one by one, I have a bill table with following detail Client, bill_date,bill_amount, branch ... I need to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.