{CloudFront}Getting started with a simple CloudFront distribution

https://blog.serverworks.co.jp/2021/02/05/151548

https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/GettingStarted.SimpleDistribution.html

 

-- 1. コマンド等のインストール

-- 1.1 aws cli version 2 インストール

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
aws --version

 

-- 1.2 jqインストール
sudo yum -y install jq


-- 2. S3 バケットを作成する

aws s3 ls

aws s3 mb s3://bucket123

-- 3. 静的ウェブサイトホスティングの有効化

vim a.json

{
    "IndexDocument": {
        "Suffix": "index.html"
    },
    "ErrorDocument": {
        "Key": "index.html"
    }
}

aws s3api put-bucket-website \
--bucket bucket123 \
--website-configuration file://a.json

aws s3api get-bucket-website \
--bucket bucket123

 


-- 4. パブリックアクセスブロック設定の編集

-- 4.1 アカウントレベル
aws s3control put-public-access-block \
--account-id 999999999999 \
--public-access-block-configuration "BlockPublicAcls=false,IgnorePublicAcls=false,BlockPublicPolicy=false,RestrictPublicBuckets=false"

aws s3control get-public-access-block \
--account-id 999999999999

-- 4.2 バケットレベル
aws s3api put-public-access-block \
--bucket bucket123 \
--public-access-block-configuration "BlockPublicAcls=false,IgnorePublicAcls=false,BlockPublicPolicy=false,RestrictPublicBuckets=false"

aws s3api get-public-access-block \
--bucket bucket123


-- 5. バケットポリシーの設定

vim b.json

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "PublicReadGetObject",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::bucket123/*",
            "Condition": {
                "IpAddress": {
                    "aws:SourceIp": "0.0.0.0/0"
                }
            }
        }
    ]
}

 

aws s3api put-bucket-policy \
--bucket bucket123 \
--policy file://b.json


aws s3api get-bucket-policy \
--bucket bucket123


-- 6. インデックスドキュメントの設定

vim index.html

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>My Website Home Page</title>
</head>
<body>
  <h1>Welcome to my website</h1>
  <p>Now hosted on Amazon S3!</p>
</body>
</html>

aws s3api put-object --bucket bucket123 --key index.html --body index.html --content-type text/html

 

 

-- 7. ディストリビューションの作成


aws cloudfront create-distribution \
--origin-domain-name bucket123.s3.ap-northeast-1.amazonaws.com \
--default-root-object index.html

 

aws cloudfront list-distributions

aws cloudfront get-distribution \
--id AAAAAAAAAAAAA

aws cloudfront get-distribution-config \
--id AAAAAAAAAAAAA

 

-- 8. 動作確認

http://bucket123.s3-website-ap-northeast-1.amazonaws.com/index.html

curl -v -X GET http://bucket123.s3-website-ap-northeast-1.amazonaws.com/index.html


http://xxxxxxxxxxxxx.cloudfront.net/index.html

curl -v -X GET http://xxxxxxxxxxxxx.cloudfront.net/index.html

 


-- 9. クリーンアップ

-- ディストリビューションの無効化

aws cloudfront get-distribution \
--id AAAAAAAAAAAAA

aws cloudfront get-distribution-config \
--id AAAAAAAAAAAAA


※ distribution.jsonはget-distribution-configコマンドのDistributionConfigから取得し、Enabledをfalseに変更する

aws cloudfront get-distribution-config \
--id AAAAAAAAAAAAA | jq -r .DistributionConfig > distribution.json

sed -i 's/Enabled": true/Enabled": false/' distribution.json

 

aws cloudfront get-distribution-config \
--id AAAAAAAAAAAAA | jq -r .ETag

aws cloudfront update-distribution \
--id AAAAAAAAAAAAA \
--if-match BBBBBBBBBBBBBB \
--distribution-config file://distribution.json


※ if-matchにはETagの値をセット

無効化されるまで待つ


-- ディストリビューションの削除

aws cloudfront get-distribution \
--id AAAAAAAAAAAAA

aws cloudfront get-distribution-config \
--id AAAAAAAAAAAAA

aws cloudfront get-distribution-config \
--id AAAAAAAAAAAAA | jq -r .ETag

aws cloudfront delete-distribution \
--id AAAAAAAAAAAAA \
--if-match CCCCCCCCCCCCCC

 

aws cloudfront list-distributions

 


-- バケットの削除
aws s3 ls

aws s3 rb s3://bucket123 --force

-- アカウントレベルのパブリックアクセスブロックの有効化

aws s3control put-public-access-block \
--account-id 999999999999 \
--public-access-block-configuration "BlockPublicAcls=true,IgnorePublicAcls=true,BlockPublicPolicy=true,RestrictPublicBuckets=true"

aws s3control get-public-access-block \
--account-id 999999999999

 

 

{ServiceCatalog}ご利用スタートにあたって

https://www.yamamanx.com/aws-service-catalog-tutorial/

https://docs.aws.amazon.com/servicecatalog/latest/adminguide/getstarted.html

 

前提:
エンドユーザとしてコンソール権限を有するtestuserを作成し、AWSServiceCatalogEndUserFullAccess 付与済み


-- 1. コマンド等のインストール

-- 1.1 aws cli version 2 インストール

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
aws --version

-- 1.2 jqインストール
sudo yum -y install jq

-- 2. IAMポリシー作成
vim policy01.json

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "cloudformation:CreateStack",
                "cloudformation:DeleteStack",
                "cloudformation:DescribeStackEvents",
                "cloudformation:DescribeStacks",
                "cloudformation:GetTemplateSummary",
                "cloudformation:SetStackPolicy",
                "cloudformation:ValidateTemplate",
                "cloudformation:UpdateStack",
                "ec2:*",
                "s3:GetObject",
                "servicecatalog:*",
                "sns:*"
            ],
            "Resource": "*"
        }
    ]
}

aws iam create-policy \
--policy-name policy01 \
--policy-document file://policy01.json

-- 3. IAMロール作成
vim role01.json

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "Service": "servicecatalog.amazonaws.com"
            },
            "Action": "sts:AssumeRole"
        }
    ]
}


aws iam create-role \
--role-name role01 \
--assume-role-policy-document file://role01.json

-- 4. ポリシーをロールにアタッチ
aws iam attach-role-policy \
--policy-arn arn:aws:iam::999999999999:policy/policy01 \
--role-name role01

 


-- 5. S3 バケットを作成する

aws s3 mb s3://bucket123

aws s3 ls

wget https://awsdocs.s3.amazonaws.com/servicecatalog/development-environment.template

aws s3 cp development-environment.template s3://bucket123

aws s3 ls s3://bucket123 --recursive

 


-- 6. ポートフォリオの作成

aws servicecatalog create-portfolio \
--display-name pf01 \
--description pf01 \
--provider-name provider01


aws servicecatalog list-portfolios

aws servicecatalog describe-portfolio \
--id port-1111111111111

 

-- 7. 製品の作成

vim a.json

{
    "AcceptLanguage": "en",
    "Name": "product01",
    "Owner": "owner01",
    "Description": "product01",
    "SupportDescription": "support01",
    "SupportEmail": "hoge@example.com",
    "SupportUrl": "http://example.com/hoge",
    "ProductType": "CLOUD_FORMATION_TEMPLATE",
    "ProvisioningArtifactParameters": {
        "Name": "v1.0",
        "Description": "v1.0",
        "Info": {
            "LoadTemplateFromURL": "https://bucket123.s3.ap-northeast-1.amazonaws.com/development-environment.template"
        },
        "Type": "CLOUD_FORMATION_TEMPLATE"
    }
}

aws servicecatalog create-product \
--cli-input-json file://a.json

aws servicecatalog associate-product-with-portfolio \
--product-id prod-2222222222222 \
--portfolio-id port-1111111111111

 

aws servicecatalog describe-product-as-admin \
--name product01

 

 


-- 8. テンプレート制約の追加


aws servicecatalog create-constraint \
--portfolio-id port-1111111111111 \
--product-id prod-2222222222222 \
--parameters '{
  "Rules": {
    "Rule1": {
      "Assertions": [
        {
          "Assert" : {"Fn::Contains": [["t2.micro", "t2.small"], {"Ref": "InstanceType"}]},
          "AssertDescription": "Instance type should be t2.micro or t2.small"
        }
      ]
    }
  }
}' \
--type TEMPLATE \
--description constraint01


aws servicecatalog list-constraints-for-portfolio \
--portfolio-id port-1111111111111

aws servicecatalog describe-constraint \
--id cons-3333333333333

 

-- 9. 起動制約の追加

aws servicecatalog create-constraint \
--portfolio-id port-1111111111111 \
--product-id prod-2222222222222 \
--parameters '{"RoleArn" : "arn:aws:iam::999999999999:role/role01"}' \
--type LAUNCH \
--description constraint02


aws servicecatalog list-constraints-for-portfolio \
--portfolio-id port-1111111111111

aws servicecatalog describe-constraint \
--id cons-4444444444444

 

-- 10. ポートフォリオへのアクセス権限をエンドユーザへ付与


aws servicecatalog associate-principal-with-portfolio \
--portfolio-id port-1111111111111 \
--principal-arn arn:aws:iam::999999999999:user/testuser \
--principal-type IAM

aws servicecatalog list-principals-for-portfolio \
--portfolio-id port-1111111111111

 

 

-- 11. エンドユーザーで動作確認

サービスカタログではt2.microかt2.smallしか選べない

 


-- 12. クリーンアップ

-- プロビジョニングされた製品の終了

-- ポートフォリオからユーザを削除

aws servicecatalog list-principals-for-portfolio \
--portfolio-id port-1111111111111

aws servicecatalog disassociate-principal-from-portfolio \
--portfolio-id port-1111111111111 \
--principal-arn arn:aws:iam::999999999999:user/testuser


-- ポートフォリオから制約を削除

aws servicecatalog list-constraints-for-portfolio \
--portfolio-id port-1111111111111


aws servicecatalog delete-constraint \
--id cons-3333333333333


aws servicecatalog delete-constraint \
--id cons-4444444444444

-- ポートフォリオから製品を削除

aws servicecatalog disassociate-product-from-portfolio \
--product-id prod-2222222222222 \
--portfolio-id port-1111111111111

 

-- 製品の削除

aws servicecatalog describe-product-as-admin \
--name product01

aws servicecatalog delete-product \
--id prod-2222222222222

 

 


-- ポートフォリオの削除


aws servicecatalog list-portfolios

aws servicecatalog describe-portfolio \
--id port-1111111111111

aws servicecatalog delete-portfolio \
--id port-1111111111111


-- バケットの削除

aws s3 ls
aws s3 rb s3://bucket123  --force


-- IAMロールの削除
aws iam list-roles | grep role01

aws iam detach-role-policy \
--role-name role01 \
--policy-arn arn:aws:iam::999999999999:policy/policy01

aws iam delete-role --role-name role01


-- IAMポリシーの削除
aws iam list-policies | grep policy01

aws iam delete-policy \
--policy-arn arn:aws:iam::999999999999:policy/policy01

 

{FIS}チュートリアル: テストインスタンスの停止と使用開始AWS FIS


https://business.ntt-east.co.jp/content/cloudsolution/column-try-46.html
https://qiita.com/hirosys-biz/items/16002428f87c08c0a637
https://docs.aws.amazon.com/ja_jp/fis/latest/userguide/fis-tutorial-stop-instances.html

-- 1. コマンド等のインストール

-- 1.1 aws cli version 2 インストール

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
aws --version

-- 1.2 jqインストール
sudo yum -y install jq


-- 2. IAMポリシー作成
vim policy01.json

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "AllowFISExperimentRoleEC2Actions",
            "Effect": "Allow",
            "Action": [
                "ec2:RebootInstances",
                "ec2:StopInstances",
                "ec2:StartInstances",
                "ec2:TerminateInstances"
            ],
            "Resource": "arn:aws:ec2:*:*:instance/*"
        },
        {
            "Sid": "AllowFISExperimentRoleSpotInstanceActions",
            "Effect": "Allow",
            "Action": [
                "ec2:SendSpotInstanceInterruptions"
            ],
            "Resource": "arn:aws:ec2:*:*:instance/*"
        }
    ]
}

aws iam create-policy \
--policy-name policy01 \
--policy-document file://policy01.json

-- 3. IAMロール作成
vim role01.json

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "Service": [
                  "fis.amazonaws.com"
                ]
            },
            "Action": "sts:AssumeRole"
        }
    ]
}


aws iam create-role \
--role-name role01 \
--assume-role-policy-document file://role01.json

-- 4. ポリシーをロールにアタッチ
aws iam attach-role-policy \
--policy-arn arn:aws:iam::999999999999:policy/policy01 \
--role-name role01


-- 5. 検証用EC2インスタンス作成

aws ec2 run-instances \
--image-id ami-0404778e217f54308 \
--instance-type t3.nano \
--key-name key1 \
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=instance01}]'

aws ec2 run-instances \
--image-id ami-0404778e217f54308 \
--instance-type t3.nano \
--key-name key1 \
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=instance02}]'

 

-- 6. 実験テンプレートの作成
vim a.json

{
    "description": "fis01",
    "stopConditions": [
        {
            "source": "none"
        }
    ],
    "targets": {
        "bothInstances": {
            "resourceType": "aws:ec2:instance",
            "resourceArns": [
                "arn:aws:ec2:ap-northeast-1:999999999999:instance/i-11111111111111111",
                "arn:aws:ec2:ap-northeast-1:999999999999:instance/i-22222222222222222"
            ],
            "selectionMode": "ALL"
        },
        "oneRandomInstance": {
            "resourceType": "aws:ec2:instance",
            "resourceArns": [
                "arn:aws:ec2:ap-northeast-1:999999999999:instance/i-22222222222222222",
                "arn:aws:ec2:ap-northeast-1:999999999999:instance/i-11111111111111111"
            ],
            "selectionMode": "COUNT(1)"
        }
    },
    "actions": {
        "stopBothInstances": {
            "actionId": "aws:ec2:stop-instances",
            "description": "stopBothInstances",
            "parameters": {
                "startInstancesAfterDuration": "PT3M"
            },
            "targets": {
                "Instances": "bothInstances"
            },
            "startAfter": [
                "stopOneInstance"
            ]
        },
        "stopOneInstance": {
            "actionId": "aws:ec2:stop-instances",
            "description": "stopOneInstance",
            "parameters": {
                "startInstancesAfterDuration": "PT3M"
            },
            "targets": {
                "Instances": "oneRandomInstance"
            }
        }
    },
    "roleArn": "arn:aws:iam::999999999999:role/role01",
    "tags": {
        "Name": "fis01"
    }
}


aws fis create-experiment-template \
--cli-input-json file://a.json

 

aws fis list-experiment-templates

aws fis get-experiment-template \
--id EXT33333333333333


-- 7. 実験の実施

aws fis list-experiments

aws fis start-experiment \
--experiment-template-id EXT33333333333333

aws fis get-experiment \
--id EXP444444444444444

 


-- 8. クリーンアップ

 

-- 実験テンプレートの削除

aws fis list-experiment-templates

aws fis get-experiment-template \
--id EXT33333333333333

aws fis delete-experiment-template \
--id EXT33333333333333

 

-- EC2インスタンスの削除

aws ec2 describe-instances

aws ec2 terminate-instances --instance-ids i-22222222222222222
aws ec2 terminate-instances --instance-ids i-11111111111111111

-- IAMロールの削除
aws iam list-roles | grep role01

aws iam detach-role-policy \
--role-name role01 \
--policy-arn arn:aws:iam::999999999999:policy/policy01


aws iam delete-role --role-name role01


-- IAMポリシーの削除
aws iam list-policies | grep policy01

aws iam delete-policy \
--policy-arn arn:aws:iam::999999999999:policy/policy01

 

 

データ更新時のundo使用量(更新カラム数の影響)


(8.0.28)

-- 1. テストテーブル準備
drop table tab1;
create table tab1(
    col1 int primary key
   ,col2 char(100)
   ,col3 char(100)
   ,col4 char(100)
   ,col5 char(100)
   ,col6 char(100)
   ,col7 char(100)
   ,col8 char(100)
   ,col9 char(100)
   );

drop procedure proc1;

delimiter //
create procedure proc1(in x int)
begin
  declare i int;
  set i = 0;
  start transaction;
  while i < x do
    set i = i + 1;
    insert into tab1 values(
       i
     , cast( (floor(rand() * 1000000000000)+1)  as char)
     , cast( (floor(rand() * 1000000000000)+1)  as char)
     , cast( (floor(rand() * 1000000000000)+1)  as char)
     , cast( (floor(rand() * 1000000000000)+1)  as char)
     , cast( (floor(rand() * 1000000000000)+1)  as char)
     , cast( (floor(rand() * 1000000000000)+1)  as char)
     , cast( (floor(rand() * 1000000000000)+1)  as char)
     , cast( (floor(rand() * 1000000000000)+1)  as char)
    );
  end while;
  commit;
end
//
delimiter ;

call proc1(1000000);
select count(*) from tab1;

 


-- 2. undo使用量確認
別セッションから確認

 

select page_type, count(*) from information_schema.innodb_buffer_page
group by page_type
order by page_type
;

: > a.log
while true;do mysql test < a.sql >> a.log ;sleep 3;done


-- 3. データ更新


測定前にUNDOを解放するため、DBを停止・起動

 

update tab1 set
     col2 = cast( (floor(rand() * 1000000000000)+1)  as char)
   , col3 = cast( (floor(rand() * 1000000000000)+1)  as char)
--   , col4 = cast( (floor(rand() * 1000000000000)+1)  as char)
--   , col5 = cast( (floor(rand() * 1000000000000)+1)  as char)
--   , col6 = cast( (floor(rand() * 1000000000000)+1)  as char)
--   , col7 = cast( (floor(rand() * 1000000000000)+1)  as char)
--   , col8 = cast( (floor(rand() * 1000000000000)+1)  as char)
--   , col9 = cast( (floor(rand() * 1000000000000)+1)  as char)
   ;

 


データサイズ確認
analyze table tab1;
select
  table_name
  ,table_rows
  ,avg_row_length
  ,data_length/1024/1024/1024 tableGB
  ,index_length/1024/1024/1024 indexGB
from information_schema.tables
where table_schema = database()
and table_name = 'tab1'
;

-- 4. 結果

-- 4.1. 更新カラム数2個の場合

tableGB        | indexGB
---------------+----------------
0.849609375000 | 0.000000000000


undo使用量 = 14942 x 16 x 1024 /1024/1024/1024 = 0.23G

-- 4.2. 更新カラム数4個の場合

tableGB        | indexGB
---------------+---------------
0.849609375000 | 0.000000000000

undo使用量 = 27482 x 16 x 1024 /1024/1024/1024 = 0.42G

-- 4.3. 更新カラム数8個の場合

tableGB        | indexGB
---------------+----------------
0.849609375000 | 0.000000000000

undo使用量 = 45179 x 16 x 1024 /1024/1024/1024 = 0.69G


undo使用量は実際に更新されるカラムサイズにおよそ比例する

 

 


(19c)

 

-- 1. テストテーブル準備
drop table tab1 purge;
create table tab1(
    col1 int primary key
   ,col2 char(100)
   ,col3 char(100)
   ,col4 char(100)
   ,col5 char(100)
   ,col6 char(100)
   ,col7 char(100)
   ,col8 char(100)
   ,col9 char(100)
   );

declare
begin
for i in 1..1000000 loop
  insert into tab1 values(
     i
   , to_char(floor(dbms_random.value(1, 1000000000001) ) )
   , to_char(floor(dbms_random.value(1, 1000000000001) ) )
   , to_char(floor(dbms_random.value(1, 1000000000001) ) )
   , to_char(floor(dbms_random.value(1, 1000000000001) ) )
   , to_char(floor(dbms_random.value(1, 1000000000001) ) )
   , to_char(floor(dbms_random.value(1, 1000000000001) ) )
   , to_char(floor(dbms_random.value(1, 1000000000001) ) )
   , to_char(floor(dbms_random.value(1, 1000000000001) ) )
   );

end loop;
end;
/

commit;
select count(*) from tab1;
select sys_context('userenv','sid') from dual;

 

-- 2. undo使用量確認
別セッションから確認


bash /u01/app/oracle/product/19.0.0/dbhome_1/sqldeveloper/sqldeveloper/bin/sql test/test@pdb1


select USED_UBLK,USED_UREC from v$transaction where addr = ( select taddr from v$session where sid = 49 );

repeat 1000 1

 


-- 3. データ更新

update tab1 set
  col2 = to_char(floor(dbms_random.value(1, 1000000000001) ) )
, col3 = to_char(floor(dbms_random.value(1, 1000000000001) ) )
-- , col4 = to_char(floor(dbms_random.value(1, 1000000000001) ) )
-- , col5 = to_char(floor(dbms_random.value(1, 1000000000001) ) )
-- , col6 = to_char(floor(dbms_random.value(1, 1000000000001) ) )
-- , col7 = to_char(floor(dbms_random.value(1, 1000000000001) ) )
-- , col8 = to_char(floor(dbms_random.value(1, 1000000000001) ) )
-- , col9 = to_char(floor(dbms_random.value(1, 1000000000001) ) )
;


commit;

 


データサイズ確認

col segment_name for a5
select segment_name,bytes/1024/1024/1024 gbytes 
from user_segments where segment_name in ('TAB1') order by segment_name;


-- 4. 結果

-- 4.1. 更新カラム数2個の場合

SEGME     GBYTES
----- ----------
TAB1    .9609375


undo使用量 = 43900 x 8 x 1024 /1024/1024/1024 = 0.33G

-- 4.2. 更新カラム数4個の場合

SEGME     GBYTES
----- ----------
TAB1    .9609375

undo使用量 = 70184 x 8 x 1024 /1024/1024/1024 = 0.54G

-- 4.3. 更新カラム数8個の場合

SEGME     GBYTES
----- ----------
TAB1    .9609375

undo使用量 = 123257 x 8 x 1024 /1024/1024/1024 = 0.94G

 

undo使用量は実際に更新されるカラムサイズにおよそ比例する

 

 


(14)

undo領域なし


(2019)

 

-- 1. テストテーブル準備
drop table tab1;
create table tab1(
    col1 int primary key
   ,col2 char(100)
   ,col3 char(100)
   ,col4 char(100)
   ,col5 char(100)
   ,col6 char(100)
   ,col7 char(100)
   ,col8 char(100)
   ,col9 char(100)
   );

declare @i int;
set @i = 1;
begin transaction;
while @i <= 1000000
begin
  insert into tab1 values(
      @i
    , cast( (floor(rand() * 1000000000000)+1) as varchar )
    , cast( (floor(rand() * 1000000000000)+1) as varchar )
    , cast( (floor(rand() * 1000000000000)+1) as varchar )
    , cast( (floor(rand() * 1000000000000)+1) as varchar )
    , cast( (floor(rand() * 1000000000000)+1) as varchar )
    , cast( (floor(rand() * 1000000000000)+1) as varchar )
    , cast( (floor(rand() * 1000000000000)+1) as varchar )
    , cast( (floor(rand() * 1000000000000)+1) as varchar )
    );
    
  set @i = @i + 1;
end
commit;
select count(*) from tab1;

 

 

-- 2. トランザクション分離レベルの変更
ALTER DATABASE test SET READ_COMMITTED_SNAPSHOT ON with rollback after 1 seconds;
DBCC USEROPTIONS

-- 3. undo使用量確認
別セッションから確認


select count(*) , sum(record_length_first_part_in_bytes)/1024/1024 mb 
from sys.dm_tran_version_store;


del a.log

@echo off

:LOOP
    sqlcmd -d test -b -i a.sql >> a.log
    timeout 3 > nul
goto :LOOP

exit /b 0


-- 4. データ更新

update tab1 set
       col2 = cast( (floor(rand() * 1000000000000)+1) as varchar )
     , col3 = cast( (floor(rand() * 1000000000000)+1) as varchar )
--     , col4 = cast( (floor(rand() * 1000000000000)+1) as varchar )
--     , col5 = cast( (floor(rand() * 1000000000000)+1) as varchar )
--     , col6 = cast( (floor(rand() * 1000000000000)+1) as varchar )
--     , col7 = cast( (floor(rand() * 1000000000000)+1) as varchar )
--     , col8 = cast( (floor(rand() * 1000000000000)+1) as varchar )
--     , col9 = cast( (floor(rand() * 1000000000000)+1) as varchar )
     ;

 

 

データサイズ確認
exec sp_spaceused 'dbo.tab1';


-- 5. 結果

-- 5.1. 更新カラム数2個の場合

data  index_size
888896 KB  3312 KB

undo使用量=0.8G


-- 5.2. 更新カラム数4個の場合

data  index_size
888896 KB  3312 KB

undo使用量=0.8G


-- 5.3. 更新カラム数8個の場合

data  index_size
888896 KB  3312 KB

undo使用量=0.8G


undo使用量は実際に更新されるカラムサイズとは関係なく、全カラムサイズに依存する

 

 

 

 

データ更新時のundo使用量(インデックスの影響)


(8.0.28)

-- 1. テストテーブル準備
drop table tab1;
create table tab1(
    col1 int primary key
   ,col2 char(100)
   ,col3 char(100)
   );

drop procedure proc1;

delimiter //
create procedure proc1(in x int)
begin
  declare i int;
  set i = 0;
  start transaction;
  while i < x do
    set i = i + 1;
    insert into tab1 values(i,0,0);
  end while;
  commit;
end
//
delimiter ;

call proc1(1000000);
select count(*) from tab1;


create index ind1 on tab1(col2);
create index ind2 on tab1(col3);


-- 2. undo使用量確認
別セッションから確認

 

select page_type, count(*) from information_schema.innodb_buffer_page
group by page_type
order by page_type
;

: > a.log
while true;do mysql test < a.sql >> a.log ;sleep 3;done


-- 3. データ更新


drop procedure proc2;

delimiter //
create procedure proc2(in x int)
begin
  declare i int;
  set i = 0;
  while i < x do
    set i = i + 1;
    update tab1 set
         col2 = cast( (floor(rand() * 1000000000000)+1)  as char)
       , col3 = cast( (floor(rand() * 1000000000000)+1)  as char)
       where col1 = i
       ;
  end while;
end
//
delimiter ;

測定前にUNDOを解放するため、DBを停止・起動

start transaction;
call proc2(1000000);

commit;


データサイズ確認
analyze table tab1;
select
  table_name
  ,table_rows
  ,avg_row_length
  ,data_length/1024/1024/1024 tableGB
  ,index_length/1024/1024/1024 indexGB
from information_schema.tables
where table_schema = database()
and table_name = 'tab1'
;

-- 4. 結果

-- 4.1. インデックスなしの場合

tableGB        | indexGB
---+
0.228256225586 | 0.000000000000

undo使用量 = 15836 x 16 x 1024 /1024/1024/1024 = 0.24G

-- 4.2. インデックス1個の場合

tableGB        | indexGB
---+
0.228256225586 | 0.267837524414

undo使用量 = 22285 x 16 x 1024 /1024/1024/1024 = 0.34G

-- 4.3. インデックス2個の場合

tableGB        | indexGB
---+---
0.228256225586 | 0.570068359375


undo使用量 = 23912 x 16 x 1024 /1024/1024/1024 = 0.36G

 

インデックス1個追加によりundo使用量は1.5倍くらいに増える。
インデックス追加によるundo使用量増加分はインデックス数にあまり依存しない。

 

 


(19c)

 

-- 1. テストテーブル準備
drop table tab1 purge;
create table tab1(
    col1 int primary key
   ,col2 char(100)
   ,col3 char(100)
   );

declare
begin
for i in 1..1000000 loop
  insert into tab1 values(i,0,0);
end loop;
end;
/

commit;
select count(*) from tab1;
select sys_context('userenv','sid') from dual;

create index ind1 on tab1(col2);
create index ind2 on tab1(col3);


-- 2. undo使用量確認
別セッションから確認


bash /u01/app/oracle/product/19.0.0/dbhome_1/sqldeveloper/sqldeveloper/bin/sql test/test@pdb1

select USED_UBLK,USED_UREC from v$transaction where addr = ( select taddr from v$session where sid = 48 );

repeat 1000 1

 

-- 3. データ更新

declare
begin
for i in 1..1000000 loop
  update tab1 set
      col2 = to_char(floor(dbms_random.value(1, 1000000000001) ) )
    , col3 = to_char(floor(dbms_random.value(1, 1000000000001) ) )
    where col1 = i
    ;
end loop;
end;
/

commit;

 


データサイズ確認

col segment_name for a5
select segment_name,bytes/1024/1024/1024 gbytes 
from user_segments where segment_name in ('TAB1','IND1','IND2') order by segment_name;


-- 4. 結果

-- 4.1. インデックスなしの場合

SEGME     GBYTES
- --
TAB1    .2265625

undo使用量 = 44885 x 8 x 1024 /1024/1024/1024 = 0.34G

-- 4.2. インデックス1個の場合
SEGME     GBYTES
- --
IND1      .28125
TAB1    .2265625

undo使用量 = 89932 x 8 x 1024 /1024/1024/1024 = 0.67G

-- 4.3. インデックス2個の場合

SEGME     GBYTES
- --
IND1      .28125
IND2      .28125
TAB1    .2265625

undo使用量 = 136038 x 8 x 1024 /1024/1024/1024 = 1.04G


インデックス1個追加によりundo使用量は2倍くらいに増える。
インデックス追加によるundo使用量増加分はインデックス数にほぼ比例する


 


(14)

undo領域なし

 


(2019)

 

-- 1. テストテーブル準備
drop table tab1;
create table tab1(
    col1 int primary key
   ,col2 char(100)
   ,col3 char(100)
   );

declare @i int;
set @i = 1;
begin transaction;
while @i <= 1000000
begin
  insert into tab1 values(@i,0,0);
  set @i = @i + 1;
end
commit;
select count(*) from tab1;


create index ind1 on tab1(col2);
create index ind2 on tab1(col3);


-- 2. トランザクション分離レベルの変更
ALTER DATABASE test SET READ_COMMITTED_SNAPSHOT ON with rollback after 1 seconds;
DBCC USEROPTIONS

-- 3. undo使用量確認
別セッションから確認


select count(*) , sum(record_length_first_part_in_bytes)/1024/1024 mb 
from sys.dm_tran_version_store;


del a.log

@echo off

:LOOP
    sqlcmd -d test -b -i a.sql >> a.log
    timeout 3 > nul
goto :LOOP

exit /b 0


-- 4. データ更新

declare @i int;
set @i = 1;
begin transaction;
while @i <= 1000000
begin
   update tab1 set
           col2 = cast( (floor(rand() * 1000000000000)+1) as varchar )
         , col3 = cast( (floor(rand() * 1000000000000)+1) as varchar )
         where col1 = @i
         ;
   set @i = @i + 1;
end

commit;

 

データサイズ確認
exec sp_spaceused 'dbo.tab1';


-- 5. 結果

-- 5.1. インデックスなしの場合

data  index_size
228576 KB    856 KB

undo使用量=0.2G


-- 5.2. インデックス1個の場合

data  index_size
228576 KB  183480 KB

undo使用量=0.3G

-- 5.3. インデックス2個の場合

data  index_size
421048 KB  364880 KB

undo使用量=0.4G


インデックス1個追加によりundo使用量は1.5倍くらいに増える。
インデックス追加によるundo使用量増加分はインデックス数にほぼ比例する