SA Developer .NET

Welcome to SA Developer .NET Sign in | Join | Help
in Search

Efficient Conditional Statements

Last post 08-27-2008, 18:33 by GilaMonster. 15 replies.
Page 1 of 2 (16 items)   1 2 Next >
Sort Posts: Previous Next
  •  08-27-2008, 10:46 14344

    Efficient Conditional Statements

    Which would be better for performance:

    Angel

    IF @oldVal1 <> @newVal1

    OR @oldVal2 <> @newVal2

    OR @oldVal3 <> @newVal3

    SET @isValueChanged = 1

     

    or

    Beer

    IF @oldVal1 <> @newVal1

    OR @oldVal2 <> @newVal2

    OR @oldVal3 <> @newVal3

    SET @isValueChanged = 1

     


    A Program is only Complete when the Programmer Dies
  •  08-27-2008, 10:55 14345 in reply to 14344

    Re: Efficient Conditional Statements

    I can't see any difference between the two options you posted.

    Gail Shaw - SQL In the Wild
    SQL Server MVP
    --
    Chaos, panic and disorder. My job here is done!
  •  08-27-2008, 10:57 14346 in reply to 14345

    Re: Efficient Conditional Statements

    The one has a beer and the other an angel? ... personally I'd go for the beer Stick out tongue

    Why go against tradition when we can admit defeat, live in decline, be the victim of our own design?
    http://dotnet.org.za/calmyourself
  •  08-27-2008, 11:21 14348 in reply to 14346

    Re: Efficient Conditional Statements

    Lol, I've always had a problem with copying and pasting....after a few beers.

     [ A ]

    IF @oldVal1 <> @newVal1

      OR @oldVal2 <> @newVal2

      OR @oldVal3 <> @newVal3

      SET @isValueChanged = 1

    or

    [ B ]

    IF @oldVal1 <> @newVal1

      SET @isValueChanged = 1

    IF @oldVal2 <> @newVal2

      SET @isValueChanged = 1

    IF @oldVal3 <> @newVal3

      SET @isValueChanged = 1

     


    A Program is only Complete when the Programmer Dies
  •  08-27-2008, 11:50 14353 in reply to 14346

    Re: Efficient Conditional Statements

    CalmYourself:
    The one has a beer and the other an angel? ... personally I'd go for the beer Stick out tongue

    I am relying on my years of very technical experience here and I am going to have to say....

    The one with the beer next to it is the better option!


    SA Developer .Net Online Community Support
    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
  •  08-27-2008, 12:06 14355 in reply to 14353

    Re: Efficient Conditional Statements

    fridgi:

    CalmYourself:
    The one has a beer and the other an angel? ... personally I'd go for the beer Stick out tongue

    I am relying on my years of very technical experience here and I am going to have to say....

    The one with the beer next to it is the better option!

    To hell with technical experience, beer always!!!! Stick out tongue


    The Question is the Answer, and the Answer is the Question!
  •  08-27-2008, 12:08 14356 in reply to 14348

    Re: Efficient Conditional Statements

    Anyway,

    Back to your problem, I'd personally go with option (A). I personally feel you should have as few COF statements as possible.

    Unless of course, you have many values to check.. for this example I'd go with (A).


    The Question is the Answer, and the Answer is the Question!
  •  08-27-2008, 12:17 14357 in reply to 14356

    Re: Efficient Conditional Statements

    Having just run this scenario through SQL profiler, it seems Profiler registers no difference in whether or not you use (A) or (B). It only registers 14 reads in total.
    The Question is the Answer, and the Answer is the Question!
  •  08-27-2008, 12:20 14358 in reply to 14355

    Re: Efficient Conditional Statements

    horatio:
    fridgi:

    CalmYourself:
    The one has a beer and the other an angel? ... personally I'd go for the beer Stick out tongue

    I am relying on my years of very technical experience here and I am going to have to say....

    The one with the beer next to it is the better option!

    To hell with technical experience, beer always!!!! Stick out tongue

     * Kitten perks up interestedly.

    Beer?  When?  Where?  Oh, mistype...

     * Kitten loses interest.

    I'd have to say A would be the better option there too...


    Someone once told me love makes the world go around and, well, I just had to laugh in their face because EVERYBODY knows what REALLY makes the world go around is a mutant gerbil on a treadmill.
  •  08-27-2008, 12:23 14359 in reply to 14357

    Re: Efficient Conditional Statements

    I'm swayed towards option A cause the value of the variable could potentially be set to itself multiple times in option B.

    but, option A can  become untidy over time as more checks are added to the same IF statement.


    A Program is only Complete when the Programmer Dies
  •  08-27-2008, 12:31 14360 in reply to 14359

    Re: Efficient Conditional Statements

    NeverDie:

    I'm swayed towards option A cause the value of the variable could potentially be set to itself multiple times in option B.

    but, option A can  become untidy over time as more checks are added to the same IF statement.

    True,

    If you foresee the statement could change in the future (requiring a "quick fix"), rather use Option B then. It will be much easier to debug as well if you have a lot of statements.

    As I said before, there is no difference in performance, so (B) could do fine.


    The Question is the Answer, and the Answer is the Question!
  •  08-27-2008, 12:34 14362 in reply to 14360

    Re: Efficient Conditional Statements

    Well, if there are a lot of condition statements then why not create a SQL function to do this? Wouldn't that be better? Hmm

    "I would love to change the world, but they won't give me the source code"
    SMS your TO DO list to your email
  •  08-27-2008, 14:12 14374 in reply to 14348

    Re: Efficient Conditional Statements

    NeverDie:

    Lol, I've always had a problem with copying and pasting....after a few beers.

    At 11:00?  Where do you work?  Can I join you? Cool


    Unscrambling Eggs: Decompiling ASP.NET
  •  08-27-2008, 15:04 14383 in reply to 14344

    Re: Efficient Conditional Statements

    NeverDie:

    Which would be better for performance:

    Generally in SQL, the times you need to worry about performance is when you are accessing data. Shaving a millisecond or two off a comparison is fairly inconsequential if the query takes significantly longer.


    Gail Shaw - SQL In the Wild
    SQL Server MVP
    --
    Chaos, panic and disorder. My job here is done!
  •  08-27-2008, 16:07 14390 in reply to 14383

    Re: Efficient Conditional Statements

    Thanks Gila.

    I'm finding these days that Processing Power and memory available is not becoming an issue or bottleneck with performance anymore.

    You are quite right, the performance is around actually accessing the data. I've seen up to two thousand percent performance gains by just reworking the SQL to access the data better.


    A Program is only Complete when the Programmer Dies
Page 1 of 2 (16 items)   1 2 Next >
View as RSS news feed in XML
Powered by Community Server (Commercial Edition), by Telligent Systems