sunnyworm
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # MariaDB Versioning - without versioning - Feets in the hole. ###### tags: `kyc-right` - Related article: MariaDB Temporal Table https://hackmd.io/Iwe0MVlwRpOMZ70W1ZwPtQ --- [TOC] ## Description With versioned table (timestamp based) including one non-versioned column, using upsert (insert … on duplicate update) to update the unversioning column will still create new version records, which shouldn't. - upstream tracking number https://jira.mariadb.org/browse/MDEV-23100 ## Environment Docker image from docker hub > Server version: 10.4.12-MariaDB-1:10.4.12+maria~bionic ## Reproduce ### Create versioned table Create in 3 different ways, inclusive, exclusive, and set by each column. ```sql= CREATE TABLE t1 ( id INT auto_increment primary key, x INT unique, y INT WITHOUT SYSTEM VERSIONING )WITH SYSTEM VERSIONING; CREATE TABLE t2 ( id INT auto_increment primary key, x INT unique WITH SYSTEM VERSIONING, y INT ); CREATE TABLE t3 ( id INT auto_increment primary key, x INT unique WITH SYSTEM VERSIONING, y INT WITHOUT SYSTEM VERSIONING ); ``` ### Upsert Upsert with `insert on duplicate key update`. The 2nd and 3rd SQL query for each table are doing UPDATE to non-versioned column. ```sql insert into t1 (x, y) values ('1', '123') on duplicate key update x = values(x), y = values(y); insert into t1 (x, y) values ('1', '1234') on duplicate key update x = values(x), y = values(y); insert into t1 (x, y) values ('1', '12345') on duplicate key update x = values(x), y = values(y); insert into t2 (x, y) values ('1', '123') on duplicate key update x = values(x), y = values(y); insert into t2 (x, y) values ('1', '1234') on duplicate key update x = values(x), y = values(y); insert into t2 (x, y) values ('1', '12345') on duplicate key update x = values(x), y = values(y); insert into t3 (x, y) values ('1', '123') on duplicate key update x = values(x), y = values(y); insert into t3 (x, y) values ('1', '1234') on duplicate key update x = values(x), y = values(y); insert into t3 (x, y) values ('1', '12345') on duplicate key update x = values(x), y = values(y); ``` #### Output There should be only 1 version, but we see 3 instead. ```sql= select *, ROW_START, ROW_END from t1 for system_time all; select *, ROW_START, ROW_END from t2 for system_time all; select *, ROW_START, ROW_END from t3 for system_time all; ``` ![](https://i.imgur.com/Tsl95of.png) ### Simple UPDATE works fine ```sql= update t1 set y = '123456' where x = 1; select *, ROW_START, ROW_END from t1 for system_time all; ``` ![](https://i.imgur.com/uPe4lWJ.png) > With `on duplicate key update`, > even we only update the column `WITHOUT VERSIONING`, > MariaDB still create new version record for the row. ### Replace? ```sql= replace into t1 (x, y) values ('1', '123456'); -- try this few times ``` ![](https://i.imgur.com/u44RWBX.png) - same result as upsert - tried setting `id` to `WITHOUT SYSTEM VERSIONING` makes no difference - removing auto-increment id makes no difference ### trxid-based versioning #### Does not support partition > https://jira.mariadb.org/browse/MDEV-15951 > That's because "partition" storage engine doesn't say that it supports trxid-based versioning. I think it should support it at least for PARTITION BY RANGE and other not BY SYSTEM_TIME partitioning. #### Testing ```sql= CREATE TABLE t1 ( id INT auto_increment primary key, x INT unique, y INT, z timestamp without system versioning, create_at timestamp default CURRENT_TIMESTAMP(), start_trxid BIGINT UNSIGNED GENERATED ALWAYS AS ROW START, end_trxid BIGINT UNSIGNED GENERATED ALWAYS AS ROW END, PERIOD FOR SYSTEM_TIME(start_trxid, end_trxid) )WITH SYSTEM VERSIONING; insert into t1 (x, y, z) values ('1', '1', '2020-07-02 07:36:00'), ('2', '2', '2020-07-02 07:36:00'), ('3', '3', '2020-07-02 07:36:00') on duplicate key update y = values(y), z = values(z); insert into t1 (x, y, z) values ('2', '3', '2020-07-04 07:36:00'), ('3', '4', '2020-07-04 07:36:00'), ('4', '4', '2020-07-04 07:36:00'), ('5', '5', '2020-07-04 07:36:00') on duplicate key update y = values(y), z = values(z); insert into t1 (x, y, z) values ('2', '4', '2020-07-06 07:36:00'), ('3', '5', '2020-07-06 07:36:00'), ('4', '5', '2020-07-06 07:36:00'), ('5', '5', '2020-07-06 07:36:00') on duplicate key update y = values(y), z = values(z); delete from t1 where z < '2020-07-04 07:36:00'; select * from t1 for system_time all; -- # last update z: 2020-07-03 07:36:00, current z: 2020-07-06 07:36:00 -- # delete: 取得所有最新資料(包含刪除)後,last_show_at 大於等於上次更新時間且 max_trx 不為最大值者 select *, MAX(end_trxid) as max_trx from t1 for system_time all group by id; select * from (select *, MAX(end_trxid) as max_trx from t1 for system_time all group by id) as latest where latest.z >= '2020-07-02 07:36:00' and latest.max_trx < 18446744073709551615; -- # update: end_trx = MAX and z = 這次時間 select * from t1 where id in (select *, MAX(z) from t1 group by id); select * from t1 for system_time all as max_2 where max_2.z < (select MAX(z) from t1 for system_time all as max_1 where max_2.id = max_1.id group by id) -- # 找出有第二大的 z 值的紀錄 and max_2.z >= '2020-07-04 07:36:00' -- # 且第二大的 z 值大於等於上次更新時間 (上次做 change-set 時還沒有被更新到) order by id; select MAX(z) as latest from t1 for system_time all as max_1 group by id -- # insert: create_at > 上次更新時間 select * from t1 for system_time all where create_at > '2020-07-03 09:05:57' -- # 2 versions -- # deleted -- # -- # select * from t1; -- # show create table t1 ``` ### on duplicate key update with primary key + multi unique key #### Test1 ```sql= CREATE TABLE `com_supervisor` ( `id` int(11) NOT NULL AUTO_INCREMENT, `business_uid` varchar(10) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `sup_id` text COLLATE utf8mb4_unicode_ci DEFAULT NULL, `sup_title` text COLLATE utf8mb4_unicode_ci DEFAULT NULL, `sup_name` text COLLATE utf8mb4_unicode_ci DEFAULT NULL, `sup_rep_id` varchar(10) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `sup_rep_name` text COLLATE utf8mb4_unicode_ci DEFAULT NULL, `sup_contribution` text COLLATE utf8mb4_unicode_ci DEFAULT NULL, `last_show_at` datetime DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `business_uid` (`business_uid`,`sup_id`) USING HASH ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; SET @@system_versioning_alter_history = 1; ALTER TABLE com_supervisor ADD COLUMN start_trxid BIGINT UNSIGNED GENERATED ALWAYS AS ROW START, ADD COLUMN end_trxid BIGINT UNSIGNED GENERATED ALWAYS AS ROW END, ADD PERIOD FOR SYSTEM_TIME(start_trxid, end_trxid), ADD SYSTEM VERSIONING; ALTER TABLE com_supervisor CHANGE last_show_at last_show_at DATETIME WITHOUT SYSTEM VERSIONING; INSERT INTO com_supervisor (business_uid, sup_id, sup_title, sup_name, sup_rep_id, sup_rep_name, sup_contribution, last_show_at) VALUES ('91734707', '0008', '', '黃秋松', '', '', '750,000', CURRENT_TIMESTAMP()), ('91734707', '0007', '', '黃秋松', '', '', '750,000', CURRENT_TIMESTAMP()) ON DUPLICATE KEY UPDATE sup_name = VALUES(sup_name), sup_title = VALUES(sup_title), sup_rep_id = VALUES(sup_rep_id), sup_rep_name = VALUES(sup_rep_name), sup_contribution = VALUES(sup_contribution), last_show_at = VALUES(last_show_at); -- ok INSERT INTO com_supervisor (business_uid, sup_id, sup_title, sup_name, sup_rep_id, sup_rep_name, sup_contribution, last_show_at) VALUES ('91734707', '0008', '1', '黃秋松', '', '', '750,000', CURRENT_TIMESTAMP()), ('91734707', '0007', '2', '黃秋松', '', '', '750,000', CURRENT_TIMESTAMP()) ON DUPLICATE KEY UPDATE sup_name = VALUES(sup_name), sup_title = VALUES(sup_title), sup_rep_id = VALUES(sup_rep_id), sup_rep_name = VALUES(sup_rep_name), sup_contribution = VALUES(sup_contribution), last_show_at = VALUES(last_show_at); -- ok for first time, but after the versioning created, it's will cause an error -- `[23000][1062] Duplicate entry '91734707-0008-18446744073709551615' for key 'business_uid'` ``` #### Test2 ```sql CREATE TABLE `com_supervisor` ( `id` INT primary key auto_increment, `business_uid` varchar(10) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `sup_id` varchar(10) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `sup_title` text COLLATE utf8mb4_unicode_ci DEFAULT NULL, `sup_name` text COLLATE utf8mb4_unicode_ci DEFAULT NULL, `sup_rep_id` varchar(10) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `sup_rep_name` text COLLATE utf8mb4_unicode_ci DEFAULT NULL, `sup_contribution` text COLLATE utf8mb4_unicode_ci DEFAULT NULL, `last_show_at` datetime DEFAULT NULL WITHOUT SYSTEM VERSIONING, `start_trxid` bigint(20) unsigned GENERATED ALWAYS AS ROW START, `end_trxid` bigint(20) unsigned GENERATED ALWAYS AS ROW END, UNIQUE KEY `business_uid` (`business_uid`,`sup_id`,`end_trxid`) USING HASH, PERIOD FOR SYSTEM_TIME (`start_trxid`, `end_trxid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci WITH SYSTEM VERSIONING; -- same as test1 ``` #### Test3 ```sql CREATE TABLE `com_supervisor` ( `id` INT primary key auto_increment, `business_uid` varchar(10) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `sup_id` varchar(10) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `sup_title` text COLLATE utf8mb4_unicode_ci DEFAULT NULL, `sup_name` text COLLATE utf8mb4_unicode_ci DEFAULT NULL, `sup_rep_id` varchar(10) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `sup_rep_name` text COLLATE utf8mb4_unicode_ci DEFAULT NULL, `sup_contribution` text COLLATE utf8mb4_unicode_ci DEFAULT NULL, `last_show_at` datetime DEFAULT NULL WITHOUT SYSTEM VERSIONING, `start_trxid` bigint(20) unsigned GENERATED ALWAYS AS ROW START, `end_trxid` bigint(20) unsigned GENERATED ALWAYS AS ROW END, UNIQUE KEY `business_uid` (`business_uid`,`sup_id`,`end_trxid`), PERIOD FOR SYSTEM_TIME (`start_trxid`, `end_trxid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci WITH SYSTEM VERSIONING; -- it works ```

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully