Ask a Question related to ASP Database, Design and Development.

  1. #1

    Default Re: IN() vs. OR

    MDW wrote:
    > Hey all.
    >
    > Assume I have two SQL statements:
    >
    > "SELECT Col1,Col2 FROM MyTable WHERE Col1 = 'Val1' OR Col1 = 'Val2'
    > OR Col1 = 'Val3' OR Col1 = 'Val4'"
    >
    > and
    >
    > "SELECT Col1,Col2 FROM MyTable WHERE Col1
    > IN('Val1','Val2','Val3','Val4')"
    >
    > Is there any difference in the recordet created by those statements?
    > Is one preferable over another? Does than answer change if you've got
    > a lot (say, more than 200) values?
    It depends on the database. Most rdbms's (even Jet) provide the ability to
    view and analyze the query plans generated by the query optimizers. I
    suggest you utilize that functionality to answer your own question.

    Bob Barrows
    --
    Microsoft MVP -- ASP/ASP.NET
    Please reply to the newsgroup. The email account listed in my From
    header is my spam trap, so I don't check it very often. You will get a
    quicker response by posting to the newsgroup.


    Bob Barrows [MVP] Guest

  2. #2

    Default Re: IN() vs. OR

    > Is there any difference in the recordet created by those statements?

    No, they will return the same data.
    > Is one preferable over another?
    Depends on your criteria. They should generate the same query plan, at
    least in SQL Server... an IN list will be translated by the query engine as
    a series of OR clauses. But you can always test it yourself, with 4 values
    or over 200.
    > Does than answer change if you've got a lot (say, more than 200) values?
    Again, depends on your criteria. If you're writing this statement manually,
    obviously you wouldn't want to do either if there are over 200 values.
    Hopefully you have iterative code that will generate the list/statement for
    you. As for performance, see above, and test it yourself. As Bob
    indicates, this is why performance/analysis tools come with the product.

    REMEMBER! It is *ALWAYS* relevant to tell us what database you are using!!!

    --
    Aaron Bertrand
    SQL Server MVP
    [url]http://www.aspfaq.com/[/url]


    Aaron Bertrand - MVP Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139