Distinct is taking a lot of time and want to avoid that

  • Hi , I have a query which is taking a lot of time because of a Distinct column.Ca any one suggest how can i remove the distinct and replace with any other functions like PARTITION BY to get the output.

    My Query:

    SELECT
          date
          , NAME
          , LONG
          , LAT
         , COUNT ( DISTINCT IMEI) + COUNT ( DISTINCT CASE WHEN IMEI IS NULL THEN 1 END) AS NUMBER_OF_DISTINCT_USERS
         , MAX(MAT) MARKET
          FROM poc.CP_TEST
          WHERE date = to_timestamp('20190130','YYYYMMDDHH24') - INTERVAL '1 DAY'
          and MAT like '%Light%'
         GROUP BY
          date
         ,NAME
         ,LONG
         ,LAT

    I want to replace that distinct with some other functions...

  • skmoh2 - Wednesday, February 13, 2019 11:05 PM

    Hi , I have a query which is taking a lot of time because of a Distinct column.Ca any one suggest how can i remove the distinct and replace with any other functions like PARTITION BY to get the output.

    My Query:

    SELECT
          date
          , NAME
          , LONG
          , LAT
         , COUNT ( DISTINCT IMEI) + COUNT ( DISTINCT CASE WHEN IMEI IS NULL THEN 1 END) AS NUMBER_OF_DISTINCT_USERS
         , MAX(MAT) MARKET
          FROM poc.CP_TEST
          WHERE date = to_timestamp('20190130','YYYYMMDDHH24') - INTERVAL '1 DAY'
          and MAT like '%Light%'
         GROUP BY
          date
         ,NAME
         ,LONG
         ,LAT

    I want to replace that distinct with some other functions...

    This kind of exercise is mostly guesswork without some details of the data distribution and number of rows encountered by the query, most of which can be provided by an execution plan. Can you attach one please? Best would be an "actual"plan, as a .sqlplan attachment.
    Thanks.

    β€œWrite the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • I think you are asking a question about Oracle in a SQL Server forum.

  • Forget about getting the benefit of indexes because of the following snippet:
    and MAT like '%Light%'"

    Every row is going to have to be read, and with a large table, that's going to take a lot of time.
    Probably not much you could to this for that reason.   The DISTINCT count is also part of the
    problem, but an index on that column would eliminate that piece.   Just don't expect that to be
    the solution to the problem.

    Steve (aka sgmunson) πŸ™‚ πŸ™‚ πŸ™‚
    Rent Servers for Income (picks and shovels strategy)

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply