473,387 Members | 1,687 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,387 software developers and data experts.

Make slow query faster through joins

Hi,

In the past I seem to have been able to speed up queries by making them a
join rather than ... not a join :o)

An example would be

select * from x where a in (select a from y)

would be something like... select x.* from x inner join y on x.a = y.a

Now I have a more complicated query, which seems to drag for hours, and I
wondered if it could be made faster by the use of joins.

SELECT *
FROM addresses
WHERE [conditionX] = true
and personref not in (select personref from addresses where [conditionY] =
true);

The personref is not unique! So I want addresses where conditionX is true,
and the refno has no other addresses associated that have conditionY true.

Thanks in advance
Chris
Nov 12 '05 #1
3 6271
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Query speed is dependent on various things: indexes on criteria
columns, number of rows in tables, number of columns in tables.

Using a join sometimes can increase the speed of a query, sometimes
NOT. Experimentation can be instructive.

Your query doesn't need joins it just needs a better WHERE clause:

SELECT *
FROM addresses
WHERE conditionX = true
AND conditionY = False
MGFoster:::mgf
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBP6FlR4echKqOuFEgEQK9hACgg2aVjXTFsK0742xmjLlDlh n47hkAn0q9
v6nJVMY5+B8L0K1h1u/ye3LF
=nmmW
-----END PGP SIGNATURE-----
Not Me wrote:
Hi,

In the past I seem to have been able to speed up queries by making them a
join rather than ... not a join :o)

An example would be

select * from x where a in (select a from y)

would be something like... select x.* from x inner join y on x.a = y.a

Now I have a more complicated query, which seems to drag for hours, and I
wondered if it could be made faster by the use of joins.

SELECT *
FROM addresses
WHERE [conditionX] = true
and personref not in (select personref from addresses where [conditionY] =
true);

The personref is not unique! So I want addresses where conditionX is true,
and the refno has no other addresses associated that have conditionY true.

Thanks in advance
Chris


Nov 12 '05 #2
Query performance is an 'experimental' process for the most part. Speed depends on
# of records, indexes, etc.

Perhaps if you create indexes on 'personref and conditionx' as one index and
another on 'personref and conditiony' as another index. Also in the subquery
if you perform a group by - this would require less searching...

"Not Me" <su**@sumwhere.fake> wrote in message news:<bn**********@ucsnew1.ncl.ac.uk>...
Hi,

In the past I seem to have been able to speed up queries by making them a
join rather than ... not a join :o)

An example would be

select * from x where a in (select a from y)

would be something like... select x.* from x inner join y on x.a = y.a

Now I have a more complicated query, which seems to drag for hours, and I
wondered if it could be made faster by the use of joins.

SELECT *
FROM addresses
WHERE [conditionX] = true
and personref not in (select personref from addresses where [conditionY] =
true);

The personref is not unique! So I want addresses where conditionX is true,
and the refno has no other addresses associated that have conditionY true.

Thanks in advance
Chris

Nov 12 '05 #3
"MGFoster" <me@privacy.com> wrote in message
news:0z*****************@newsread4.news.pas.earthl ink.net...
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Your query doesn't need joins it just needs a better WHERE clause:

SELECT *
FROM addresses
WHERE conditionX = true
AND conditionY = False


Thanks, but that won't work as it only compares records individually, I want
X to be true on one record and Y false on all other records with the same
refno.

I think grouping might work, as MeadeR suggested.

Thanks again,
Chris
Nov 12 '05 #4

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

Similar topics

3
by: Robert | last post by:
I am having performance issues on a SQL query in Access. My query is accessing and joining several tables (one very large one). The tables are linked ODBC. The client submits the query to the...
11
by: DJJ | last post by:
I am using the MySQL ODBC 3.51 driver to link three relatively small MySQL tables to a Microsoft Access 2003 database. I am finding that the data from the MySQL tables takes a hell of a long time...
3
by: Not Me | last post by:
Hi, Can't post specifics at the moment but if this seems like a common problem any help would be appreciated. When querying with ~6 tables, using mostly left outer joins, I get standard...
3
by: Doug Baroter | last post by:
Hi, I have a small Access 97 database (~2.5 MB), which has about 9 user tables, and data are normalized up to 3NF. I'm using a third party tool/language for web interface application. I just...
21
by: CSN | last post by:
I have a pretty simple select query that joins a table (p) with 125K rows with another table (pc) with almost one million rows: select p.* from product_categories pc inner join products p on...
24
by: clare at snyder.on.ca | last post by:
I have a SQL query I need to design to select name and email addresses for policies that are due and not renewed in a given time period. The problem is, the database keeps the information for every...
2
by: gkellymail | last post by:
the following query works fine: select link.idx, link.x_table, link.x_id_a, link.x_id_z, a.strandid, b.strandid from link_detail, link, strand A, strand B where link_detail.x_table =...
10
by: Extremest | last post by:
I know there are ways to make this a lot faster. Any newsreader does this in seconds. I don't know how they do it and I am very new to c#. If anyone knows a faster way please let me know. All...
1
by: Don Li | last post by:
Hi, Env: MS SQL Server 2000 DB Info (sorry no DDL nor sample data): tblA has 147249 rows -- clustered index on pk (one key of datatype(int)) and has two clumns, both are being used in joins;...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
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.