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

Speed cost for using "or" clause and functions on join statement

Select member
from NameList
Inner join Members
on (Left(Namelist.NameID,5) = Members.ID
OR (left(namelist.SSN,9) = Members.ssn
OR (Left(namelist.CustID,9) + '*01' = Members.CustID)
where
namelist.name <> ''

How do I speed up a process like this? Can I create indexes on the
members table based on a function
Like an index based on the left(members.id,5)

or should these statements go into the where clause?

Jul 23 '05 #1
2 2117
Why are you using an OR clause within a JOIN clause? This is very
unorthodox. Simply continue joining namelist to members like this, where
"nl" is alias for namelist and "m" is alias for members:

join m on nl.ssn = m.id
join m on nl.custid = m.custid

OR clauses are notorious for slowing down a query - they only belong in the
WHERE clause, and even then you shoudl try to avoid them if possible

"Phillip" <pp*******@ECommunity.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Select member
from NameList
Inner join Members
on (Left(Namelist.NameID,5) = Members.ID
OR (left(namelist.SSN,9) = Members.ssn
OR (Left(namelist.CustID,9) + '*01' = Members.CustID)
where
namelist.name <> ''

How do I speed up a process like this? Can I create indexes on the
members table based on a function
Like an index based on the left(members.id,5)

or should these statements go into the where clause?

Jul 23 '05 #2
Phillip (pp*******@ECommunity.com) writes:
Select member
from NameList
Inner join Members
on (Left(Namelist.NameID,5) = Members.ID
OR (left(namelist.SSN,9) = Members.ssn
OR (Left(namelist.CustID,9) + '*01' = Members.CustID)
where
namelist.name <> ''

How do I speed up a process like this? Can I create indexes on the
members table based on a function
Like an index based on the left(members.id,5)


There are at least two problems with your query in terms of performance.
1) Say that you have an index on Namelist.NameID. That index is not very
useful here, because NameID appears in an expression. There for SQL Server
cannot seek the index. That is, look up a value using the index tree. At
best SQL Server can scan the index.
2) As Brian pointed the OR clauses can be problematic.

However, if you have non-clustered indexes (member, NameID, name),
(name, SSN, member) and (member, CustID, name), SQL Server can scan
the three indexes and do index intersection.

And, yes could create indexes on a computed column of which the
vale is leff(NameID, 5).

Then again, if NameList is the small table, and Members the big one,
this matters little anyway.

Finally, my guess is that you query is best expressed as:

SELECT nl.member
FROM NameList nl
WHERE EXISTS (SELECT *
FROM Members m
WHERE Left(Namelist.NameID,5) = Members.ID
OR left(namelist.SSN,9) = Members.ssn
Left(namelist.CustID,9) + '*01' = Members.CustID)
AND nl.name <> ''
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #3

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

Similar topics

3
by: Sara Khalatbari | last post by:
Hi! Suppose you're writing a module & writing the definition of each function in that module in " or """. for example: a) "This function does this & that" or: b) """This function does blah...
5
by: Fred | last post by:
Hi all, I have done a lot of experimentations using several "or" predicates within an sql select statements on mysql. My only conclusion so far is that whatever syntax or order I use it is...
3
by: Colleyville Alan | last post by:
I am constructing a SQL command from a function. Some code builds the WHERE clause in a looping structure and passes that as an argument to the SQL-building function. But the results do not...
3
by: Andy_Khosravi | last post by:
I have been trying to build a user friendly search engine for a small database I have created. I'm having some particular problems with one of my date fields. Here's the setup: I'm using...
2
by: Julio Allegue | last post by:
I am getting the wrong Count(*) on vb.net using the ExecuteScalar . It returns all the rows. It doesn't seem to look at the WHERE clause. At the same time, I am getting the correct count on "SQL...
2
by: juan.gautier | last post by:
Hi, I try to construct a SQL code for a view to select a specific data from a table, this query take the value of the filter from a text box in a visual basic 6.0 form. my problem is when i...
2
by: santoshsri | last post by:
Hello, Is there any by which I can write a SQL to get the 4th largest salary from a table without using "join" and without using "where" clause. Suppose the table is having two columns :...
14
by: kpfunf | last post by:
I have a select query that uses an "OR" join. With that join in place, I cannot edit the data in the query datasheet; if I delete the "OR" and only use one join, then I can edit the data. I read...
6
by: adigga1 | last post by:
Hello, I am attempting to call a particular table if the OR statement is used. When I try it gives me a Syntax error. Please assist? Here is the code: SELECT T_Physicians.TaxID,...
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
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.