|
|
|
SSC-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).
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Friday, October 07, 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
|
|
|
|
|
SSC-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.
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Wednesday, May 08, 2013 7:42 AM
Points: 2,802,
Visits: 7,103
|
|
This method should be faster
SELECT rows FROM sysindexes WHERE id = OBJECT_ID('YourTableName') AND indid < 2
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Friday, October 07, 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
|
|
|
|
|
SSC-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.
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Wednesday, May 08, 2013 7:42 AM
Points: 2,802,
Visits: 7,103
|
|
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..
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Friday, February 22, 2013 12:03 AM
Points: 2,555,
Visits: 2,587
|
|
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
|
|
|
|
|
SSC-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!!
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Tuesday, April 09, 2013 4:24 AM
Points: 318,
Visits: 1,172
|
|
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
|
|
|
|