any ideas on how to spead up this query?

  • As the title says

    select

    id

    , count(distinct company) as CompanyCount

    , count(distinct district) as DistrictCount

    from Tracking

    group by id

    thanks in advance

  • 2 indexes:

    (id, company)

    (id, district)

    _____________
    Code for TallyGenerator

  • Index intersection might work with the 2 indexes, but it might not. If not, you could try the same two indexes and then use a UNION ALL query to put the information back together.

    I'd also experiment with a single index, either id, company, whatever it was, or id, whatever, company.

    Check the execution plans to see which use the indexes best.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • It would be also nice to see actual plan in xml. And maybe some info about a table, e.g. size, structure and indexes.


    I am really sorry for my poor gramma. And I hope that value of my answers will outweigh the harm for your eyes.
    Blog: http://somewheresomehow.ru[/url]
    Twitter: @SomewereSomehow

  • erics44 (8/16/2012)


    As the title says

    select

    id

    , count(distinct company) as CompanyCount

    , count(distinct district) as DistrictCount

    from Tracking

    group by id

    thanks in advance

    Depending on how wide the table is, a nonclustered index on id with included columns company and district may speed up the query. Of course, knowing more about the data would help as well.

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

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