How to get the number of rows from a table reside in a linked server as fast as possible.

  • We have table reside in a linked server having more than 15 million data. Please provide me a T -SQL which can retrieve the number of rows as fast as possible (the query should fetch count from a linked server).

  • abhishek.sahoo (1/20/2009)


    We have table reside in a linked server having more than 15 million data. Please provide me a T -SQL which can retrieve the number of rows as fast as possible (the query should fetch count from a linked server).

    select Count(*) From count(. )

    kshitij kumar
    kshitij@krayknot.com
    www.krayknot.com

  • No, I expect something that can retrieve data faster. Your one will take a longer time to execute if the record count is more than 15 million.

  • This method should be faster

    SELECT rows FROM sysindexes

    WHERE id = OBJECT_ID('YourTableName') AND indid < 2

  • steveb (1/20/2009)


    This method should be faster

    SELECT rows FROM sysindexes

    WHERE id = OBJECT_ID('YourTableName') AND indid < 2

    Here the local sql server's object id will need to apply. but if you will run the same command on the linked server. again the speed isssue will arise especially for such large data

    kshitij kumar
    kshitij@krayknot.com
    www.krayknot.com

  • I am not sure whether we can use sys index to retrieve data from a linked server. The T-SQL you have provided works fine if the table is in same server. What if the table resides in a linked server.

  • abhishek.sahoo (1/20/2009)


    I am not sure whether we can use sys index to retrieve data from a linked server. The T-SQL you have provided works fine if the table is in same server. What if the table resides in a linked server.

    I would run the code on the linked server, if possible..

  • Or you could do something like....

    SELECTi.rows

    FROM[linkedserver].[linkeddb].dbo.sysindexes i

    INNER JOIN [linkedserver].[linkeddb].dbo.sysobjects o ON i.id = o.id

    WHEREo.name = 'HereGoesTheTable'

    AND i.indid < 2

    --Ramesh


  • Its working and the result is coming with in seconds. Thanks a lot Ramesh! Thanks All!!

  • Two things:

    1) Using sysindexes.rows (or any approach based on metadata from the system tables) is not guaranteed to give an accurate value. If you just need an approximate answer as quickly as possible great, otherwise use COUNT().

    2) Be careful with your ON and WHERE clauses across a linked server boundary - in Ramesh's example there's a varchar-based WHERE condition, and unless you've got your collation settings matched up correctly between local and remote servers you can end up with the remote table being pulled across to the local server before the conditions are resolved. It's much better to encapsulate your linked server logic into a stored proc on the remote server so that all of its conditions are resolved on that server, and simply EXEC it from the local server. The resulting data can be either INSERT... EXECed into a local table or passed back with OUTPUT params.

    Regards,

    Jacob

  • Thanks Jacob for your valuable suggestion. Definitely I will try to implement it and let you know if I face any issue. Could you please tell me any other solution where I don't have to create any sp in remote server because we are not the owner of that server and it would be difficult to get permission to create a stored procedure in that server. It is fine if I create a SP in our server (the server belongs to our application).

  • SELECT rows FROM sysindexes

    WHERE id = OBJECT_ID('YourTableName') AND indid < 2

    The above query is good, but it will give the accurate count all the time. Since it takes the count using the index.

    It depends on the index how you created.

    If you need more info on this check msdn help on indexes and rowcount of sysninexs table.

  • @abhishek-2:

    In that case you will have to use Ramesh's approach (if you want to use sysindexes). Make sure that the local server collation matches the remote server collation exactly, and specify Collation Compatible = True on your linker server definition.

    If you want an accurate result you're stuck with COUNT(), in which case collation doesn't matter.

    Regards,

    Jacob

  • The above query is good, but it will give the accurate count all the time. Since it takes the count using the index.

    It depends on the index how you created.

    If you need more info on this check msdn help on indexes and rowcount of sysninexs table.

    sorry for the typo.. it should be....

    The above query is good, but it will [font="Arial Black"]not[/font] give the accurate count all the time. Since it takes the count using the index.

    It depends on the index how you created.

    If you need more info on this check msdn help on indexes and rowcount of sysnindexs table.

Viewing 14 posts - 1 through 13 (of 13 total)

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