If I have same table and same multiple database servers. How to connect to multiple database servers, obtain those records from each database server and then display the first 10 of the combined results ?
-
Im tried get all records in server master and then sort by DateTime and get 10 records! – no camer Nov 12 '13 at 15:08
-
Do you mean in Sql Server or connecting to each Sql Server db using C#? – Ben Nov 12 '13 at 15:12
-
In MS SQL Server you can use linked servers. Then in .NET you can access using the 4 part name and even join table on different servers. – paparazzo Nov 12 '13 at 15:36
-
join table on different servers ?You can give me a reference for more ? – no camer Nov 12 '13 at 15:38
2 Answers
Here are a couple solutions off the top of my head.
Solution 1:
1 - Create a staging database / table on server A.
2 - Import all data from all servers into table.
3 - Query table to get results.
Solution 2:
1 - Create a Linked server for each server B .. Z on server A.
2 - Create Query using 4 part notations on linked servers.
Overall, solution 2 can be slow since you are using distributed transactions.
Solution 1 allows you to store the aggregated results that can be indexed (for speed) and can be queried multiple times.
As for importing the data from server to server, just pick a way to do it. There are two many solutions are there to get into the particulars.

- 14,351
- 4
- 26
- 30
-
thanks @CRAFTY DBA, but i think Solution 1 not good for large data,10000000 rows? – no camer Nov 12 '13 at 15:22
-
@nocamer Why on earth would you query 10 million rows? If you need top 10 you only need the top 10 of each server - wich would make 40 results and take the top 10 of those? either one server got all the newest or its a mix of all 4 – Patrick Artner Jul 25 '18 at 05:43
Say for instance you are querying the multiple instances using different connection strings for a sample table Orders, you could try the following:
var orders = ConfigurationManager.ConnectionStrings.Cast<ConnectionStringSettings>()
// filter to the relevant connection strings
.Where(s => s.ConnectionString.ToLower().Contains("metadata"))
.SelectMany(s => {
// for each connection string, select a data context
using(var context = new NorthwindEntities(s.ConnectionString)) {
// for each context, select all relevant orders
return context.Orders.ToArray();
} // and dispose of the context when done with it
})
.Take(10)
.ToList();

- 100,957
- 23
- 236
- 235
-
-
Not sure if the above will work across multiple servers but have a go at least. You can refer to these questions for more info http://stackoverflow.com/q/6048718, http://stackoverflow.com/q/2510309, http://stackoverflow.com/a/4281113 and http://stackoverflow.com/q/1495357 – chridam Nov 12 '13 at 15:42
-
Do you think there are more optimized ways to reduce the strain on the server? – no camer Nov 12 '13 at 16:02
-
Hmmm good question, from the references and other research I am yet to see an optimized path to the problem unfortunately. – chridam Nov 12 '13 at 16:05