• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Using 1 = 1 in SQL Statements

New Here ,
Jun 12, 2006 Jun 12, 2006

Copy link to clipboard

Copied

I have just been told by our DBA that we have to go through our code and remove the "WHERE 1 = 1" in our SQL statements on a SQL 2000 database. He said it is a performance issue when nothing else follows it (no AND's).

For example (the wrong way):
SELECT this_ID FROM thistable WHERE 1 = 1

Versus...
SELECT this_ID FROM thistable WHERE 1 = 1 AND this_ID = 1001

He said Microsoft said that this is causing a performance hit when there is no AND clauses after the WHERE.

Some of these SQL statements are pretty big and there is no practical way to do a CFIF beforehand.

Has anyone heard of a performance hit like this? If so, how much of a hit? The main table in the DB has about 2 million records with full text indexes.

Thanks,
Rob in Tampa



TOPICS
Advanced techniques

Views

517

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jun 12, 2006 Jun 12, 2006
tell him to do his g**dam job and tune the database like a real dba should
and quit harping about stuff that affects performance about as much as you
farting close to the server.


"TPA_Dude" <webforumsuser@macromedia.com> wrote in message
news:e6khe4$s0a$1@forums.macromedia.com...
> MikerRoo,
> Thank you also for your speedy response. I have debugging turned on for
> my
> IP, so I will start writing down execution times for individual queries.
> I
> didn't know I could view the plan, so th...

Votes

Translate

Translate
Mentor ,
Jun 12, 2006 Jun 12, 2006

Copy link to clipboard

Copied

If you have nothing but WHERE 1=1 in your query, then you will be returning ALL rows from your table, which could be a big performance hit with a large table. The only reason for the 1=1 is to "protect" your query from throwing a syntax error if all of your <cfif> statements in your <cfquery> are false. You might change this by ensuring that at least one of your parameters exist before even executing the <cfquery> in the first place.

Phil

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Jun 12, 2006 Jun 12, 2006

Copy link to clipboard

Copied

Run your query in query analyzer. Turn on show plan and show trace and see for yourself.

Note that you usually need to average 10 or so runs to get bettter numbers.

Anyway, on one of our SQL 2000 systems there is absolutely no difference between where 1=1 and no where clause.

WHERE 1=1 and where 0=1 are valid techniques and I don't know a DBA who doesn't use them.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Jun 12, 2006 Jun 12, 2006

Copy link to clipboard

Copied

Of course don't grab 2 million rows at a time unless you are really and immediately using all of them.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 12, 2006 Jun 12, 2006

Copy link to clipboard

Copied

Phil,
Thanks for the speedy response. I forgot to mention.. there are TOP 200 clauses in all the big SQL statements.

And, like I said before... some of the queries are too big to test to see if at least 1 of the condidtions exsist. More often than not, the WHERE 1 = 1 clause will not be alone. I personally think it is a pet peeve of the DBA where he doesn't see any use of the 1 = 1. He said.. it's always going to be true. Kinda the point of having it.

This was taught to me years ago.. but, this is the biggest application I have worked on where SQL performance could be an issue. It was mentioned that the hit comes when SQL Server is building its execution plan and has to include the WHERE 1 = 1 clause. How much of a hit can it cause? Most of the empty clauses with no AND's are most likely in a JOIN clause or something.

The WHERE placeholder is in almost all of our queries in one way or another. Not a small project.

Thanks,
Rob

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 12, 2006 Jun 12, 2006

Copy link to clipboard

Copied

MikerRoo,
Thank you also for your speedy response. I have debugging turned on for my IP, so I will start writing down execution times for individual queries. I didn't know I could view the plan, so thanks. I am sure that will help alot.

Thanks again to everyone,
Rob

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 12, 2006 Jun 12, 2006

Copy link to clipboard

Copied

tell him to do his g**dam job and tune the database like a real dba should
and quit harping about stuff that affects performance about as much as you
farting close to the server.


"TPA_Dude" <webforumsuser@macromedia.com> wrote in message
news:e6khe4$s0a$1@forums.macromedia.com...
> MikerRoo,
> Thank you also for your speedy response. I have debugging turned on for
> my
> IP, so I will start writing down execution times for individual queries.
> I
> didn't know I could view the plan, so thanks. I am sure that will help
> alot.
>
> Thanks again to everyone,
> Rob
>


Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Jun 12, 2006 Jun 12, 2006

Copy link to clipboard

Copied

DBAs.... gotta love 'em. Of course, they would prefer to maintain an "ideal" database that nobody uses and which isn't corrupted by real world use... I think that ALL DBAs should be developers first so they know which end of the database is the most important.

Phil

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 13, 2006 Jun 13, 2006

Copy link to clipboard

Copied

Can the "WHERE 1 = 1" clause ever be taken to mean.. WHERE "column 1 value" equal to "the value of 1"?

Thanks

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Jun 13, 2006 Jun 13, 2006

Copy link to clipboard

Copied

LATEST
No, not in SQL server 2000.

SS2K allows columns to be referenced by number in only a few places, notably the order by clause.

Anyway, use query analyzer to show the execution plan and the execution trace. You will see no difference between where 1=1 and no where clause at all.

I must second "Marc E". Tell the DBA to show you some REAL numbers or (and) shut up.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation