Monday, February 13, 2012

Argument list in EXISTS/NOT EXISTS Selects

I've wondered what is best to use as an argument list in where clauses where I use an EXISTS or NOT EXISTS such as in

SELECT a,b,c
FROM dbo.Customer c (NOLOCK)
WHERE EXISTS (SELECT * FROM Orders o (NOLOCK)
WHERE o.customerID = c.customerID)

Is there any difference between using the wildcard * or a constant 'X' or a single field name?

Quote:

Originally Posted by mbyrdtx

I've wondered what is best to use as an argument list in where clauses where I use an EXISTS or NOT EXISTS such as in

SELECT a,b,c
FROM dbo.Customer c (NOLOCK)
WHERE EXISTS (SELECT * FROM Orders o (NOLOCK)
WHERE o.customerID = c.customerID)

Is there any difference between using the wildcard * or a constant 'X' or a single field name?


From what I understand the following statement
SELECT * FROM Table1
is much slower than
SELECT Field1, Field2 From Table1

The idea being that "select *" forces sql server to perform many more actions to return the result set. You are better off typing out every field you want returned.

I do not know if a constant instead of a field name will give you better performace.|||

Quote:

Originally Posted by mbyrdtx

I've wondered what is best to use as an argument list in where clauses where I use an EXISTS or NOT EXISTS such as in

SELECT a,b,c
FROM dbo.Customer c (NOLOCK)
WHERE EXISTS (SELECT * FROM Orders o (NOLOCK)
WHERE o.customerID = c.customerID)

Is there any difference between using the wildcard * or a constant 'X' or a single field name?


try:

select a,b,c from dbo.Customer C where c.CustomerId in (select o.customerID from Orders)

No comments:

Post a Comment