Page view counter

Correct Architecture for Typed DataSet N-Tier

Last post 05-21-2008 1:07 AM by danludwig. 57 replies.

Sort Posts:

  • Re: Correct Architecture for Typed DataSet N-Tier

    11-13-2006, 8:08 AM
    • Loading...
    • vivek_iit
    • Joined on 06-18-2006, 6:13 PM
    • New Delhi
    • Posts 3,125
    • Points 17,294
    • TrustedFriends-MVPs

    >I thought SQL views out performed their sql statement counterparts... Or have I got that wrong?

    Logically speaking, I always thought that using Views will degrade performance compared to using SPs or queries. Though I haven't done any specific performance test but I think if we use views, the DB will need to do some extra work, which will hit performance (I am considering pure performance only here). Also, I thought these links can be useful in this context (these are ofcourse debatable):

    http://www.sql-server-performance.com/views.asp

    http://techrepublic.com.com/5208-6230-0.html?forumID=88&threadID=200730&messageID=2091959

    Let me know if anyone disagrees!

    Thanks,

    Vivek

    CodeASP.NET Community

    Please mark the most helpful reply/replies as "Answer".
  • Re: Correct Architecture for Typed DataSet N-Tier

    11-13-2006, 10:21 AM
    • Loading...
    • jamesmoore123
    • Joined on 11-10-2006, 10:16 AM
    • Posts 17
    • Points 85

    Cool, thanks for the reply...

    I'm really not trying to start an argument, but I do want to get to the bottom of the issue..

    I've seen arguments for and against, but like most things I guess it's dependant on the exact situation...though I'm still to find the diffinative answer..

    The first article mentions performance degradation being caused by the extra "select" statement when using single tables (because doing a select on a single table view is a little like doing "select * from (select * from table1) t1") which I can't imagine has a huge performance impact, but I totally agree that creating a view on a single table isn't going to help anything anyway. It also makes the obvious point about selecting excess data and doing unnecessary joins when using views isn't a great idea - but the same is true of a select statement (though I guess there is a temptation to make a view all seeing then only select the few columns you need)... The second article is talking about triggers on views, which is obviously something to avoid as it will create a huge "monitoring overhead" on the database, but some people commented on that article with comments I believe are no longer true (at least in SQL server 2000 and 2005), particularly about views not using indexing (I'm not talking about indexed views here), because any view will use the underlying data tables and indexes.  MS state that views will execute in exactly the same way as the SQL statements they are equivelent to, which includes indexing etc...

    So why do I think there is a performance gain???

    If you take a complex SQL statement (which has several outer joins - yuk!!) it obviously takes time for SQL server to compile the SQL and build the execution plan, the executed plan is then stored in the cache incase it's called again, the same happens with views so there is little performance difference when called just once. The bonus comes when it's called a second time with different arguments (such as orders for a different company), the SQL statement no longer matches the original so a new execution plan for the whole complex query is compiled (time consuming), but in the case of views because the view remains the same then it does not need to recompile the SQL (though to be correct I think it must still need to compile just the "select x from view1" part, but not the whole thing), and it can simply recreate the execution plan... And I think thats where the performance gain comes, as a little experiment (not scientific but hey) I've just ran the following view and it's sql equivilent  (which is one of the more complex ones I could find - not mine I hasten to add, infact seeing the image links in there made me spit coffee everywhere...)

    select

    * from vw_list_company_activities where parent_id=1

     

    VS

     

     

    SELECT

    TOP (100) PERCENT dbo.tbl_activities.activity_id AS [Activity Id], dbo.tbl_activities.company_id AS parent_id,

    dbo

    .tbl_activities.activity_date AS Date_Time, dbo.lkp_activity_type.type AS Action, dbo.vw_list_staff.Staff_Name, dbo.tbl_branches.branch_name,

    dbo

    .tbl_contacts.firstname + ' ' + dbo.tbl_contacts.lastname AS Contact, CONVERT(varchar(50), dbo.tbl_activities.notes) AS Notes,

    CASE WHEN tbl_activities.has_attachment = 1 THEN '<img src=images/attach.gif>' ELSE '' END AS [<img src=images/attach.gif>]

    FROM

    dbo.tbl_activities LEFT OUTER JOIN

    dbo

    .tbl_contacts ON dbo.tbl_activities.contact_id = dbo.tbl_contacts.contact_id LEFT OUTER JOIN

    dbo

    .vw_list_staff ON dbo.tbl_activities.staff_id = dbo.vw_list_staff.staff_id LEFT OUTER JOIN

    dbo

    .lkp_activity_type ON dbo.tbl_activities.activity_type_id = dbo.lkp_activity_type.activity_type_id LEFT OUTER JOIN

    dbo

    .tbl_branches ON dbo.tbl_activities.branch_id = dbo.tbl_branches.branch_id

    where

    dbo.tbl_activities.company_id=1

    ORDER

    BY Date_Time

     

    I restart SQL server between the 2 to force the cache to clear, and executed both 3 times with different ids, the view version averaged at 7.5 (in the client statistics - so I guess it must be in millisecs, though that sounds a bit fast, perhaps it's 100ths of a second?) and the sql version averaged at 78.5. I've then created a stored procedure version which averaged out at 12 - of course this isn't really an accurate test.. but it's enough to make me suspect that perfomance (at least for this type of query) can be given a boost by views..

     

  • Re: Correct Architecture for Typed DataSet N-Tier

    11-13-2006, 11:34 AM
    • Loading...
    • shados
    • Joined on 07-07-2006, 11:24 PM
    • Posts 2,198
    • Points 12,098

    There's a issue with that argument James: the way SQL Server caches query plans, more or less (its oversimplification, i'll admit), as long as the only thing changing is the where clause, the plan is still valid, even for a normal SQL Statement. So if there's a performance gain, it doesn't come from there.

    literally: anything in SQL server goes through the same "pipe", so to speak. Inline statements, views, SPs. Its just a different way to manage them, and some have a few additional preprocessing feature (like permissions on views), etc. As far as I know, when you have a view, then do a select statement on the view, it gets thrown in the parser, all the duplicate junk (so if you do select * on a view, it won't do 2 selects).

    Performance should be almost exactly the same, and I have a feeling the performance difference in the above has more to do with low level (OS/hardware) caching more than anything. If you do an average over 10000 calls and use System.diagnostic and call all 3 from .NET, it might be a bit more precise.

    If you use indexed views, then its another story altogether.

    Note that my comment on the way views are handled is, however, little more than an educated guess. 

  • Re: Correct Architecture for Typed DataSet N-Tier

    11-13-2006, 2:18 PM
    • Loading...
    • jamesmoore123
    • Joined on 11-10-2006, 10:16 AM
    • Posts 17
    • Points 85

    I never knew that about the query plan, I always thought it broke it down into mini cachable sections (I dunno why, perhaps I read it somewhere?),  in that case then you're right logically you shouldn't see a performance boost... But it seems quite noticeable... soooo (being the anorak I am) I did as your suggested a wrote a quick app to benchmark them on several queries which ran 1000 of times... and I can confirm that after the first execution they all perform more or less the same (with the inline SQL version executing  very slightly slower, but I put that down to the extra text in the commands, it really was minimal). What is interesting is that the first run of each query was quite a bit quicker for views and sps when compared to inline SQL... Which must be something to do with the commands being preparsed or simlar (even for error checking?). Overall there really wasn't much in it once it had ran a few hundred times... So I suppose I've answered my own question there... 

    Still, using views isn't a bad way to manage your SQL statements so I won't be routinely binning them just yet..

     

     

  • Re: Correct Architecture for Typed DataSet N-Tier

    11-13-2006, 3:07 PM
    • Loading...
    • vivek_iit
    • Joined on 06-18-2006, 6:13 PM
    • New Delhi
    • Posts 3,125
    • Points 17,294
    • TrustedFriends-MVPs

    >shados:  Performance should be almost exactly the same.......

    I have pasted this excerpt taken from http://www.microsoft.com/technet/community/chats/trans/sql/sql0909.mspx:

    Host: Conor (Microsoft)
    Q:
    Sami: What performance impact will using a view (with joins on 4 tables) with additional where clause have as opposed to using the tables directly?
    A: Assuming you mean a non-indexed view, it doesn't matter at all. We actually expand the view and look at all the tables as if you had written it using the original tables

    So for simple joins, performance is equivalent as execution plan would be exactly the same as that of quering the tables. So there should not be any performance benefit at all. But for some cases there can be a performance hit (mentioned earlier), so James is somewhat right when he said it really depends on the situation. But there is still no evidence to suggest that non-indexed views can improve performance.

    Besides, indexed views can also hurt performance if used improperly (as the cost of maintaining the view is also taken into account)!

    So, I think overall it was nice to have this discussion.

    Vivek

    CodeASP.NET Community

    Please mark the most helpful reply/replies as "Answer".
  • Re: Correct Architecture for Typed DataSet N-Tier

    11-13-2006, 4:07 PM
    • Loading...
    • LudovicoVan
    • Joined on 12-02-2004, 3:01 PM
    • The World's End
    • Posts 1,725
    • Points 8,569

    > shados:  Performance should be almost exactly the same.......

    Exactly. And I had myself warned about that being simply a non-issue.

    > so James is somewhat right when he said it really depends on the situation.

    After spending my time in elaborating about choosing the right tool based on the situation, I would just say amen.

    Btw, lots of guesses around. If one needs the good out of views, as well as with anything else, simply read documentation about when/how/why to use them...

    Since I am fair, again amen.

    Cheers. -LV

    Julio P. Di Egidio
    Software Analyst Programmer
    =BUSINESS AND SCIENTIFIC=
    =SOFTWARE DEVELOPMENT=
    http://julio.diegidio.name

    (Peace X Love] = [++1)
  • Re: Correct Architecture for Typed DataSet N-Tier

    11-13-2006, 4:22 PM
    • Loading...
    • shados
    • Joined on 07-07-2006, 11:24 PM
    • Posts 2,198
    • Points 12,098
    Now that this is all understood and clear, 10$ my next contract will be with Oracle or Postgres or something, and Ill have to redo all this research from scratch.
  • Re: Correct Architecture for Typed DataSet N-Tier

    11-13-2006, 4:41 PM
    • Loading...
    • LudovicoVan
    • Joined on 12-02-2004, 3:01 PM
    • The World's End
    • Posts 1,725
    • Points 8,569

    shados:
    Now that this is all understood and clear, 10$ my next contract will be with Oracle or Postgres or something, and Ill have to redo all this research from scratch.

    I here imply you mean the search for the help button...

    -LV

    Julio P. Di Egidio
    Software Analyst Programmer
    =BUSINESS AND SCIENTIFIC=
    =SOFTWARE DEVELOPMENT=
    http://julio.diegidio.name

    (Peace X Love] = [++1)
  • Re: Correct Architecture for Typed DataSet N-Tier

    11-19-2006, 10:27 PM
    • Loading...
    • jasonjanofsky
    • Joined on 03-19-2004, 6:57 PM
    • San Diego
    • Posts 140
    • Points 618

    So guys, what then would be the best way to run a setup with typed datasets in which you are filtering data.  Lets take a simple example.

     

    Gridview and detailsview on the page.  Two ways to populate both gridview and detailsview:

    1. getdata query for the gridview and getdatabydatakeyID for the detailsview

          a. this runs two queries on your db, and a query every time you pull a new record, however, this binds nicely to the ODS.

    2. Back end binding in code to a dataset which on gridviewselect pulls the recordrow from the bound dataset.

         a. Not as many queries but you are writing code on the backend to get the solution working.

     

    Which way is best, and is there another way that I am missing to make this all work well?

    Feel free to hit me on MSN, I will try to help if I am not super busy.
  • Re: Correct Architecture for Typed DataSet N-Tier

    11-20-2006, 12:39 PM
    • Loading...
    • vivek_iit
    • Joined on 06-18-2006, 6:13 PM
    • New Delhi
    • Posts 3,125
    • Points 17,294
    • TrustedFriends-MVPs
    jasonjanofsky:

    So guys, what then would be the best way to run a setup with typed datasets in which you are filtering data.  Lets take a simple example.

     

    Gridview and detailsview on the page.  Two ways to populate both gridview and detailsview:

    1. getdata query for the gridview and getdatabydatakeyID for the detailsview

          a. this runs two queries on your db, and a query every time you pull a new record, however, this binds nicely to the ODS.

    2. Back end binding in code to a dataset which on gridviewselect pulls the recordrow from the bound dataset.

         a. Not as many queries but you are writing code on the backend to get the solution working.

     

    Which way is best, and is there another way that I am missing to make this all work well?

    I think that you should try to avoid the extra DB hit, just save the DataSet in the Session and based on the selected item populate the details view quering the DataSet only. This should work fine unless the DataSet itself is too large hogging up expensive server memory.

    HTH,

    Vivek

    CodeASP.NET Community

    Please mark the most helpful reply/replies as "Answer".
  • Re: Correct Architecture for Typed DataSet N-Tier

    11-20-2006, 1:31 PM
    • Loading...
    • jasonjanofsky
    • Joined on 03-19-2004, 6:57 PM
    • San Diego
    • Posts 140
    • Points 618

    So then, is there a way to use the object datasource to query the session object?

     

    Feel free to hit me on MSN, I will try to help if I am not super busy.
  • Re: Correct Architecture for Typed DataSet N-Tier

    11-21-2006, 2:03 AM
    • Loading...
    • vivek_iit
    • Joined on 06-18-2006, 6:13 PM
    • New Delhi
    • Posts 3,125
    • Points 17,294
    • TrustedFriends-MVPs
    jasonjanofsky:

    So then, is there a way to use the object datasource to query the session object?

    When you are using typed datasets as your middle layer, you can use Table adapters in your typed datasets acting as the DAL where you define the GetXX() methods at design time. This approach is suitable for ObjectDatasource control but will query the DB when each method is executed. You can use the Fill() methods of the TableAdapter and re-use the DataSet by savingit in memory. But then I don't think you can use ObjectDataSource to "query" the saved DataSet, for this you need to write custom code, which should not be difficult.

    Here is a link which might help you:

    http://quickstart.developerfusion.co.uk/QuickStart/aspnet/doc/ctrlref/data/objectdatasource.aspx

    HTH,

    Vivek

     

    CodeASP.NET Community

    Please mark the most helpful reply/replies as "Answer".
  • Re: Correct Architecture for Typed DataSet N-Tier

    05-21-2008, 1:07 AM
    • Loading...
    • danludwig
    • Joined on 10-20-2006, 10:12 AM
    • Posts 75
    • Points 95

    Leave it to me to revive all of the dead 4 year old discussions but.... It seems like the answer to the original question became a debate about speed. Does faster always necessarily mean "Correct?" Like shados says, architects should be focused on the cost of the application and how making speed a system priority can increase that cost. What about maintainability? What about flexibility? When your CIO comes to you and tells you your company needs to quickly adapt it's web interface, web services, business processes, and yes, ultimately, parts of your data model, or perish in an industry where your competitors have sexier and simpler web interfaces, more customer and supplier web services, cheaper and more streamlined business processes, and yes, ultimately, a better data model, what do you want to be able to tell him?

    Typed DataSets are invaluable, but look through the pipe from the other end. All meaningful web activity starts at the UI. Your pages, web controls, user controls, codebehind, AJAX, etc, are all concentrating on displaying the data and controlling how the user interacts with that display. It shouldn't be bogged down having to worry about how to CRUD everything too, so of course you're going to want a layer separation there. Microsoft made it easy to be able to connect your databound controls (like FormView, GridView, DropDownList, etc) to ObjectDataSources that can bind with DataTables. However, when your applications grow in complexity, which they can rather quickly in the real world as user and management requirements evolve, this is not a good idea. DataTables and DataSets are a data access layer, period. Do you really want your UI layer connected directly to your DAL through a mere ObjectDataSource?

    I came into .NET in 2005 from Java/J2EE, and the ObjectDataSource is the perfect device for hooking your UI into what we called a facade: a layer that hides all of the complexities of managing the data by wrapping operations up in specific method calls that each correspond to a system use case. However, that layer needs to operate on data objects to its job. How else could it deliver a complex object collection through a web service method? Again, do you want all of these client methods to have to create all of the TableAdapters and/or DataTables, optionally invoke SetXyzNull() umpteen times when adding rows, and manage multiple tables for one data object that is a disjointed specialization of another base data object (and all of its base classes)? It's rather easy and straightforward to wrap your DAL behind a taxonomy of data objects, what we called "POJOs", or Plain Ol' Java Objects, in J2EE. So instead of saying "new XyzDataTable().NewXyzRow()," it's much easier to say "new Xyz()." Then you can just call MyXyz.Save(), MyXyz.Purge(), etc, and use polymorphism to make sure the object's base data is saved into its (possibly separate) table(s).

    What's more is you can use the same object model from codebehind if necessary, for example, when validating whether an entered value would create a duplicate (like when creating usernames). You can create and manipulate data objects on the fly from an event handler where using ObjectDataSources wouldn't make sense, and you still keep the DAL (TableAdapters, DataTables, etc) disconnected from the UI. Now that we have the UI interacting only with the facade and the object model, we get back to the DAL. What was the original debate? SQL-based TableAdpater methods versus stored procedures? Why would you want to put your developers through the tedium of creating 4 or more stored procedures for every table in your db if they could have VisualStudio do 90% or more of them in less than 1% of the time? I think it makes sense to have more complex, highly parameterized SQL (like advanced searches) wrapped in stored procedures simply because it's easier to open, read, and update a SQL file in Management Studio than it is to work with the SQL "Query Builder" built into the VS TableAdapter wizard. Keep your database people focused more on what triggers and constraints they need to have in place to maintain data integrity.

    The only question now comes back to the subject of this discussion: do you want to allow the UI access to the DAL. If you do, you have a triangular MVC architecture. Otherwise, you can put the DAL in the same library as the facade & object model, mark all of the TableAdapters "internal", and you instead have a linear 3-tier architecture where the DAL is entirely wrapped up behind the "POC#Os". For it to be n-tier, you need to make sure the UI absolutely cannot access the DAL, or any aspects of the physical model. You want both the UI and the facade working with the "conceptual" taxonomy of data objects, which in turn encapsulate the physical table structures.

    I guess, in other words, I agree with shados.

Page 4 of 4 (58 items) < Previous 1 2 3 4