+ Reply to Thread
Results 1 to 6 of 6

Thread: Problem with ORs in JOIN statement

  1. #1
    Barn Newbie ngreen is an unknown quantity at this point ngreen's Avatar
    Join Date
    Aug 2009
    Posts
    3
    Rep Power
    3

    Problem with ORs in JOIN statement

    Hi,

    I have a problem with the SQL statement detailed below. The query returns the results I need, but takes an insane amount of time to execute. I now have so many records in the db that the page generally won't load.

    SELECT dscan.guid, dscan.drive, dscan.folder, dscan.filename, source.guid
    FROM source RIGHT JOIN dscan
    ON ((source.guid & '_dtr' = dscan.guid OR source.guid & '_dto' = dscan.guid OR source.guid = dscan.guid)
    AND dscan.guid LIKE '%" & Replace(strSearch_guid, "'", "''") & "%'
    AND dscan.filename NOT LIKE '.[_]%'
    AND dscan.drive = 'Z:')
    WHERE source.guid Is Null
    ORDER BY dscan.drive, dscan.guid

    The problem is in this part of the JOIN:

    (source.guid & '_dtr' = dscan.guid OR source.guid & '_dto' = dscan.guid OR source.guid = dscan.guid)

    If I only have one of those conditions, the page loads instantly, but with all 3 it bombs.

    Anyone know why this is happening and how I can fix it?

    ________________

    FYI for anyone wondering what I'm doing, I'm comparing a list of filenames (dscan.guid) against database records (source.guid) to identify misnamed or misplaced files.

    The files sometimes have exactly the same name as the database record or sometimes have meaningful appendages (_dtr or _dto).

    Because they are meaningful, those appendages are used in other database functions so I can't just get rid of them. I could create another column in the dscan table and separate those appendages from the rest of the filename which would then allow me to just use:

    source.guid = dscan.guid

    in the query. But I'm curious as to why it's slow to execute as is, and prefer not to have to add a whole column just to get this one query to work.

  2. #2
    Super Sarcasm Mistress mehere is a glorious beacon of light mehere is a glorious beacon of light mehere is a glorious beacon of light mehere is a glorious beacon of light mehere is a glorious beacon of light mehere's Avatar
    Join Date
    Mar 2008
    Location
    Wide Awake In Dreamland
    Posts
    830
    Rep Power
    8

    is there an index on the guid column in both tables? it's always good practice to have indexes on any columns used in JOINS or WHERE clauses.
    Quote of the Month:
    INSIGHT: When the going gets tough, the tough get going. The smart left a long time ago.

    Questions to Ponder:
    Are people more violently opposed to fur rather than leather because it's much easier to harass rich women than motorcycle gangs?

    iif([sarcasm]=true,iif([you have to ask]=true,"didn't work","ha ha ha"),"not sarcasm")
    copyright © 2008 sbenj69

    Sarchasm: The gulf between the author of sarcastic wit and the person who doesn't get it.

  3. #3
    Barn Newbie ngreen is an unknown quantity at this point ngreen's Avatar
    Join Date
    Aug 2009
    Posts
    3
    Rep Power
    3

    I don't have an index on the dscan table - it's subject to frequent updates.

    But I added one just to see if it made a difference and it hasn't helped with this query. The page still won't load.

  4. #4
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    Is this something that has to be run in real-time or is this something that you only do on occasion? At some point, the complexity of the query and size of the data just makes it too much to run interactively (for example, I have a query that takes about 2.5 hours to run, but I only do it once a month).

    Consider breaking out the work and make this a stored procedure that you can run when necessary to create a reporting table.
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

  5. #5
    Barn Enthusiast Flam is an unknown quantity at this point Flam's Avatar
    Join Date
    Dec 2008
    Posts
    249
    Rep Power
    4

    Hey ngreen,

    I'm definitely the novice here so you've probably thought of this..but if you're trying to compare to see misnamed records, there's a couple ways I go about it that are a bit simpler:


    SELECT ID, Column
    FROM TableA
    Where Column not in(Select Column from TableB)

    Probably too simple for what you're trying to accomplish.

    Another way I get this is with an intersect or an except query

    SELECT ID, Column
    From TableA

    INTERSECT (or Except depending on if you want to view the rows that match
    vs those that don't match)

    SELECT ID, Column
    From TableB

    Again, I"m sure this is way too simple for what you're doing but these help me tremendously with all the bad data entry that I have to catch on a regular basis

  6. #6
    Barn Newbie ngreen is an unknown quantity at this point ngreen's Avatar
    Join Date
    Aug 2009
    Posts
    3
    Rep Power
    3

    Quote Originally Posted by Wolffy View Post
    Is this something that has to be run in real-time or is this something that you only do on occasion? At some point, the complexity of the query and size of the data just makes it too much to run interactively (for example, I have a query that takes about 2.5 hours to run, but I only do it once a month).

    Consider breaking out the work and make this a stored procedure that you can run when necessary to create a reporting table.
    That's an option I hadn't considered. I avoid additional tables like this wherever possible. If it comes to this, I think I'd rather add the extra column to the dscan table to allow me to run a straight source.guid=dscan.guid comparison.

    Quote Originally Posted by Flam View Post
    Hey ngreen,

    I'm definitely the novice here so you've probably thought of this..but if you're trying to compare to see misnamed records, there's a couple ways I go about it that are a bit simpler:


    SELECT ID, Column
    FROM TableA
    Where Column not in(Select Column from TableB)

    Probably too simple for what you're trying to accomplish.
    That method works, but is even worse when it comes to execution time. I tried that method briefly with some of the simpler comparisons I run and found it to be very slow.

    Quote Originally Posted by Flam View Post
    Another way I get this is with an intersect or an except query

    SELECT ID, Column
    From TableA

    INTERSECT (or Except depending on if you want to view the rows that match
    vs those that don't match)

    SELECT ID, Column
    From TableB

    Again, I"m sure this is way too simple for what you're doing but these help me tremendously with all the bad data entry that I have to catch on a regular basis
    I'd never heard of this one (I'm a complete novice too) so I'll give it a try next week and report back on how it goes. But I'm not sure if it can handle what I need, eg. it would need to be:

    SELECT guid
    From dscan

    EXCEPT

    SELECT (guid OR guid & '_dto' OR guid & '_dtr')
    From TableB

+ Reply to Thread

Similar Threads

  1. SQL JOIN - Which to use?
    By tommysunny in forum SQL Development
    Replies: 13
    Last Post: July 1st, 2009, 06:47 PM
  2. sql statement with an iif statement inside
    By arloguth in forum Microsoft Access
    Replies: 7
    Last Post: June 7th, 2009, 11:31 PM
  3. Conditional JOIN in Stored Procedure
    By richyrich in forum MySQL
    Replies: 5
    Last Post: April 6th, 2009, 04:55 PM
  4. SQL Left Outer Join Problem
    By ubundom in forum Database Design Help
    Replies: 1
    Last Post: October 24th, 2008, 10:19 AM
  5. sql inner outer join help
    By peebman2000 in forum SQL Development
    Replies: 6
    Last Post: September 22nd, 2008, 12:24 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO