Slow - Linked server query

  • Hi

    I have this slow query that is pulling data from a linked server. This is the slow part of the statement:

      SELECT c_sub.claim_id, MAX(cs.status_type) AS clm_status_type
    FROM dbo.tblBatchKeys k_sub
    INNER JOIN [SQL22.CARE.NET\SQL22NODEASTAGE]PROD.dbo.claim c_sub ON c_sub.claim_id = k_sub.claim_id
    INNER JOIN [SQL22.CARE.NET\SQL22NODEASTAGE].PROD.dbo.claim_procedure ci_sub ON c_sub.claim_id = ci_sub.claim_id AND ci_sub.claim_procedure_id = k_sub.claim_procedure_id
    INNER JOIN [SQL22.CARE.NET\SQL22NODEASTAGE].PROD.dbo.claim_procedure_status cs ON cs.claim_procedure_status_id = ci_sub.claim_procedure_status_id
    WHERE k_sub.BatchSeq BETWEEN @StartSeq AND @EndSeq
    AND ci_sub.claim_procedure_status_id IN (71,72,1,70,2,66,79,5,73,80,16)
    GROUP BY c_sub.claim_id

    Is there any way to speed this up? Tried to use Open Query. But I think I'm doing something wrong:

      SELECT top 10 c_sub.claim_id, MAX(cs.status_type) AS clm_status_type
    FROM dbo.tblBatchKeys k_sub
    INNER JOIN OPENQUERY([PROD], 'SELECT * FROM PROD.dbo.claim ') c_SUB ON c_sub.claim_id = k_sub.claim_id
    INNER JOIN OPENQUERY([PROD], 'SELECT * FROM PROD.dbo.claim_procedure') ci_SUB ON c_sub.claim_id = ci_sub.claim_id AND ci_sub.claim_procedure_id = k_sub.claim_procedure_id
    INNER JOIN OPENQUERY([PROD], 'SELECT * FROM PROD.dbo.claim_procedure_status ') cs ON cs.claim_procedure_status_id = ci_sub.claim_procedure_status_id
    WHERE k_sub.BatchSeq BETWEEN '1111' AND '2222222'
    AND ci_sub.claim_procedure_status_id IN (71,72,1,70,2,66,79,5,73,80,16)
    GROUP BY c_sub.claim_id

     

    The above doesnt seem to be much faster. Any ideas?

     

  • Can't be sure without a query plan, but odds are you'd be much better off running it on the remote server; SQL Server is likely copying the entire tables from the remote server to the local server.

    How large is the local table?  Can you copy the relevant parts of that table to the other server fairly quickly?

     

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

  • For example, if practical:


    EXEC('DROP TABLE IF EXISTS tempdb.dbo.k_sub_remote_query; CREATE TABLE tempdb.dbo.k_sub_remote_query ( claim_id int NOT NULL, claim_procedure_id int NOT NULL, PRIMARY KEY ( claim_id, claim_procedure_id ) )') AT [SQL22.CARE.NET\SQL22NODEASTAGE];

    INSERT INTO tempdb.dbo.k_sub_remote_query ( claim_id, claim_procedure_id )
    SELECT DISTINCT k_sub.claim_id, k_sub.claim_procedure_id
    FROM dbo.tblBatchKeys k_sub
    WHERE k_sub.BatchSeq BETWEEN @StartSeq AND @EndSeq

    EXEC('SELECT k_sub.claim_id, MAX(cs.status_type) AS clm_status_type
    FROM tempdb.dbo.k_sub_remote_query k_sub
    INNER JOIN [SQL22.CARE.NET\SQL22NODEASTAGE]PROD.dbo.claim c_sub ON c_sub.claim_id = k_sub.claim_id
    INNER JOIN [SQL22.CARE.NET\SQL22NODEASTAGE].PROD.dbo.claim_procedure ci_sub ON c_sub.claim_id = ci_sub.claim_id AND ci_sub.claim_procedure_id = k_sub.claim_procedure_id
    INNER JOIN [SQL22.CARE.NET\SQL22NODEASTAGE].PROD.dbo.claim_procedure_status cs ON cs.claim_procedure_status_id = ci_sub.claim_procedure_status_id
    WHERE ci_sub.claim_procedure_status_id IN (71,72,1,70,2,66,79,5,73,80,16)
    GROUP BY c_sub.claim_id') AT [SQL22.CARE.NET\SQL22NODEASTAGE];

    EXEC('DROP TABLE IF EXISTS tempdb.dbo.k_sub_remote_query') AT [SQL22.CARE.NET\SQL22NODEASTAGE];

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

  • Scott's got the right idea imo.  But embedding t-sql in strings and sending row requests over the linked server can be avoided.  More framework is available for this purpose.  Maybe you could use a proc on the remote server that accepts a JSON parameter.  Then serialize the list on the local server and execute the proc

    Aus dem Paradies, das Cantor uns geschaffen, soll uns niemand vertreiben können

  • Thanks Scott and Steve! Very helpful. Good karma to you both, Really appreciate your help.

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

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