Scott on Writing

Musings on technical writing...

A Tool For Querying Multiple Databases

I recently blogged about different multi-tenant data architectures, comparing and constrasting the Separate Databases and Shared Database, Shared Schema architectures, as well as noting what sorts of questions to ask when trying to ascertain which model to use. One of the disadvantages of the Separate Databases approach is that it can be difficult to view data aggregated across the databases:

Viewing data aggregated across the databases is difficult. I've touched upon this topic in an earlier blog post, Running the Same Query Against Multiple Databases. When you find a bug on one database and need to see whether it affects data in other databases there are not many tools at your disposal. One poor man's tool is sp_msForEachDb, but it's less than ideal.

And reader John Chapman added his two cents in the comments regarding this issue:

For lots of applications you actually have situations where there are users who need to be able to see data from multiple customers at the same time. For example you may have situations where you have external customers who see only their data, but yet have internal liaisons who need to oversee the activities of multiple customers. Therefore necessitating that they see data from multiple customers on a single screen.

I have ran into these situations before, which was a key reason why we used a single database shared schema. The application we replaced used separate databases and was unable to provide this sort of functionality.

Over the years I have created a very (very!) rough tool for querying multiple databases in a Separate Databases architecture. In short, you enter a query, select which databases to query against, and then the tool runs that query against each selected database and combines the results into a single <table> on the page. As you can see from the screen shot below (click for a larger version), the tool includes a multi-line textbox for entering the query to execute and a CheckBoxList of the databases to query. The results are included in a single <table>.

While the above screen shot shows a query that just returns a scalar value (one column, one row), it certainly works with queries that return multiple rows and columns. And with a little bit of legwork the tool could be enhanced to include rollup-type functionality, showing subtotals per database and grand totals across all selected databases for numeric columns.

To learn more about how I created this tool, check out my latest 4GuysFromRolla.com article: Querying a Multi-Tenant Data Architecture.

posted on Tuesday, September 08, 2009 3:15 PM

Feedback

# re: A Tool For Querying Multiple Databases 9/8/2009 6:22 PM Michael K. Campbell

Scott,

I've given this some thought a few times on my own. But, ultimately, I've been INSANELY happy with Red Gate's SQL Multi-Script. In fact, I probably use it weekly or more - and every time I use it I'm tickled pink by how easy it is to use, and how it makes my life sooo much easier.

# Dew Drop &#8211; September 9, 2009 | Alvin Ashcraft&#039;s Morning Dew 9/9/2009 4:31 AM Pingback/TrackBack

Dew Drop &#8211; September 9, 2009 | Alvin Ashcraft&#039;s Morning Dew

# A Tool For Querying Multiple Databases - Scott Mitchell 9/9/2009 8:56 AM DotNetShoutout

Thank you for submitting this cool story - Trackback from DotNetShoutout

# A Tool For Querying Multiple Databases 9/9/2009 1:31 PM PimpThisBlog.com

Thank you for submitting this cool story - Trackback from PimpThisBlog.com

# re: A Tool For Querying Multiple Databases 9/10/2009 8:07 AM Scott Mitchell

Michael, thank you for the suggestion re: Red Gate's SQL Mutli Script. I had no idea this tool existed. It certainly fits the bill for what I need to do.

However, I can see where a customized tool (like the one I created) would be needed in scenarios where you needed to limit what sorts of queries could be executed. E.g., you might want to allow internal liasons run SELECT queries, but not INSERT, UPDATE, or DELETE. Also, I could see the need for a tool where you don't allow the user to enter ad-hoc queries, but rather only the ability to run pre-defined queries against a user-selectable set of databases.

But for DBAs and trusted developers, SQL Multi Script rocks!

# A Tool For Querying Multiple Databases 10/10/2009 10:17 PM Servefault.com

Thank you for submitting this cool story - Trackback from Servefault.com

Title:  
Name:  
Url:
Protected by Clearscreen.SharpHIPEnter the code you see:
Comments   

My Links

Ads Via DevMavens

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<March 2010>
SMTWTFS
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910

Comment Stats

DayTotal% of Total
Sunday 2056.8%
Monday 42514.1%
Tuesday 51917.2%
Wednesday 55518.4%
Thursday 58019.2%
Friday 54718.1%
Saturday 1886.2%
Total 3019100.0%

Hour1Total% of Total
12:00 AM 782.6%
1:00 AM 812.7%
2:00 AM 682.3%
3:00 AM 822.7%
4:00 AM 692.3%
5:00 AM 1264.2%
6:00 AM 1183.9%
7:00 AM 1816.0%
8:00 AM 1926.4%
9:00 AM 1585.2%
10:00 AM 1886.2%
11:00 AM 1936.4%
12:00 PM 2016.7%
1:00 PM 1846.1%
2:00 PM 1695.6%
3:00 PM 1354.5%
4:00 PM 1153.8%
5:00 PM 1073.5%
6:00 PM 1013.3%
7:00 PM 1073.5%
8:00 PM 923.0%
9:00 PM 882.9%
10:00 PM 913.0%
11:00 PM 953.1%
Total 3019100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 4.97159
Monday 4.80384
Tuesday 4.04477
Wednesday 7.39680
Thursday 6.26676
Friday 5.07466
Saturday 4.78177
Total 5.403019

Hour1 Entry MadeAvg.Total
12:00 AM 5.2937
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 3.8550
8:00 AM 3.72134
9:00 AM 6.06297
10:00 AM 5.63276
11:00 AM 4.22194
12:00 PM 6.16351
1:00 PM 3.09133
2:00 PM 4.89230
3:00 PM 7.64321
4:00 PM 4.00108
5:00 PM 6.07170
6:00 PM 4.64116
7:00 PM 8.95188
8:00 PM 8.63164
9:00 PM 5.00115
10:00 PM 6.31101
11:00 PM 4.5732
Total 5.403019

Learn More About Comment Stats
1 - All times GMT -8...


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles