Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. SQL IN statement or exists

SQL IN statement or exists

Scheduled Pinned Locked Moved Database
databaseperformancehelp
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    programmervb netc
    wrote on last edited by
    #1

    Could I write this as a exists statement we use this quite often and I read it has very poor performance.

    WHERE `cl_client_common`.`UniqueNumber-900` IN(5337, 4439, 4725, 4096, 8078, 5912, etc, etc)

    We do not know what the values will be until the user selects the clients they want. All of the examples I see are using a subquery for the EXISTS statement and the only way I can think to write that with a subquery would be to use an IN in the subquery. Thank you any help is appreciated.

    Humble Programmer

    _ M P 3 Replies Last reply
    0
    • P programmervb netc

      Could I write this as a exists statement we use this quite often and I read it has very poor performance.

      WHERE `cl_client_common`.`UniqueNumber-900` IN(5337, 4439, 4725, 4096, 8078, 5912, etc, etc)

      We do not know what the values will be until the user selects the clients they want. All of the examples I see are using a subquery for the EXISTS statement and the only way I can think to write that with a subquery would be to use an IN in the subquery. Thank you any help is appreciated.

      Humble Programmer

      _ Offline
      _ Offline
      _Damian S_
      wrote on last edited by
      #2

      If you are storing the list of numbers in a temp table or similar, you could use

      WHERE `cl_client_common`.`UniqueNumber-900` IN (select NUMBER from TEMPTABLE)

      or simply by doing an inner join to the temp table... Otherwise, the way you are doing it will have to do!!

      I don't have ADHD, I have ADOS... Attention Deficit oooh SHINY!! If you like cars, check out the Booger Mobile blog | If you feel generous - make a donation to Camp Quality!!

      1 Reply Last reply
      0
      • P programmervb netc

        Could I write this as a exists statement we use this quite often and I read it has very poor performance.

        WHERE `cl_client_common`.`UniqueNumber-900` IN(5337, 4439, 4725, 4096, 8078, 5912, etc, etc)

        We do not know what the values will be until the user selects the clients they want. All of the examples I see are using a subquery for the EXISTS statement and the only way I can think to write that with a subquery would be to use an IN in the subquery. Thank you any help is appreciated.

        Humble Programmer

        M Offline
        M Offline
        Mycroft Holmes
        wrote on last edited by
        #3

        Learn to use the sql profiler, it will take you query and give you an execution plan, it may even suggest what fields need indexes to optomise your query, it will certinaly give you the cost of each process. You can then decide on the best approach. As DM said you have lots of options, temp table, table variable, sub query. Use and in() or an inner join or even exists as suggested earlier. I'm not sure if the exists is a better solution, I would be tempted to go with the table variable on a large table os just a sub query as you have done. Depends on the size of the data sets and the indexing.

        Never underestimate the power of human stupidity RAH

        1 Reply Last reply
        0
        • P programmervb netc

          Could I write this as a exists statement we use this quite often and I read it has very poor performance.

          WHERE `cl_client_common`.`UniqueNumber-900` IN(5337, 4439, 4725, 4096, 8078, 5912, etc, etc)

          We do not know what the values will be until the user selects the clients they want. All of the examples I see are using a subquery for the EXISTS statement and the only way I can think to write that with a subquery would be to use an IN in the subquery. Thank you any help is appreciated.

          Humble Programmer

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          I generally prefer to use a join. I caution against IN unless the values are hard-coded (which is likely a bad design in its own right). I especially dislike subqueries in WHERE clauses. I used to have to use them with Oracle, but I don't recall ever doing them with SQL Server.

          P 1 Reply Last reply
          0
          • P PIEBALDconsult

            I generally prefer to use a join. I caution against IN unless the values are hard-coded (which is likely a bad design in its own right). I especially dislike subqueries in WHERE clauses. I used to have to use them with Oracle, but I don't recall ever doing them with SQL Server.

            P Offline
            P Offline
            programmervb netc
            wrote on last edited by
            #5

            We are joining the tables. The values are not really hard coded... The user is presented with a grid with checkboxes they can choose one or as many "clients" as they want in the grid. Then we use the primary key for the record on the grid to create the new sql statement. So user selects primary key 1,2,3,4 then our IN statement will be `UniqueNumber IN (1,2,3,4)

            Humble Programmer

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups