indexing – Microsoft SQLServer

anggaplah ada sebuah table view dengan script seperti ini

create view[dbo].[Total_FactInternetSales]
as 
select    sum(DiscountAmount) as Total_DiscountAmount,
        sum(ProductStandardCost) as Total_ProductStandartCost,
        sum(TotalProductCost) as Total_TotalProductCost,
        sum(SalesAmount) as Total_SalesAmount,
        OrderDate,
        CustomerKey,
        CurrencyKey
from dbo.FactInternetSales
group by OrderDate, CustomerKey, CurrencyKey

anggaplah kita memiliki 7 column, dan pada beberapa column pembentuk memiliki nilai tetap, kita dapat melakukan indexing pada column-column tersebut agar mendapatkan performa yang lebih baik

ubah view yang telah ada, dan untuk menjaga integritas column antar table pembentuk view kita gunakan SCHEMABINDING, bisa juga menambahkan colum perhitungan lain (ex: RecordCount)

ALTER VIEW    [dbo].[Total_FactInternetSales]
WITH SCHEMABINDING
AS
SELECT sum(DiscountAmount) as Total_DiscountAmount,
        sum(ProductStandardCost) as Total_ProductStandartCost,
        sum(TotalProductCost) as Total_TotalProductCost,
        sum(SalesAmount) as Total_SalesAmount,
        OrderDate,
        CustomerKey,
        CurrencyKey,
        COUNT_BIG(*) AS RecordCount
from dbo.FactInternetSales
group by OrderDate, CustomerKey, CurrencyKey

tambahkan indexing

CREATE UNIQUE CLUSTERED INDEX [IX_Total_FactInternetSales]
ON [dbo].[Total_FactInternetSales] (OrderDate, CustomerKey, CurrencyKey )

Published by

G3n1k

just to remember what i had known :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s