Memigrasikan Kolom Terkomputasi

Berlaku untuk: SQL ServerAzure SQL DatabaseAzure SQL Managed Instance

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

Dimulai dengan SQL Server 2017 (14.x), kolom komputasi didukung dalam tabel dan indeks yang dioptimalkan memori.

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

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 Komputasi Yang Dipertahankan

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 bidang komputasi sesuai dengan input, seperti bagaimana kolom komputasi ditentukan pada tabel berbasis disk asli. Kemudian, sisipkan atau perbarui tabel sesuai kebutuhan di dalam prosedur 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 OLTP Dalam Memori