| Author |
Topic |
|
Kristen
Test
22859 Posts |
Posted - 2005-01-23 : 14:53:07
|
I have a content management system. A "Page" is made up of several "items" (e.g. paragraphs). There is then a "Content" table that contains all the bits of content - each Version of the content gets a new ID.There is then a "Group" table which records each Content Record, and its Version. All the versions of a given piece of Content are given the same Group ID.Each Page Item record the appropriate Group ID. Thus, for a given Page Item, using the Group table, all the versions of the Content can be listed.What's the most efficient way to get all the most recent (highest version number) Content records for a given Page (all Items for that Page)?Here's my attempt:SET NOCOUNT ON CREATE TABLE MyPage( PageID int, PageItem int, PageTitle varchar(255), PageGroupID int)GOCREATE TABLE MyGroup( GroupID int, GroupVersion int, GroupContentID int)GOCREATE TABLE MyContent( ContentID int, ContentText text -- Note: This is a TEXT column in the actual program)GOINSERT INTO MyPageSELECT 1001, 1, 'Home Page', 20011 UNION ALLSELECT 1001, 2, 'Welcome Paragraph', 20012 UNION ALLSELECT 1002, 1, 'About Us', 20021 UNION ALLSELECT 1002, 2, 'Address', 20022 UNION ALLSELECT 1002, 3, 'Employees', 20023INSERT INTO MyContentSELECT 300111, 'This is my Home Page Version 1' UNION ALLSELECT 300112, 'This is my Home Page Version 2' UNION ALLSELECT 300121, 'Welcome to my site Version 1' UNION ALLSELECT 300211, 'About Us Version 1' UNION ALLSELECT 300212, 'About Us Version 2' UNION ALLSELECT 300221, 'Address Version 1' UNION ALLSELECT 300231, 'Employees Version 1' UNION ALLSELECT 300232, 'Employees Version 1'INSERT INTO MyGroupSELECT 20011, 1, 300111 UNION ALLSELECT 20011, 2, 300112 UNION ALLSELECT 20012, 1, 300121 UNION ALLSELECT 20021, 1, 300211 UNION ALLSELECT 20021, 2, 300212 UNION ALLSELECT 20022, 1, 300221 UNION ALLSELECT 20023, 1, 300231 UNION ALLSELECT 20023, 2, 300232-- List ALL versions of Content for a given pageSELECT PageID, PageItem, GroupID, GroupVersion, ContentID, PageTitle, ContentTextFROM MyPage JOIN MyGroup ON GroupID = PageGroupID JOIN MyCOntent ON ContentID = GroupContentIDWHERE PageID = 1002 -- Use 1001 or 1002 as examplesORDER BY PageID, PageItem, GroupID, ContentID-- List just MOST RECENT version of each Page Item's ContentSELECT T_PageID, T_PageItem, T_GroupID, T_GroupVersion, PageID, PageItem, GroupID, GroupVersion, ContentID, PageTitle, ContentTextFROM ( SELECT [T_PageID] = PageID, [T_PageItem] = PageItem, [T_GroupID] = GroupID, [T_GroupVersion] = MAX(GroupVersion) FROM MyPage JOIN MyGroup ON GroupID = PageGroupID WHERE PageID = 1002 -- Use 1001 or 1002 as examples GROUP BY PageID, PageItem, GroupID ) T JOIN MyPage ON PageID = T_PageID AND PageItem = T_PageItem JOIN MyGroup ON GroupID = T_GroupID AND GroupVersion = T_GroupVersion JOIN MyCOntent ON ContentID = GroupContentIDORDER BY -- T_PageID, T_PageItem, T_GroupID, T_GroupVersion, PageID, PageItem, GroupID, ContentIDGODROP TABLE MyPageGODROP TABLE MyGroupGODROP TABLE MyContentGOSET NOCOUNT ON ThanksKristen |
|
|
hgorijal
Constraint Violating Yak Guru
277 Posts |
Posted - 2005-01-24 : 01:50:48
|
How about this..-- List just MOST RECENT version of each Page Item's ContentSELECT PageID, PageItem, GroupID, GroupVersion, ContentID, PageTitle, ContentTextFROM ( SELECT [T_GroupID] = GroupID, [T_GroupVersion] = MAX(GroupVersion) FROM MyGroup GROUP BY GroupID ) T JOIN MyPage ON PageGroupID = T_GroupID JOIN MyGroup ON GroupID = T_GroupID AND GroupVersion = T_GroupVersion JOIN MyCOntent ON ContentID = GroupContentIDWHERE PageID = 1002ORDER BY -- T_PageID, T_PageItem, T_GroupID, T_GroupVersion, PageID, PageItem, GroupID, ContentID or (even better) this...-- List just MOST RECENT version of each Page Item's ContentSELECT PageID, PageItem, GroupID, GroupVersion, ContentID, PageTitle, ContentTextFROM MyPage ,MyGroup ,MyCOntentWHERE PageID = 1002 AND GroupID = PageGroupID AND ContentID = GroupContentID and GroupVersion = (SELECT MAX(GroupVersion) FROM MyGroup where GroupID = MyPage.PageGroupID)ORDER BY -- T_PageID, T_PageItem, T_GroupID, T_GroupVersion, PageID, PageItem, GroupID, ContentID Hemanth GorijalaExchange a Dollar, we still have ONE each._______Exchange an Idea, we both have TWO each. |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-01-24 : 10:39:24
|
| I was going to suggest something very similar to hgorijala's "How about this.." I had guessed that the order (in performance from best to worst) would have been:1. How about this..2. Or (even better) this...3. -- List just MOST RECENT version of each Page Item's Contentbut after doing a very simple comparison in scan and read counts (by setting statistics and IO in current connection properties) it looks like the order is:1. Or (even better) this...2. How about this..3. -- List just MOST RECENT version of each Page Item's ContentI thought the correlated sub-query wouldn't have been as good as the simple derrived table but at least with this sample data (and no indexes) I was wrong.I don't know if you have the option, Kristen, but if you could alter the structure to include an IsCurrent bit column in MyGroup table that could be maintained by the ins, upd, del SPs for that table, that might be the best option of all because you wouldn't need a derrived table or a correlated subquery.tg |
 |
|
|
|
|
|