Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

How to get the number of rows from a table reside in a linked server as fast as possible. Expand / Collapse
Author
Message
Posted Tuesday, January 20, 2009 3:03 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, November 29, 2011 4:36 PM
Points: 129, Visits: 37
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).
Post #639746
Posted Tuesday, January 20, 2009 3:41 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Friday, October 7, 2011 1:41 AM
Points: 346, Visits: 534
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([link server address]. )


kshitij kumar
kshitij@krayknot.com
www.krayknot.com
Post #639766
Posted Tuesday, January 20, 2009 3:49 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, November 29, 2011 4:36 PM
Points: 129, Visits: 37
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.
Post #639774
Posted Tuesday, January 20, 2009 4:01 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, May 30, 2014 6:27 PM
Points: 2,808, Visits: 7,175
This method should be faster

SELECT rows FROM sysindexes
WHERE id = OBJECT_ID('YourTableName') AND indid < 2

Post #639783
Posted Tuesday, January 20, 2009 4:09 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Friday, October 7, 2011 1:41 AM
Points: 346, Visits: 534
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
Post #639784
Posted Tuesday, January 20, 2009 4:11 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, November 29, 2011 4:36 PM
Points: 129, Visits: 37
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.
Post #639785
Posted Tuesday, January 20, 2009 4:22 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, May 30, 2014 6:27 PM
Points: 2,808, Visits: 7,175
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..
Post #639789
Posted Tuesday, January 20, 2009 4:30 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, October 21, 2013 12:32 AM
Points: 2,551, Visits: 2,593
Or you could do something like....

SELECT	i.rows 
FROM [linkedserver].[linkeddb].dbo.sysindexes i
INNER JOIN [linkedserver].[linkeddb].dbo.sysobjects o ON i.id = o.id
WHERE o.name = 'HereGoesTheTable'
AND i.indid < 2



--Ramesh

Post #639791
Posted Tuesday, January 20, 2009 4:37 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, November 29, 2011 4:36 PM
Points: 129, Visits: 37
Its working and the result is coming with in seconds. Thanks a lot Ramesh! Thanks All!!
Post #639794
Posted Wednesday, January 21, 2009 12:06 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Thursday, March 6, 2014 6:13 PM
Points: 318, Visits: 1,183
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
Post #640451
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse