mirror of
https://github.com/Microsoft/sql-server-samples.git
synced 2025-12-08 14:58:54 +00:00
1237 lines
51 KiB
Plaintext
1237 lines
51 KiB
Plaintext
{
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"name": "SQL",
|
|
"display_name": "SQL",
|
|
"language": "sql"
|
|
},
|
|
"language_info": {
|
|
"name": "sql",
|
|
"version": ""
|
|
}
|
|
},
|
|
"nbformat_minor": 2,
|
|
"nbformat": 4,
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [
|
|
"# Accelerated Database Recovery - Fast rollback and log truncation\r\n",
|
|
"In this example you will see how fast rollback can now be with Accelerated Database Recovery. You will also see that a long active transaction does not affect the ability to truncate the transaction log.\r\n",
|
|
""
|
|
],
|
|
"metadata": {
|
|
"azdata_cell_guid": "636c7ed1-c819-4f37-9386-899d732263a2"
|
|
}
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [
|
|
"## Step 1: Setup the database\r\n",
|
|
"This demo uses the WideWorldImportersDW database. You can get the WideWorldImportersDW-Full.bak database backup file from https://github.com/Microsoft/sql-server-samples/releases/tag/wide-world-importers-v1.0.\r\n",
|
|
"\r\n",
|
|
"The log file is enlarged to 4GB to avoid any autogrow.\r\n",
|
|
"\r\n",
|
|
"Leave the database SIMPLE recovery to make it easier to analyze log truncation.\r\n",
|
|
"\r\n",
|
|
"The database assumes a path for a standard SQL Server Windows installation.\r\n",
|
|
"\r\n",
|
|
"**Note**: *For Linux installations the default path to use is /var/opt/mssql*"
|
|
],
|
|
"metadata": {
|
|
"azdata_cell_guid": "5e1d8226-6420-4bde-aa30-f33b5e7646a5"
|
|
}
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": [
|
|
"USE [master]\r\n",
|
|
"GO\r\n",
|
|
"IF EXISTS (SELECT [database_id] FROM sys.databases WHERE [name] = 'WideWorldImportersDW')\r\n",
|
|
"ALTER DATABASE [WideWorldImportersDW] SET SINGLE_USER WITH ROLLBACK IMMEDIATE\r\n",
|
|
"GO\r\n",
|
|
"\r\n",
|
|
"DECLARE @datafilepath VARCHAR(8000) = CAST(SERVERPROPERTY('InstanceDefaultDataPath') AS VARCHAR(4000)) + 'WideWorldImportersDW.mdf'\r\n",
|
|
"DECLARE @logfilepath VARCHAR(8000) = CAST(SERVERPROPERTY('InstanceDefaultLogPath') AS VARCHAR(4000)) + 'WideWorldImportersDW.ldf'\r\n",
|
|
"DECLARE @inmemfilepath VARCHAR(8000) = CAST(SERVERPROPERTY('InstanceDefaultDataPath') AS VARCHAR(4000)) + 'WideWorldImportersDW_InMemory_Data_1'\r\n",
|
|
"DECLARE @secondaryfilepath VARCHAR(8000) = CAST(SERVERPROPERTY('InstanceDefaultDataPath') AS VARCHAR(4000))+ 'WideWorldImportersDW_2.ndf'\r\n",
|
|
"\r\n",
|
|
"-- Change @backupfile file path as needed\r\n",
|
|
"DECLARE @backupfile VARCHAR(8000) = 'E:\\SampleDBs\\WideWorldImportersDW-Full.bak'\r\n",
|
|
"RESTORE DATABASE WideWorldImportersDW\r\n",
|
|
"FROM DISK = @backupfile \r\n",
|
|
"WITH MOVE 'WWI_Primary' TO @datafilepath,\r\n",
|
|
" MOVE 'WWI_UserData' TO @secondaryfilepath,\r\n",
|
|
" MOVE 'WWIDW_InMemory_Data_1' TO @inmemfilepath,\r\n",
|
|
" MOVE 'WWI_Log' TO @logfilepath, NOUNLOAD, REPLACE, STATS = 10\r\n",
|
|
"GO\r\n",
|
|
"\r\n",
|
|
"USE [master]\r\n",
|
|
"GO\r\n",
|
|
"ALTER DATABASE [WideWorldImportersDW] MODIFY FILE ( NAME = N'WWI_Log', SIZE = 4GB )\r\n",
|
|
"GO"
|
|
],
|
|
"metadata": {
|
|
"azdata_cell_guid": "e87d443b-4258-48db-ad65-6d6fbcb494d7",
|
|
"tags": []
|
|
},
|
|
"outputs": [
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Commands completed successfully."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Commands completed successfully."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "10 percent processed."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Processed 1192 pages for database 'WideWorldImportersDW', file 'WWI_Primary' on file 1."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Processed 28184 pages for database 'WideWorldImportersDW', file 'WWI_UserData' on file 1."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Processed 26 pages for database 'WideWorldImportersDW', file 'WWI_Log' on file 1."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Processed 24 pages for database 'WideWorldImportersDW', file 'WWIDW_InMemory_Data_1' on file 1."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Converting database 'WideWorldImportersDW' from version 852 to the current version 904."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 852 to version 853."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 853 to version 854."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 854 to version 855."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 855 to version 856."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 856 to version 857."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 857 to version 858."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 858 to version 859."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 859 to version 860."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 860 to version 861."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 861 to version 862."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 862 to version 863."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 863 to version 864."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 864 to version 865."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 865 to version 866."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 866 to version 867."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 867 to version 868."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 868 to version 869."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 869 to version 875."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 875 to version 876."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 876 to version 877."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 877 to version 878."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 878 to version 879."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 879 to version 880."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 880 to version 881."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 881 to version 882."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 882 to version 883."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 883 to version 884."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 884 to version 885."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 885 to version 886."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 886 to version 887."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 887 to version 888."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 888 to version 889."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 889 to version 890."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 890 to version 891."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 891 to version 892."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 892 to version 893."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 893 to version 894."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 894 to version 895."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 895 to version 896."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 896 to version 897."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 897 to version 898."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 898 to version 899."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 899 to version 900."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 900 to version 901."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 901 to version 902."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 902 to version 903."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Database 'WideWorldImportersDW' running the upgrade step from version 903 to version 904."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "100 percent processed."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "RESTORE DATABASE successfully processed 29426 pages in 4.474 seconds (51.382 MB/sec)."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Commands completed successfully."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Commands completed successfully."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Total execution time: 00:01:07.918"
|
|
},
|
|
"metadata": {}
|
|
}
|
|
],
|
|
"execution_count": 35
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [
|
|
"## Step 2: Create the table\r\n",
|
|
"\r\n",
|
|
"Create a table with 3.7 million rows to be able to show the different in log growth and rollback times.\r\n",
|
|
"\r\n",
|
|
"Depending on the speed of your server, enlarging the database and data could take several minutes."
|
|
],
|
|
"metadata": {
|
|
"azdata_cell_guid": "544412ea-e909-4325-a636-d53e27e25d35"
|
|
}
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": [
|
|
"IF DB_NAME() != 'WideWorldImportersDW' \r\n",
|
|
"USE WideWorldImportersDW\r\n",
|
|
"SET NOCOUNT ON\r\n",
|
|
"GO\r\n",
|
|
"\r\n",
|
|
"IF EXISTS (SELECT [object_id] FROM sys.objects (NOLOCK) WHERE [object_id] = OBJECT_ID(N'[Fact].[OrderHistory]') AND [type] IN (N'U'))\r\n",
|
|
"DROP TABLE [Fact].[OrderHistory];\r\n",
|
|
"GO\r\n",
|
|
"\r\n",
|
|
"IF NOT EXISTS (SELECT [object_id] FROM sys.objects (NOLOCK) WHERE [object_id] = OBJECT_ID(N'[Fact].[OrderHistory]') AND [type] IN (N'U'))\r\n",
|
|
"BEGIN\r\n",
|
|
" SELECT [Order Key], [City Key], [Customer Key], [Stock Item Key], [Order Date Key], [Picked Date Key], [Salesperson Key], [Picker Key], [WWI Order ID], [WWI Backorder ID], [Description], Package, Quantity, [Unit Price], [Tax Rate], [Total Excluding Tax], [Tax Amount], [Total Including Tax], [Lineage Key]\r\n",
|
|
" INTO [Fact].[OrderHistory]\r\n",
|
|
" FROM [Fact].[Order];\r\n",
|
|
"END;\r\n",
|
|
"\r\n",
|
|
"ALTER TABLE [Fact].[OrderHistory]\r\n",
|
|
"ADD CONSTRAINT PK_Fact_OrderHistory PRIMARY KEY CLUSTERED([Order Key] ASC, [Order Date Key] ASC) WITH (DATA_COMPRESSION = PAGE);\r\n",
|
|
"GO\r\n",
|
|
"\r\n",
|
|
"-- Enlarge Table\r\n",
|
|
"IF (SELECT COUNT(*) FROM [Fact].[OrderHistory]) < 3702592\r\n",
|
|
"BEGIN\r\n",
|
|
"\tDECLARE @i smallint\r\n",
|
|
"\tSET @i = 0\r\n",
|
|
"\tWHILE @i < 4\r\n",
|
|
"\tBEGIN\r\n",
|
|
"\t\tINSERT INTO [Fact].[OrderHistory] ([City Key], [Customer Key], [Stock Item Key], [Order Date Key], [Picked Date Key], [Salesperson Key], [Picker Key], [WWI Order ID], [WWI Backorder ID], Description, Package, Quantity, [Unit Price], [Tax Rate], [Total Excluding Tax], [Tax Amount], [Total Including Tax], [Lineage Key])\r\n",
|
|
"\t\tSELECT [City Key], [Customer Key], [Stock Item Key], [Order Date Key], [Picked Date Key], [Salesperson Key], [Picker Key], [WWI Order ID], [WWI Backorder ID], Description, Package, Quantity, [Unit Price], [Tax Rate], [Total Excluding Tax], [Tax Amount], [Total Including Tax], [Lineage Key]\r\n",
|
|
"\t\tFROM [Fact].[OrderHistory];\r\n",
|
|
"\r\n",
|
|
"\t\tSET @i = @i +1\r\n",
|
|
"\tEND;\r\n",
|
|
"END\r\n",
|
|
"GO"
|
|
],
|
|
"metadata": {
|
|
"azdata_cell_guid": "e2d4ef34-6de4-47da-880f-e81a346b08a0"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Commands completed successfully."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Commands completed successfully."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Commands completed successfully."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Commands completed successfully."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Total execution time: 00:01:25.068"
|
|
},
|
|
"metadata": {}
|
|
}
|
|
],
|
|
"execution_count": 36
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [
|
|
"## Step 2: Delete all the rows in the table\r\n",
|
|
"Delete all the rows in the table in a user defined transaction"
|
|
],
|
|
"metadata": {
|
|
"azdata_cell_guid": "0289f187-20b1-412e-bff8-e216cd6a9b24"
|
|
}
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": [
|
|
"USE master\r\n",
|
|
"GO\r\n",
|
|
"ALTER DATABASE WideWorldImportersDW SET ACCELERATED_DATABASE_RECOVERY = OFF\r\n",
|
|
"GO\r\n",
|
|
"\r\n",
|
|
"USE WideWorldImportersDW\r\n",
|
|
"GO\r\n",
|
|
"BEGIN TRAN\r\n",
|
|
"DELETE FROM [Fact].[OrderHistory]\r\n",
|
|
"GO"
|
|
],
|
|
"metadata": {
|
|
"azdata_cell_guid": "a42899e6-ac52-4b60-9188-2f2bb61b06ea"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Commands completed successfully."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Commands completed successfully."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Commands completed successfully."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Commands completed successfully."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Total execution time: 00:00:29.702"
|
|
},
|
|
"metadata": {}
|
|
}
|
|
],
|
|
"execution_count": 37
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [
|
|
"## Step 3: Check how much transaction log space is used\r\n",
|
|
"Because there is an active transaction the transaction log will have space used that cannot be truncated"
|
|
],
|
|
"metadata": {
|
|
"azdata_cell_guid": "d0996d47-5537-435f-b44d-8428ad76e152"
|
|
}
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": [
|
|
"SELECT * FROM sys.dm_db_log_space_usage\r\n",
|
|
"GO"
|
|
],
|
|
"metadata": {
|
|
"azdata_cell_guid": "4bfcc990-7cf3-4b45-820e-4d95c5f3fb7f"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Commands completed successfully."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Total execution time: 00:00:00.012"
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "execute_result",
|
|
"execution_count": 38,
|
|
"data": {
|
|
"application/vnd.dataresource+json": {
|
|
"schema": {
|
|
"fields": [
|
|
{
|
|
"name": "database_id"
|
|
},
|
|
{
|
|
"name": "total_log_size_in_bytes"
|
|
},
|
|
{
|
|
"name": "used_log_space_in_bytes"
|
|
},
|
|
{
|
|
"name": "used_log_space_in_percent"
|
|
},
|
|
{
|
|
"name": "log_space_in_bytes_since_last_backup"
|
|
}
|
|
]
|
|
},
|
|
"data": [
|
|
{
|
|
"0": "8",
|
|
"1": "4294959104",
|
|
"2": "2810818560",
|
|
"3": "65.4446",
|
|
"4": "825794560"
|
|
}
|
|
]
|
|
},
|
|
"text/html": "<table><tr><th>database_id</th><th>total_log_size_in_bytes</th><th>used_log_space_in_bytes</th><th>used_log_space_in_percent</th><th>log_space_in_bytes_since_last_backup</th></tr><tr><td>8</td><td>4294959104</td><td>2810818560</td><td>65.4446</td><td>825794560</td></tr></table>"
|
|
},
|
|
"metadata": {}
|
|
}
|
|
],
|
|
"execution_count": 38
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [
|
|
"## Step 4: Does a checkpoint truncate the transaction log?\r\n",
|
|
"Compare the used_log_space_in_percent to the value from the previous step. Normally for a database with simple recovery a CHECKPOINT would truncate the log."
|
|
],
|
|
"metadata": {
|
|
"azdata_cell_guid": "6a8f88bd-96e6-457c-8ed5-9af54c239a1b"
|
|
}
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": [
|
|
"CHECKPOINT\r\n",
|
|
"GO\r\n",
|
|
"SELECT * FROM sys.dm_db_log_space_usage\r\n",
|
|
"GO"
|
|
],
|
|
"metadata": {
|
|
"azdata_cell_guid": "c6fd6403-773f-4332-94ae-85988d587c65"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Commands completed successfully."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Commands completed successfully."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Total execution time: 00:00:03.760"
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "execute_result",
|
|
"execution_count": 39,
|
|
"data": {
|
|
"application/vnd.dataresource+json": {
|
|
"schema": {
|
|
"fields": [
|
|
{
|
|
"name": "database_id"
|
|
},
|
|
{
|
|
"name": "total_log_size_in_bytes"
|
|
},
|
|
{
|
|
"name": "used_log_space_in_bytes"
|
|
},
|
|
{
|
|
"name": "used_log_space_in_percent"
|
|
},
|
|
{
|
|
"name": "log_space_in_bytes_since_last_backup"
|
|
}
|
|
]
|
|
},
|
|
"data": [
|
|
{
|
|
"0": "8",
|
|
"1": "4294959104",
|
|
"2": "2810855424",
|
|
"3": "65.44545",
|
|
"4": "825262080"
|
|
}
|
|
]
|
|
},
|
|
"text/html": "<table><tr><th>database_id</th><th>total_log_size_in_bytes</th><th>used_log_space_in_bytes</th><th>used_log_space_in_percent</th><th>log_space_in_bytes_since_last_backup</th></tr><tr><td>8</td><td>4294959104</td><td>2810855424</td><td>65.44545</td><td>825262080</td></tr></table>"
|
|
},
|
|
"metadata": {}
|
|
}
|
|
],
|
|
"execution_count": 39
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [
|
|
"## Step 5: How long does it take to rollback all the deletes\r\n",
|
|
"Try to rollback the transation and measure the time it takes"
|
|
],
|
|
"metadata": {
|
|
"azdata_cell_guid": "dd4e57e3-ab71-4a1f-9d0e-1e339b922e07"
|
|
}
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": [
|
|
"ROLLBACK TRAN\r\n",
|
|
"GO"
|
|
],
|
|
"metadata": {
|
|
"azdata_cell_guid": "8cd860d5-7dba-4964-ac67-1a63c5e2381f"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Commands completed successfully."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Total execution time: 00:01:23.086"
|
|
},
|
|
"metadata": {}
|
|
}
|
|
],
|
|
"execution_count": 40
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [
|
|
"## Step 6: What is the log space usage after a CHECKPOINT\r\n",
|
|
"Because the active transaction is rolled back, a CHECKPOINT will allow the log to be truncated."
|
|
],
|
|
"metadata": {
|
|
"azdata_cell_guid": "56c64247-7bf9-44ce-8595-204109476cde"
|
|
}
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": [
|
|
"CHECKPOINT\r\n",
|
|
"GO\r\n",
|
|
"SELECT * FROM sys.dm_db_log_space_usage\r\n",
|
|
"GO"
|
|
],
|
|
"metadata": {
|
|
"azdata_cell_guid": "29104ac5-22b8-4da4-a660-56d0b99042e3"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Commands completed successfully."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Commands completed successfully."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Total execution time: 00:00:01.956"
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "execute_result",
|
|
"execution_count": 41,
|
|
"data": {
|
|
"application/vnd.dataresource+json": {
|
|
"schema": {
|
|
"fields": [
|
|
{
|
|
"name": "database_id"
|
|
},
|
|
{
|
|
"name": "total_log_size_in_bytes"
|
|
},
|
|
{
|
|
"name": "used_log_space_in_bytes"
|
|
},
|
|
{
|
|
"name": "used_log_space_in_percent"
|
|
},
|
|
{
|
|
"name": "log_space_in_bytes_since_last_backup"
|
|
}
|
|
]
|
|
},
|
|
"data": [
|
|
{
|
|
"0": "8",
|
|
"1": "4294959104",
|
|
"2": "981958656",
|
|
"3": "22.86305",
|
|
"4": "783413248"
|
|
}
|
|
]
|
|
},
|
|
"text/html": "<table><tr><th>database_id</th><th>total_log_size_in_bytes</th><th>used_log_space_in_bytes</th><th>used_log_space_in_percent</th><th>log_space_in_bytes_since_last_backup</th></tr><tr><td>8</td><td>4294959104</td><td>981958656</td><td>22.86305</td><td>783413248</td></tr></table>"
|
|
},
|
|
"metadata": {}
|
|
}
|
|
],
|
|
"execution_count": 41
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [
|
|
"## Step 7: Turn on Accelerated Database Recovery\r\n",
|
|
"\r\n",
|
|
"**Note**: If you prefer to enable ADR in the existing database file in the PRIMARY filegroup, just execute the last `ALTER DATABASE` without the `( PERSISTENT_VERSION_STORE_FILEGROUP = [PVS])` statement."
|
|
],
|
|
"metadata": {
|
|
"azdata_cell_guid": "6dd6df07-c8a8-4af5-a37c-e76387891562"
|
|
}
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": [
|
|
"USE master\r\n",
|
|
"GO\r\n",
|
|
"-- Add a new filegroup specific for the Persistent Version Store (optional)\r\n",
|
|
"ALTER DATABASE [WideWorldImportersDW] ADD FILEGROUP [PVS]\r\n",
|
|
"GO\r\n",
|
|
"\r\n",
|
|
"DECLARE @adrdatafilepath VARCHAR(8000) = CAST(SERVERPROPERTY('InstanceDefaultDataPath') AS VARCHAR(4000)) + 'ADR.mdf'\r\n",
|
|
"DECLARE @sqlcmd VARCHAR(8000) = 'ALTER DATABASE [WideWorldImportersDW] ADD FILE ( NAME = N''ADR'', FILENAME = ''' + @adrdatafilepath + ''', SIZE = 1GB , FILEGROWTH = 65536KB ) TO FILEGROUP [PVS]'\r\n",
|
|
"EXEC (@sqlcmd)\r\n",
|
|
"GO\r\n",
|
|
"\r\n",
|
|
"-- Enable ADR\r\n",
|
|
"ALTER DATABASE WideWorldImportersDW SET ACCELERATED_DATABASE_RECOVERY = ON ( PERSISTENT_VERSION_STORE_FILEGROUP = [PVS])\r\n",
|
|
"GO"
|
|
],
|
|
"metadata": {
|
|
"azdata_cell_guid": "ecea5d73-8c88-40e0-92d1-cdc28ff6506a"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Commands completed successfully."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Commands completed successfully."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Commands completed successfully."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Commands completed successfully."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Total execution time: 00:00:00.246"
|
|
},
|
|
"metadata": {}
|
|
}
|
|
],
|
|
"execution_count": 42
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [
|
|
"## Step 8: Try to delete all the rows again under a transaction"
|
|
],
|
|
"metadata": {
|
|
"azdata_cell_guid": "32126f0d-33de-4c96-b2fb-64249ed5d6f6"
|
|
}
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": [
|
|
"USE WideWorldImportersDW\r\n",
|
|
"GO\r\n",
|
|
"BEGIN TRAN\r\n",
|
|
"DELETE FROM [Fact].[OrderHistory]\r\n",
|
|
"GO"
|
|
],
|
|
"metadata": {
|
|
"azdata_cell_guid": "8c611ea2-27aa-429f-813e-35def0fc1b25"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Commands completed successfully."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Commands completed successfully."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Total execution time: 00:01:02.286"
|
|
},
|
|
"metadata": {}
|
|
}
|
|
],
|
|
"execution_count": 43
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [
|
|
"## Step 9: Check log space usage before and after a CHECKPOINT\r\n",
|
|
"Notice the space used for the log is lower even before the CHECKPOINT. This is because an active transaction does not affect log truncation when using Accelerated Database Recovery."
|
|
],
|
|
"metadata": {
|
|
"azdata_cell_guid": "b5563198-be8d-4ded-b79d-d675a1bfbf77"
|
|
}
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": [
|
|
"SELECT * FROM sys.dm_db_log_space_usage\r\n",
|
|
"GO\r\n",
|
|
"CHECKPOINT\r\n",
|
|
"GO\r\n",
|
|
"SELECT * FROM sys.dm_db_log_space_usage\r\n",
|
|
"GO"
|
|
],
|
|
"metadata": {
|
|
"azdata_cell_guid": "f1817c17-8b5a-4f47-b534-8120244a95ce"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Commands completed successfully."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "execute_result",
|
|
"execution_count": 44,
|
|
"data": {
|
|
"application/vnd.dataresource+json": {
|
|
"schema": {
|
|
"fields": [
|
|
{
|
|
"name": "database_id"
|
|
},
|
|
{
|
|
"name": "total_log_size_in_bytes"
|
|
},
|
|
{
|
|
"name": "used_log_space_in_bytes"
|
|
},
|
|
{
|
|
"name": "used_log_space_in_percent"
|
|
},
|
|
{
|
|
"name": "log_space_in_bytes_since_last_backup"
|
|
}
|
|
]
|
|
},
|
|
"data": [
|
|
{
|
|
"0": "8",
|
|
"1": "4294959104",
|
|
"2": "2085478400",
|
|
"3": "48.55642",
|
|
"4": "1794609152"
|
|
}
|
|
]
|
|
},
|
|
"text/html": "<table><tr><th>database_id</th><th>total_log_size_in_bytes</th><th>used_log_space_in_bytes</th><th>used_log_space_in_percent</th><th>log_space_in_bytes_since_last_backup</th></tr><tr><td>8</td><td>4294959104</td><td>2085478400</td><td>48.55642</td><td>1794609152</td></tr></table>"
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Commands completed successfully."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Commands completed successfully."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Total execution time: 00:00:03.640"
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "execute_result",
|
|
"execution_count": 44,
|
|
"data": {
|
|
"application/vnd.dataresource+json": {
|
|
"schema": {
|
|
"fields": [
|
|
{
|
|
"name": "database_id"
|
|
},
|
|
{
|
|
"name": "total_log_size_in_bytes"
|
|
},
|
|
{
|
|
"name": "used_log_space_in_bytes"
|
|
},
|
|
{
|
|
"name": "used_log_space_in_percent"
|
|
},
|
|
{
|
|
"name": "log_space_in_bytes_since_last_backup"
|
|
}
|
|
]
|
|
},
|
|
"data": [
|
|
{
|
|
"0": "8",
|
|
"1": "4294959104",
|
|
"2": "1299976192",
|
|
"3": "30.26749",
|
|
"4": "1011310592"
|
|
}
|
|
]
|
|
},
|
|
"text/html": "<table><tr><th>database_id</th><th>total_log_size_in_bytes</th><th>used_log_space_in_bytes</th><th>used_log_space_in_percent</th><th>log_space_in_bytes_since_last_backup</th></tr><tr><td>8</td><td>4294959104</td><td>1299976192</td><td>30.26749</td><td>1011310592</td></tr></table>"
|
|
},
|
|
"metadata": {}
|
|
}
|
|
],
|
|
"execution_count": 44
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [
|
|
"## Step 10: How fast is a rollback?\r\n",
|
|
"See if a rollback is any faster with Accelerated Database Recovery"
|
|
],
|
|
"metadata": {
|
|
"azdata_cell_guid": "90c4d3dd-2573-43ed-8ead-1cc25fd35d10"
|
|
}
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": [
|
|
"ROLLBACK TRAN\r\n",
|
|
"GO"
|
|
],
|
|
"metadata": {
|
|
"azdata_cell_guid": "9f51686e-3266-4f8b-97e0-cae4f8b1107f"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Commands completed successfully."
|
|
},
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"text/html": "Total execution time: 00:00:00.021"
|
|
},
|
|
"metadata": {}
|
|
}
|
|
],
|
|
"execution_count": 45
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [
|
|
"It is noticeably faster: 21ms with ADR vs 1m 23s without ADR"
|
|
],
|
|
"metadata": {
|
|
"azdata_cell_guid": "07899843-f9bc-4a76-a0fb-a0cbc40f8f85"
|
|
}
|
|
}
|
|
]
|
|
} |