Bagikan melalui


Memigrasikan Kolom Terkomputasi

Kolom komputasi tidak didukung dalam tabel yang dioptimalkan memori. Namun, Anda dapat mensimulasikan kolom komputasi.

Anda harus mempertimbangkan kebutuhan untuk mempertahankan kolom komputasi saat memigrasikan tabel berbasis disk ke tabel yang dioptimalkan memori. Karakteristik performa dari tabel yang dioptimalkan untuk memori dan prosedur tersimpan yang dikompilasi secara asli mungkin dapat menghilangkan kebutuhan akan penyimpanan berkelanjutan.

Kolom Komputasi Yang Tidak Dipertahankan

Untuk mensimulasikan efek kolom komputasi yang tidak bertahan, buat tampilan pada tabel yang dioptimalkan memori. Dalam pernyataan SELECT yang menentukan tampilan, tambahkan definisi kolom komputasi ke dalam tampilan. Kecuali dalam prosedur tersimpan yang dikompilasi secara asli, kueri yang menggunakan nilai dari kolom komputasi harus dibaca dari tampilan. Di dalam prosedur tersimpan yang dikompilasi secara asli, Anda harus memperbarui pernyataan pilih, perbarui, atau hapus apa pun sesuai dengan definisi kolom komputasi Anda.

-- Schema for the table dbo.OrderDetails:  
-- OrderId int not null primary key,  
-- ProductId int not null,  
-- SalePrice money not null,  
-- Quantity int not null,  
-- Total money not null  
--  
-- Total is computed as SalePrice * Quantity and is not persisted.  
CREATE VIEW dbo.v_order_details AS  
   SELECT  
      OrderId,  
      ProductId,  
      SalePrice,  
      Quantity,  
      Quantity * SalePrice AS Total  
   FROM dbo.order_details  

Kolom Terhitung Tersimpan

Untuk mensimulasikan efek kolom komputasi yang dipertahankan, buat prosedur tersimpan untuk menyisipkan ke dalam tabel dan prosedur tersimpan lainnya untuk memperbarui tabel. Saat menyisipkan atau memperbarui tabel, panggil prosedur tersimpan ini untuk melakukan tugas-tugas ini. Di dalam prosedur tersimpan, hitung nilai untuk kolom yang dihitung sesuai dengan input, mirip dengan cara kolom komputasi didefinisikan pada tabel berbasis disk asli. Kemudian, sisipkan atau perbarui tabel seperti yang dibutuhkan di dalam prosedur yang tersimpan.

-- Schema for the table dbo.OrderDetails:  
-- OrderId int not null primary key,  
-- ProductId int not null,  
-- SalePrice money not null,  
-- Quantity int not null,  
-- Total money not null  
--  
-- Total is computed as SalePrice * Quantity and is persisted.  
-- we need to create insert and update procedures to calculate Total.  
CREATE PROCEDURE sp_insert_order_details   
@OrderId int, @ProductId int, @SalePrice money, @Quantity int  
WITH NATIVE_COMPILATION, SCHEMABINDING, EXECUTE AS OWNER  
AS BEGIN ATOMIC WITH (LANGUAGE = N'english', TRANSACTION ISOLATION LEVEL = SNAPSHOT)  
-- compute the value here.   
-- this stored procedure works with single rows only.  
-- for bulk inserts, accept a table-valued parameter into the stored procedure  
-- and use an INSERT INTO SELECT statement.  
DECLARE @total money = @SalePrice * @Quantity  
INSERT INTO dbo.OrderDetails (OrderId, ProductId, SalePrice, Quantity, Total)  
VALUES (@OrderId, @ProductId, @SalePrice, @Quantity, @total)  
END  
GO  
  
CREATE PROCEDURE sp_update_order_details_by_id  
@OrderId int, @ProductId int, @SalePrice money, @Quantity int  
WITH NATIVE_COMPILATION, SCHEMABINDING, EXECUTE AS OWNER  
AS BEGIN ATOMIC WITH (LANGUAGE = N'english', TRANSACTION ISOLATION LEVEL = SNAPSHOT)  
-- compute the value here.   
-- this stored procedure works with single rows only.  
DECLARE @total money = @SalePrice * @Quantity  
UPDATE dbo.OrderDetails   
SET ProductId = @ProductId, SalePrice = @SalePrice, Quantity = @Quantity, Total = @total  
WHERE OrderId = @OrderId  
END  
GO  

Lihat Juga

Migrasi ke Dalam Memori OLTP