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
  1. Home
  2. Database & SysAdmin
  3. Database
  4. which one is faster???

which one is faster???

Scheduled Pinned Locked Moved Database
comperformancequestion
9 Posts 7 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.
  • L Offline
    L Offline
    LukeV
    wrote on last edited by
    #1

    SELECT Company FROM Orders -or- SELECT DISTINCT Company FROM Orders Is there any speed gain when using DISTINCT? Does DISTINCT works like Find First... or it goes through all the records anyways? Thanks guys! :) --------------- Concentrating on Ideas http://www.edovia.com

    L M P V 4 Replies Last reply
    0
    • L LukeV

      SELECT Company FROM Orders -or- SELECT DISTINCT Company FROM Orders Is there any speed gain when using DISTINCT? Does DISTINCT works like Find First... or it goes through all the records anyways? Thanks guys! :) --------------- Concentrating on Ideas http://www.edovia.com

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      SELECT Company FROM Orders will list every record, IOW ABC XYZ ABC EFG GHI XYZ SELECT DISTINCT Company FROM Orders will list only unique matches IOW ABC XYZ EFG GHI Have a look at the SQL Server SELECT statement article, nice ;) "There are no stupid question's, just stupid people."

      L 1 Reply Last reply
      0
      • L leppie

        SELECT Company FROM Orders will list every record, IOW ABC XYZ ABC EFG GHI XYZ SELECT DISTINCT Company FROM Orders will list only unique matches IOW ABC XYZ EFG GHI Have a look at the SQL Server SELECT statement article, nice ;) "There are no stupid question's, just stupid people."

        L Offline
        L Offline
        LukeV
        wrote on last edited by
        #3

        Thanks! I guess the real question is how to prevent the complete parsing of the records. Is there a way to tell SQL to "find the first occurence then stop"? Luc --------------- Concentrating on Ideas http://www.edovia.com

        1 Reply Last reply
        0
        • L LukeV

          SELECT Company FROM Orders -or- SELECT DISTINCT Company FROM Orders Is there any speed gain when using DISTINCT? Does DISTINCT works like Find First... or it goes through all the records anyways? Thanks guys! :) --------------- Concentrating on Ideas http://www.edovia.com

          M Offline
          M Offline
          Morten Abrahamsen
          wrote on last edited by
          #4

          Well, this depends on your data. Speaking from a purely SQL Server perspective the basic SELECT is the fastest, but if the DISTINCT clause eliminates a large amount of records it might be faster when taking the middle tier, network access, aso (basically the rest of your processing...) into consideration. The thing to remember is that a DISTINCT is post processed. So the SQL Server actually makes a complete SELECT first, and then filters the records. Morty

          L 1 Reply Last reply
          0
          • M Morten Abrahamsen

            Well, this depends on your data. Speaking from a purely SQL Server perspective the basic SELECT is the fastest, but if the DISTINCT clause eliminates a large amount of records it might be faster when taking the middle tier, network access, aso (basically the rest of your processing...) into consideration. The thing to remember is that a DISTINCT is post processed. So the SQL Server actually makes a complete SELECT first, and then filters the records. Morty

            L Offline
            L Offline
            LukeV
            wrote on last edited by
            #5

            Here's what I want to do: I have a table that contains words and there are no duplicates. Right now, this is what I do: SELECT * FROM table WHERE word = 'hello'; This will cause the SQL server to parse the entire table to look for records that match the criteria. But since I'm 100% sure that the word 'hello' is only there once in the table, I want the query to stop as soon as it finds the record. What can I use for this kind of job? Thanks for your help! :) --------------- Concentrating on Ideas http://www.edovia.com

            P M 2 Replies Last reply
            0
            • L LukeV

              Here's what I want to do: I have a table that contains words and there are no duplicates. Right now, this is what I do: SELECT * FROM table WHERE word = 'hello'; This will cause the SQL server to parse the entire table to look for records that match the criteria. But since I'm 100% sure that the word 'hello' is only there once in the table, I want the query to stop as soon as it finds the record. What can I use for this kind of job? Thanks for your help! :) --------------- Concentrating on Ideas http://www.edovia.com

              P Offline
              P Offline
              Paul Riley
              wrote on last edited by
              #6

              LukeV wrote: What can I use for this kind of job? I think what you're looking for is "SELECT TOP 1" but I'm not an expert. Give it a go. Paul I think there're pieces of me you've never seen - Tori Amos, Tear in Your Hand

              1 Reply Last reply
              0
              • L LukeV

                Here's what I want to do: I have a table that contains words and there are no duplicates. Right now, this is what I do: SELECT * FROM table WHERE word = 'hello'; This will cause the SQL server to parse the entire table to look for records that match the criteria. But since I'm 100% sure that the word 'hello' is only there once in the table, I want the query to stop as soon as it finds the record. What can I use for this kind of job? Thanks for your help! :) --------------- Concentrating on Ideas http://www.edovia.com

                M Offline
                M Offline
                Matt Gullett
                wrote on last edited by
                #7

                If the 'word' column is indexed as unique, SQL Server will not have to look through the entire table and will only need to search the index (very fast) to find the desired row. If the 'word' column is indexed as non-unique, SQL server will still search the index, but may need to return more than one row. If the 'word' column is not indexed SQL server will do a table-scan (full table search) and return every match. You can use the SELECT TOP 1 bla.bla.bla syntax, but probably what you want is for SQL Server to have a unique index on the 'word' column.

                1 Reply Last reply
                0
                • L LukeV

                  SELECT Company FROM Orders -or- SELECT DISTINCT Company FROM Orders Is there any speed gain when using DISTINCT? Does DISTINCT works like Find First... or it goes through all the records anyways? Thanks guys! :) --------------- Concentrating on Ideas http://www.edovia.com

                  P Offline
                  P Offline
                  Paul Ingles
                  wrote on last edited by
                  #8

                  If you're using SQL Server you can use Query Analyzer to find out how long it took to run the query, and where the greatest cost was incurred. It's an incredibly useful tool. -- Paul "I need the secure packaging of Jockeys. My boys need a house!" - Kramer, in "The Chinese Woman" episode of Seinfeld MS Messenger: paul@oobaloo.co.uk Sonork: 100.22446

                  1 Reply Last reply
                  0
                  • L LukeV

                    SELECT Company FROM Orders -or- SELECT DISTINCT Company FROM Orders Is there any speed gain when using DISTINCT? Does DISTINCT works like Find First... or it goes through all the records anyways? Thanks guys! :) --------------- Concentrating on Ideas http://www.edovia.com

                    V Offline
                    V Offline
                    Vagif Abilov
                    wrote on last edited by
                    #9

                    DISTINCT is in general slow (depends on actual data of course). Whenever you use DISTINCT, SQL server needs to group result set (by sorting them) on DISTINCT columns. In case there's no index on those columns, it will take time. On the other hand, if result set is huge, but it contains a lot of repetitions on DISTINCT columns, eliminating repetitions may have positive effect on speed of transferring filtered results to the client. Vagif Abilov MCP (Visual C++) Oslo, Norway Hex is for sissies. Real men use binary. And the most hardcore types use only zeros - uppercase zeros and lowercase zeros. Tomasz Sowinski

                    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