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.
ngreen, August 26th, 2009 11:01 PM
Bookmarks