Performance issue

  • On the limited amount of informaton provided, it is difficult to say how to improve the performance.

    The main problem is the CHARINDEX(@query, a.account) > 0, or the equivalent a.account LIKE '%' + @query + '%', which will require an index or a table scan on activedirectory. Also the implicit CAST on the JOIN will not help.

    You could try with the following indexes:

    CREATE NONCLUSTERED INDEX IX_ADCover

    ON activedirectory (account, employeeid, displayname)

    CREATE NONCLUSTERED INDEX IX_WPDisplayName

    ON WhitePage (displayname)

    CREATE NONCLUSTERED INDEX IX_WPEmpNumDisplayName

    ON WhitePage (empnum, displayname)

    using an UNION query:

    ;WITH ADQueryResult

    AS

    (

    SELECT employeeid, account, displayname

    -- Do cast here.

    ,'00000' + employeeid AS empnum

    FROM activedirectory

    WHERE account LIKE '%' + @query + '%'

    )

    SELECT TOP 20

    A1.employeeid AS [ID], A1.account, A1.displayname AS [Name]

    ,W1.workphonenumber AS phone, W1.emailaddress AS email

    FROM ADQueryResult A1

    JOIN WhitePage W1

    ON A1.displayname = W1.displayname

    UNION ALL

    SELECT

    A2.employeeid, A2.account, A2.displayname

    ,W2.workphonenumber, W2.emailaddress

    FROM ADQueryResult A2

    JOIN WhitePage W2

    ON A2.empnum = W2.empnum

    AND A1.displayname <> W1.displayname

    -- Top without an ORDER BY is non-deterministic

    ORDER BY [Name]

  • Okay this is like duplicate post #7 for this problem. I also provided an answer here. Which I would not have wasted my time with had there been only 1 thread started for this problem, since I said basically the same thing without the code.

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

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