Avoiding the use of DISTINCT

Discussion in 'Databases' started by NoamBarz, May 7, 2007.

  1. #1
    I was wondering whether there was a way to avoid using the DISTINCT keyword, since it seems to be causing really bad performance issues. Is there a general solution to problems such as these?
     
    NoamBarz, May 7, 2007 IP
  2. SoKickIt

    SoKickIt Active Member

    Messages:
    305
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    70
    #2
    No, but you can explain your situation (post queries, table structure) and I'm sure someone will help you.
     
    SoKickIt, May 7, 2007 IP
  3. Carlito

    Carlito Peon

    Messages:
    679
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    0
    #3
    If you're using MSSQL, I haven't really seen any performance issues from using DISTINCT even in high volume environments. Maybe avoid using DISTINCT on a long text column, select the distinct IDs and join on that.
     
    Carlito, May 7, 2007 IP
  4. alamster

    alamster Peon

    Messages:
    41
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    you can use group by as distinct alternative with faster execution time and more condition statement.
     
    alamster, May 13, 2007 IP
  5. teachai

    teachai Peon

    Messages:
    9
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Look at your query. Are you missing a join.
    In general with a properly designed database and a properly written query you will have very few occasions where you need to use distinct.

    I suspect strongly that you have a missing join condition. If you fix the missing join condition you probably won't need a distinct.

    Also a distinct means that you are doing a sort operation which if you have a lot of rows can be very memory intensive.
     
    teachai, May 23, 2007 IP
  6. rennick

    rennick Peon

    Messages:
    85
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Depending on the database you're using, it might also indicate that you need to add an index.
     
    rennick, May 23, 2007 IP
  7. bochgoch

    bochgoch Peon

    Messages:
    1,918
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Store unique data in the queried table. That may involve creating consolidated tables with data at the level of granularity you require.

    Post more detail and I'm sure we can help further.
     
    bochgoch, May 23, 2007 IP
  8. teachai

    teachai Peon

    Messages:
    9
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    :confused: :eek:

    Fix the query. All the performance problems will disappear.
    Why would you use a totally unnecessary group by. If group by was the same as distinct, then there wouldn't be two separate commands.

    Group by is for aggregated data.

    Distinct is for removing duplicates but is not a replacement for a properly joined query. Use only when absolutely necessary
     
    teachai, May 24, 2007 IP