Question

I have created a schema design but not sure whether this is resulting in an optimized performance. The commissions and commmissions table could potentially be very large. What would be a common pattern for a database schema where the a historytable is kept for a table that would be filling up.

enter image description here

Also looking at this query where would I have to create indexes if any needed?

select
       d.name             AS [Dealer Name]
      ,c.commissionamount AS [Commission Amount]
      ,c.createddate      AS [Commission Created Date] 
      ,p.name             AS [Product Name]
      ,stuff((SELECT ', ' + pcci.Name
              FROM ProductCategory pcci
              join ProductCategoryMapping pcmi 
              on pcmi.ProductCategoryId=pcci.ProductCategoryId
              join Product p1i 
              on p1i.ProductId=pcmi.ProductId
              WHERE p1i.ProductId = p.productid
              FOR XML PATH(''),TYPE).value('.','NVARCHAR(MAX)'),1,2,'')
                           AS [CSV]

from commission c join dealer d 
on d.dealerid=c.dealerid
join product p 
on c.productid=p.productid;

I also created a sqlfiddle for this:http://sqlfiddle.com/#!6/d574e/38

Was it helpful?

Solution

What would be a common pattern for a database schema where the a history table is kept for a table that would be filling up?

The reason for creating a history table is that you want to separate the current (operational) data from the historical (query) data.

Generally the history table looks similar to the current table. Any foreign keys in the current table are converted to the actual values in the history table, especially if those values are likely to change over time. The history table doesn't have to be normalized, since you're only selecting and not inserting, updating, or deleting.

A history table generally will have multiple indexes, to facilitate querying by different criteria. A history table is essentially a mini data warehouse.

OTHER TIPS

You can simplify the subquery by removing the extra join to product:

  ,stuff((SELECT ', ' + pcci.Name
          FROM ProductCategory pcci join
               ProductCategoryMapping pcmi 
               on pcmi.ProductCategoryId = pcci.ProductCategoryId
          WHERE pcmi.ProductId = p.productid
          FOR XML PATH(''),TYPE).value('.','NVARCHAR(MAX)'),1,2,'')
                       AS [CSV]

I assume that the primary keys are all indexed, so other than this simplification, no other indexes come to mind.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top