Designing Cross Database Queries

  • Comments posted to this topic are about the content posted at http://www.sqlservercentral.com/columnists/sjones/designingcrossdatabasequeries.asp

  • 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

  • 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?.

     

  • 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.

  • 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

  • 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!
  • 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?

  • >>>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

  • 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

  • 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.

  • I agree that the server name in a linked server doesn't have to match, but it gets confusing quickly if it doesn't. It's one thing if you have 5 or 6 servers, quite another when there are 500 servers.

    I still have used linked servers, but I encapsulate everything in a view so the code doesn't need to change if the linked server moves.

  • As always, to each his own.  The code in the view does have to change, and it often has to be different from environment to environment (an often dangerous practice unless you are very careful or don't deal with testing).

    We name our linked servers by logical functionality (typically by database), not by server.  In some cases, we have multiple links on a server pointing to the same physical server, but the functionality (and typically the database) is different.  This has not gotten confusing in my opinion, but this is only dealing with around 40 servers.

    Another benefit is in the case of failover, I don't have to search through my views to see what needs to be altered, I can update my linked servers, and am back in action.

  • All,

    My point here is, we can't create an index on a view, if the view definition contains tables that resides in other databases, coz to create an index on a view, you require schema binding. this is not possible as the table resides in a different database. so whenever we run a query against  a view, the query optimizer is always going to do a table scan on the view. so if we deal with large amount of data, your performance is going to get a hit.

    we use to Create a UDF or a SP in the database that returns the set of records and we call that UDF or SP. even if we need to do any computation or need to show only a subset of columns to the users, i create a view in the same database as the table and do u schema binding and i create the proper indexes required on the view. then i write an SP or an UDF to access the view.

    Am i making sence?

    Ramesh

     

  • Ramesh, you are not correct.  Indexes on the underlying tables will be used, there is nothing in the view to "table scan."  You can see this for yourself in query analyzer's "execution plan" tab if you create & select from a simple cross-db query.

    What you cannot do with cross-db views is create an "indexed view" or "materialized view" but that's another issue altogether.

  • For my part I always enjoy Steve's articles because they are so exceedingly well written - I have 3 separate databases - one is production and there are 2 development/test databases that I am working with - all 3 databases eventually have to be merged since they all belong to the same application (different modules) - I have been dreading this merge (at least a year away) - but Steve's method shows me a simple way out...

    I am the sole developer & dba for this project and all 3 databases are ALWAYS going to reside on the same server...I've never done any cross-querying (rookie and still finding my feet) but you can bet that I am going to start right now....

    Thank you Steve!







    **ASCII stupid question, get a stupid ANSI !!!**

Viewing 15 posts - 1 through 15 (of 56 total)

You must be logged in to reply to this topic. Login to reply