Distinct on Varchar column

  • The below TSQL query takes about 19 minutes to run

    SELECT count(distinct SessionID) ) FROM SessionTracker

    Sessionid is a varchar(138)

    Through SQL profiler - I notice implicit conversion taking place when running distinct option.

    Select count(sessionid) from sessiontracker

    - runs in milliseconds.

    Any tips to run the first query quicker is appreciated.

    Thanks!

  • Take a look at the execution plan for the query. That will tell you what's happening with it. DISTINCT is an aggregation command. To get the best performance out of it, you'll need an index on that column that it can use. The execution plan should help lead you there.

    ----------------------------------------------------The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood... Theodore RooseveltThe Scary DBAAuthor of: SQL Server 2017 Query Performance Tuning, 5th Edition and SQL Server Execution Plans, 3rd EditionProduct Evangelist for Red Gate Software

Viewing 2 posts - 1 through 1 (of 1 total)

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