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

SQL Performance Tuning

hi!!!

the following is the sql which is veri slow cos of the 'Not In' clause,
would appreciate if u anyone can suggest any other way to bring about
the same result

SELECT Id, LOC, AGENCY, BATCH
FROM tblRows
WHERE tblRows.AGENCY NOT IN (SELECT DISTINCT B.AGENCY
FROM tblRows AS A, tblRows AS B
WHERE A.LOC = "B"
AND B.AGENCY = A.AGENCY
AND B.BATCH = A.BATCH)

tblRows
ID LOC AGENCY BATCH
1 B 1000 WAD
2 R 1000 WAD
3 B 1010 QAD
4 B 1020 WAD
5 R 1020 WAD
6 R 1030 RRR
7 I 1030 RRR
8 V 1030 RRR
9 B 1040 UIA
10 R 1040 UIA
11 I 1040 UIA
12 V 1040 UIA

the subquery is to return the rows with LOC = B. the above query as the
whole should return the rows where LOC <> b and also must exclude rows
belong to the LOC = B subset (that is for example the first two rows
with ID 1 and 2. the first row with ID 1 has LOC as B, Agency as 1000
and BATCH as WAD. the second row with Id 2 has LOC as R as the same
AGENCY and BATCH as first row with ID 1 so is the subset of first row.
similarly row with Id's 4 and 5.

the above query must return the following the rows (that is we can say
the orphan rows which doesn't have LOC AS B nor belongs to the B's
subset

6 R 1030 RRR
7 I 1030 RRR
8 V 1030 RRR

hope i am clear in my explanation and would appreciate if someone can
point me in the right direction. the reason for posting this in ms
access group is because this is going to be a query in MS Access.

regards
bala

Nov 13 '05 #1
4 3013
Use the Query Builder, join on AGENCY and set the criteria for Agency in
tblRows to Is Null. Indexing AGENCY in both Tables is likely to increase the
speed, as well. If you need the SQL to use in code, go to SQL view in the
Query Builder and copy it.

Larry Linson
Microsoft Access MVP

"bala" <ba*****@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
hi!!!

the following is the sql which is veri slow cos of the 'Not In' clause,
would appreciate if u anyone can suggest any other way to bring about
the same result

SELECT Id, LOC, AGENCY, BATCH
FROM tblRows
WHERE tblRows.AGENCY NOT IN (SELECT DISTINCT B.AGENCY
FROM tblRows AS A, tblRows AS B
WHERE A.LOC = "B"
AND B.AGENCY = A.AGENCY
AND B.BATCH = A.BATCH)

tblRows
ID LOC AGENCY BATCH
1 B 1000 WAD
2 R 1000 WAD
3 B 1010 QAD
4 B 1020 WAD
5 R 1020 WAD
6 R 1030 RRR
7 I 1030 RRR
8 V 1030 RRR
9 B 1040 UIA
10 R 1040 UIA
11 I 1040 UIA
12 V 1040 UIA

the subquery is to return the rows with LOC = B. the above query as the
whole should return the rows where LOC <> b and also must exclude rows
belong to the LOC = B subset (that is for example the first two rows
with ID 1 and 2. the first row with ID 1 has LOC as B, Agency as 1000
and BATCH as WAD. the second row with Id 2 has LOC as R as the same
AGENCY and BATCH as first row with ID 1 so is the subset of first row.
similarly row with Id's 4 and 5.

the above query must return the following the rows (that is we can say
the orphan rows which doesn't have LOC AS B nor belongs to the B's
subset

6 R 1030 RRR
7 I 1030 RRR
8 V 1030 RRR

hope i am clear in my explanation and would appreciate if someone can
point me in the right direction. the reason for posting this in ms
access group is because this is going to be a query in MS Access.

regards
bala

Nov 13 '05 #2

"bala" <ba*****@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
hi!!!

the following is the sql which is veri slow cos of the 'Not In' clause,
would appreciate if u anyone can suggest any other way to bring about
the same result

SELECT Id, LOC, AGENCY, BATCH
FROM tblRows
WHERE tblRows.AGENCY NOT IN (SELECT DISTINCT B.AGENCY
FROM tblRows AS A, tblRows AS B
WHERE A.LOC = "B"
AND B.AGENCY = A.AGENCY
AND B.BATCH = A.BATCH)

tblRows
ID LOC AGENCY BATCH
1 B 1000 WAD
2 R 1000 WAD
3 B 1010 QAD
4 B 1020 WAD
5 R 1020 WAD
6 R 1030 RRR
7 I 1030 RRR
8 V 1030 RRR
9 B 1040 UIA
10 R 1040 UIA
11 I 1040 UIA
12 V 1040 UIA

the subquery is to return the rows with LOC = B. the above query as the
whole should return the rows where LOC <> b and also must exclude rows
belong to the LOC = B subset (that is for example the first two rows
with ID 1 and 2. the first row with ID 1 has LOC as B, Agency as 1000
and BATCH as WAD. the second row with Id 2 has LOC as R as the same
AGENCY and BATCH as first row with ID 1 so is the subset of first row.
similarly row with Id's 4 and 5.

the above query must return the following the rows (that is we can say
the orphan rows which doesn't have LOC AS B nor belongs to the B's
subset

6 R 1030 RRR
7 I 1030 RRR
8 V 1030 RRR

hope i am clear in my explanation and would appreciate if someone can
point me in the right direction. the reason for posting this in ms
access group is because this is going to be a query in MS Access.

regards
bala

When you explain your query, you mention rows 1 and 2 having the same AGENCY
and BATCH. When you run your query, you do not seem to care about BATCH
being the same. In other words if the BATCH column for row 2 was "BANANA",
row 2 would still be excluded because its AGENCY column is 1000 (and
AGENCY_1000 appears elsewhere in the table with a LOC of "B").
If this is OK and the query returns the right results, then why is the
subquery so complicated? Surely the following shows the rows we want to
exclude:
SELECT DISTINCT B.AGENCY FROM tblRows AS B WHERE B.LOC = "B"

and if this is being used as a sub-query, you do not need to worry about the
DISTINCT, so the whole query would be:

SELECT Id, LOC, AGENCY, BATCH
FROM tblRows
WHERE tblRows.AGENCY NOT IN
(SELECT B.AGENCY FROM tblRows AS B WHERE B.LOC = "B")

Generally speaking, these sort of subqueries should not be slow. So if
performance is still poor, look at the indexing of the table before you
throw out the sub-query idea. You could let us know how many rows the table
holds, how many rows the query returns and how long it took to run. This
will give us an idea of whether you could expect better results.
PS you also do not mention the location or type of back-end tables.
Nov 13 '05 #3
hi justin

thanx for the response, it is appreciated.

well just to answer your question i want the combination of BATCH and
AGENCY to be unique but since just checking AGENCY would solved the
purpose i am using BATCH alone.

also regarding indexing - i already have ID column and AGENCY column
are both indexed.

it is taking more than 30 mins and most of the time the application
hangs when i use the 'NOT IN' keyword or even '<>' symbol, whereas if
remove the NOT IN or '<>' and just run it (just for checking the speed)
the resultset comes in seconds eventhough it retrieves 16,000 records.
in the actual resultset i want it the number of records wont be more
than 500.

hope this information helps.

since the number of rows are less and the query needn't be optimized
much i used subquery as it would solve the purpose but 'NOT IN' is
giving me weird problems. :)

bala

Nov 13 '05 #4
hi larry

thanx for the response, it is appreciated.

well i always build my SQL statements manually.

let me give the query builder a shot and see whether i can the
statement right. will let u know if i am successful or not

regards
bala

Nov 13 '05 #5

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

Similar topics

5
by: nmac | last post by:
Hi all, hopefully someone can offer some sagely advice regarding Production use of Jakarta's Tomcat. First, some brief background. My company have a servlet application that connects to a MySQL...
0
by: Tober | last post by:
Anyone happen to know of any good books on performance tuning for MS-SQL 2000? I am currently building a Cognos datawarehouse and would like to ensure that the db is tuned for peak performance. ...
9
by: pheonix1t | last post by:
hello, I've been assigned to do performance tuning on an SQL2000 database (around 10GB in size, several instances). So far, I see a single RAID5 array, 4CPU (xeon 700MHZ), 4GB RAM. I see the...
1
by: Fusheng Wang | last post by:
Hi, I have an insert intensive database, and it seems that the performance is a problem. Any suggestions on performance tuning on insert performance? Thanks a lot! Frank
35
by: sacha.prins | last post by:
Hi, I read a lot about DB2 INSERT performance here. I have a nice story as well. The thing is, I work on 2 installations of DB2 (on completely different locations) which run on different (but...
2
by: Jeff S | last post by:
I'm looking for guidance (tutorials, backgrounders, tips, or otherwise) on measuring the performance of ASP.NET applications. I'm specifically interested in acquiring the capability of generating...
6
by: Marc Hoeijmans | last post by:
Can any one tell me if the performance in the 2.0 framework is improved? An 1.1 ASP.NET application performce bad once a moth we are thinking of upgrading 2.0, should this help ore is it more...
3
by: hpw | last post by:
Hi all, i'm looking for a good book about .net Performance Tuning. Things that should be covered by this book: - Glean information about your program's behavior from profiling tools -...
13
by: atlaste | last post by:
Hi, I'm currently developing an application that uses a lot of computational power, disk access and memory caching (to be more exact: an information retrieval platform). In these kind of...
4
by: 73k5blazer | last post by:
Hello again all.. We have a giant application from a giant software vendor that has very poor SQL. It's a PLM CAD application, that makes a call to the db for every cad node in the assembly. So...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
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...
0
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...

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.