January 20, 2009 at 3:03 am
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).
January 20, 2009 at 3:41 am
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
January 20, 2009 at 3:49 am
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.
January 20, 2009 at 4:01 am
This method should be faster
SELECT rows FROM sysindexes
WHERE id = OBJECT_ID('YourTableName') AND indid < 2
January 20, 2009 at 4:09 am
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
January 20, 2009 at 4:11 am
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.
January 20, 2009 at 4:22 am
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..
January 20, 2009 at 4:30 am
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
January 20, 2009 at 4:37 am
Its working and the result is coming with in seconds. Thanks a lot Ramesh! Thanks All!!
January 21, 2009 at 12:06 am
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
January 21, 2009 at 12:25 am
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).
January 21, 2009 at 12:34 am
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.
January 21, 2009 at 1:09 am
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
January 22, 2009 at 12:56 pm
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