Which is more efficient? (1 Viewer)

Local time
Yesterday, 20:41
Joined
Feb 25, 2008
Messages
410
In the picture attached, there are two fields we're working with;
Address1 and DepartmentID

The picture shows two different queries that return the same results.

If you wanted to run this query where;
Address1 = "123 Fake St." and
DepartmentID = 28 or 29

Which method would be more efficicent, and why?

I think it would be the bottom method in the picture because it creates a simpler-looking SQL statement, but I have no theoretical basis to that hypothesis.

There was a real address that I had to black-out so just pretend it says "123 Fake St."
 

Attachments

  • untitled.JPG
    untitled.JPG
    26.6 KB · Views: 104

Banana

split with a cherry atop.
Local time
Yesterday, 20:41
Joined
Sep 1, 2005
Messages
6,318
I'll bet that if you switch to SQL view in the query builder, you'll find that both statements are actually identical. The only thing that's different is how it's visually presented in the builder but the visual layout has no relation to how the SQL is actually constructed (and why should it? Moving the tables may be helpful in making a query more clearer but it won't change the underlying SQL statement at all).
 

boblarson

Smeghead
Local time
Yesterday, 20:41
Joined
Jan 12, 2001
Messages
32,059
The bottom method is more efficient because you only have to put the criteria for the address line in once :D
 

Banana

split with a cherry atop.
Local time
Yesterday, 20:41
Joined
Sep 1, 2005
Messages
6,318
Well, I would argue for top method being more efficient on the account of number of keystrokes:

Four keystrokes for the criteria and a down arrow for the top compared to four keystroke for the criteria plus four more for " OR ", so top method saves us 3 keystrokes. :p
 

boblarson

Smeghead
Local time
Yesterday, 20:41
Joined
Jan 12, 2001
Messages
32,059
How about the number of keystrokes as you have to move from column to column if you have more than two criteria on the right side? The more criteria, it would get less efficient, especially with a large number of OR's (for which you could switch to IN).

So, a bunch of silliness anyway, eh? For as you stated, it makes no difference really what you do (although I've seen Access reformat it to the lower version from the top after a save).
 

Banana

split with a cherry atop.
Local time
Yesterday, 20:41
Joined
Sep 1, 2005
Messages
6,318
I happened had a second to quickly test, and I wasn't quite right. It's very similar but not identical.

Top version
Code:
SELECT 1 AS Expr1
FROM dual
WHERE (((1)=27 Or (1)=28));

Bottom version
Code:
SELECT 1 AS Expr1
FROM dual
WHERE ((1)=27 OR (1)=28);

The difference being a extra (), which is probably just one extra cycle. Big whooopee considering that we're using several gigahertz chips nowadays. But Bob is right; it get reformatted anyway.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 04:41
Joined
Sep 12, 2006
Messages
15,660
what about using IN

SELECT 1 AS Expr1
FROM dual
WHERE 1 in (27,28);

is this processed in the same way as far as the compiler goes, or is this optimised more efficiently?
 

Banana

split with a cherry atop.
Local time
Yesterday, 20:41
Joined
Sep 1, 2005
Messages
6,318
Good question.

I do know for a fact that IN() in the WHERE is actually a compile-time shortcut for OR in case of SQL Server & MySQL. This is also why you can't do this:

Code:
PARAMETERS foo TEXT;
SELECT 1 AS Expr1
FROM dual
WHERE 1 IN (foo);

(or in SQL Server/MySQL's parlance)
Code:
SELECT 1
FROM dual
WHERE 1 IN (@p);

There's no way to basically say "here, parse this string of delimited values for the IN at runtime" short of using dynamic SQL, though the Erland (a SQL Server MVP) gave a cool solution of transforming a space-delimited arrays of integers into a temporary table and doing a join to do just that; I've actually used that myself).

I know that Jet also does not accept the above query with a parameter so it must be passed as a dynamic SQL (e.g. use VBA to build the query just in time) so though I don't have an explicit text saying so, I'm inclined to say Jet is same as SQL Server & MySQL in that it treats X IN(A,B,C) as same thing as X = A OR X = B OR X = C, so it exists mostly for our convenience rather than for optimizing the query.
 

Users who are viewing this thread

Top Bottom