SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Designing Cross Database Queries


Designing Cross Database Queries

Author
Message
Steve Jones
Steve Jones
SSC Guru
SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)

Group: Administrators
Points: 145843 Visits: 19425
Comments posted to this topic are about the content posted at http://www.sqlservercentral.com/columnists/sjones/designingcrossdatabasequeries.asp

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
Ramesh Kilputhur Sridharan
Ramesh  Kilputhur Sridharan
SSC Journeyman
SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)

Group: General Forum Members
Points: 78 Visits: 13

Your Idea looks pretty good, if we are dealing small amount of data.

in case, if the data grows larger, the performace going to get a hit.

why i am saying this is, we can't index the views, like you siad in your examples. since it requires schema binding. so Querying or any DML statments on the view that is created from other database tables will be much slower.

i hope that i make sence

ramesh





rs
rs
Mr or Mrs. 500
Mr or Mrs. 500 (553 reputation)Mr or Mrs. 500 (553 reputation)Mr or Mrs. 500 (553 reputation)Mr or Mrs. 500 (553 reputation)Mr or Mrs. 500 (553 reputation)Mr or Mrs. 500 (553 reputation)Mr or Mrs. 500 (553 reputation)Mr or Mrs. 500 (553 reputation)

Group: General Forum Members
Points: 553 Visits: 94

Steve,

Good solution. I agree partially with Ramesh comments, If data grows there will be performence hit but we could create index on view.

How about putting same logic in user defined function passing parameters to filter out the data?.





bbdpres
bbdpres
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1192 Visits: 80

Ramesh,

I disagree. When the databases are on the same server, query plans are built the same from a view in the view-database as if it were querying on the base-table-database. You still have the ability to index the base tables to optimize the views. You only lose the functionality when you use linked servers in the views.

We have an environment for reporting here that needs to use dynamic aggregations on products. The product aggregations are on what we call market definitions and product groups (1-m-m; market def-prod grp-prod). Users have the ability to add and remove products from these groupings based on rules regarding attributes of the products. In our environment, we have a reference database with the most recent product data available that have views in the market definition database referencing them. This allows us to load a separate (parallel) database for the product reference while still using the prior load's data for updates to the market definitions. Once the new product data is loaded, we switch database names - making the newly loaded data the live reference data. Views work great with this implementation.

On the reporting end, we have several data marts that have views in them referencing the OLTP (Market Definition) database (we actually do the same for data mart loads - load to a separate database and switch database names). This is the only time I've ever considered mixing OLTP and Reporting applications' databases on the same server.

My point is that during tuning exercises, we've been very successful in tuning the base tables to improve view performance.





battelofhalfwits
battelofhalfwits
SSC-Enthusiastic
SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)

Group: General Forum Members
Points: 125 Visits: 16

For clarification you can index views but only the Enterprise, Developer, and Evaluation versons of SS2K support them. You can how ever create them in all versions of SS2K for migration from one edition to another, but the query optimiser will not use the index in Standard and Personal versions of SS2K.

I don't know if SS2005 changed this though.




Imagination is more important than knowledge.

– Albert Einstein

YSLGuru
YSLGuru
SSCarpal Tunnel
SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)

Group: General Forum Members
Points: 4412 Visits: 1667

My company produces accounting software for a specific industry. Often our clients have multiple database's because the government or some other legal entity has one or more regulations requiring this seperation. For these clients it is impossible to perform consolidated reporting using standard reports provided since they have data in multiple databases. The idea of using a view to pull data from other databases is in my opinion a great idea. Using aview minimizes the amount of work needed and the amount of space required to do this. Copyinig data from many databases to a single DB takes time & resources such as space. Why do something like this when views that pull in data from other DB's can be used to do the same thing?

Ed



Kindest Regards,

Just say No to Facebook!
Mike Good
Mike Good
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1697 Visits: 1042

I like the view idea better than explicitly referencing objects in other DBs.

But to me the idea of having to grant my users rights to the underlying tables in the other DBs is significantly worse/more painful than simply enabling cross-database ownership chaining.

On my production servers the only db-owner we allow is sa, and cross-db ownership chaining is enabled. We use similar approach for cross-db views, and users have no rights to underlying tables. Is there some security downside to this approach that I'm missing?





noeld
noeld
SSC-Insane
SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)

Group: General Forum Members
Points: 22614 Visits: 2048

>>>For clarification you can index views but only the Enterprise, Developer, and Evaluation versons of SS2K support them.<<<

Allow me to differ

Indexed views are suported in the standard edition also and the only thing you have to keep in mind is that you must use the NO EXPAND option when refering to the indexed views!




* Noel
noeld
noeld
SSC-Insane
SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)

Group: General Forum Members
Points: 22614 Visits: 2048

Ramesh,

The views on sql server are EXPANDED by default when creating the execution plan therefore using the correct indexes on the base tables (like bbdpres suggested) will give you the performance benefits required when tunning.

The only case that views afect performance is when the nested level is extremely high and you are performing some unneeded joins or calculations

HTH




* Noel
okie_greg
okie_greg
SSChasing Mays
SSChasing Mays (617 reputation)SSChasing Mays (617 reputation)SSChasing Mays (617 reputation)SSChasing Mays (617 reputation)SSChasing Mays (617 reputation)SSChasing Mays (617 reputation)SSChasing Mays (617 reputation)SSChasing Mays (617 reputation)

Group: General Forum Members
Points: 617 Visits: 26

I prefer to use a linked server. This method is the most flexible and supports multiple environments (dev,test,prod).

You imply in your article that the linked server name must be the actual server name. This is not true. Instead of using the "SQL Server" option in the linked server creation dialog, if you use the "Other data sources" option and point to the OLE DB driver for SQL Server, you can now set the linked server up with any name you want. Now, you can have a linked server named PubsLink in every environent and code does not have to change, only the details of the linked server. Also, when that subscription database grows in size or users to the point where you HAVE to move it off to it's own server, there are no code changes to make, only details of the linked server and it just works.





Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search