Upgrade to Pro — share decks privately, control downloads, hide ads and more …

All you need to know about MySQL InnoDB Primary...

All you need to know about MySQL InnoDB Primary Keys

This is a new version of this slide deck that has for ambition to teach you why Primary Keys are important when using InnoDB.

lefred

May 10, 2024
Tweet

More Decks by lefred

Other Decks in Technology

Transcript

  1. Frédéric Descamps Community Manager Oracle MySQL May 2024 - GroningenPHP

    All you need to know about MySQL InnoDB Primary Keys
  2. @lefred MySQL Evangelist using MySQL since version 3.20 devops believer

    living in h ps://lefred.be Frédéric Descamps Copyright @ 2024 Oracle and/or its affiliates. 3
  3. Clustered Index Each InnoDB table has a special index called

    the clustered index that stores row data. Typically, the clustered index is synonymous with the primary key. Copyright @ 2024 Oracle and/or its affiliates. 5
  4. Clustered Index Each InnoDB table has a special index called

    the clustered index that stores row data. Typically, the clustered index is synonymous with the primary key. Let's check now an example on how we usually mentally represent a table and an index. Copyright @ 2024 Oracle and/or its affiliates. 5
  5. Mental representation of table and an index Table 1 Indexed

    Column column 1 column 2 column x column n Index Copyright @ 2024 Oracle and/or its affiliates. 6
  6. Mental representation of table and an index Table 1 Index

    Indexed Column column 1 column 2 column x column n 5001 5001 a b 2022-03-14 NULL Copyright @ 2024 Oracle and/or its affiliates. 7
  7. Mental representation of a table and an index Table 1

    Index Indexed Column column 1 column 2 column x column n 5001 3 3 5001 a a c b 2022-03-14 2022-03-15 NULL NULL Copyright @ 2024 Oracle and/or its affiliates. 8
  8. Mental representation of a table and an index Table 1

    Index Indexed Column column 1 column 2 column x column n 5001 3 3 6 a a c b 2022-03-14 2022-03-15 NULL NULL 6 5001 be f 2022-03-15 1 Copyright @ 2024 Oracle and/or its affiliates. 9
  9. Mental representation of a table and an index Table 1

    Index Indexed Column column 1 column 2 column x column n 5001 3 3 6 a a c b 2022-03-14 2022-03-15 NULL NULL 6 27 be f 2022-03-15 1 27 5001 it d 2022-03-16 101 Copyright @ 2024 Oracle and/or its affiliates. 10
  10. Mental representation of a table and an index Table 1

    Index Indexed Column column 1 column 2 column x column n 5001 3 3 6 a a c b 2022-03-14 2022-03-15 NULL NULL 6 27 be f 2022-03-15 1 27 12 it d 2022-03-16 101 12 5001 uk e 2022-03-22 NULL Copyright @ 2024 Oracle and/or its affiliates. 11
  11. Mental representation of a table and an index Table 1

    Index Indexed Column column 1 column 2 column x column n 5001 3 3 6 a a c b 2022-03-14 2022-03-15 NULL NULL 6 27 be f 2022-03-15 1 27 12 it d 2022-03-16 101 12 5001 uk e 2022-03-22 NULL Copyright @ 2024 Oracle and/or its affiliates. 12
  12. InnoDB representation of a table and PK Table 1 PRIMARY

    KEY column 1 column 2 column x column n 5001 a b 2022-03-14 NULL insert into table1 values (5001,'a','b','2022-03-14', NULL); Copyright @ 2024 Oracle and/or its affiliates. 13
  13. InnoDB representation of a table and PK Table 1 PRIMARY

    KEY column 1 column 2 column x column n 5001 a b 2022-03-14 NULL insert into table1 values (3,'c','a','2022-03-15', NULL); 3 a c 2022-03-15 NULL Copyright @ 2024 Oracle and/or its affiliates. 14
  14. InnoDB representation of a table and PK 6 be f

    2022-03-15 1 Table 1 PRIMARY KEY column 1 column 2 column x column n 5001 a b 2022-03-14 NULL insert into table1 values (6,'f','be','2022-03-15', 1); 3 a c 2022-03-15 NULL Copyright @ 2024 Oracle and/or its affiliates. 15
  15. InnoDB representation of a table and PK 6 be f

    2022-03-15 1 Table 1 PRIMARY KEY column 1 column 2 column x column n 5001 a b 2022-03-14 NULL insert into table1 values (27,'d','it','2022-03-16', 101); 3 a c 2022-03-15 NULL 27 it d 2022-03-16 101 Copyright @ 2024 Oracle and/or its affiliates. 16
  16. InnoDB representation of a table and PK 6 be f

    2022-03-15 1 Table 1 PRIMARY KEY column 1 column 2 column x column n 5001 a b 2022-03-14 NULL insert into table1 values (12,'e','uk','2022-03-22', NULL); 3 a c 2022-03-15 NULL 27 it d 2022-03-16 101 12 uk e 2022-03-22 NULL Copyright @ 2024 Oracle and/or its affiliates. 17
  17. InnoDB representation of a table and PK Table 1 PRIMARY

    KEY column 1 column 2 column x column n 5001 a b 2022-03-14 NULL 3 a c 2022-03-15 NULL 6 be f 2022-03-15 1 27 it d 2022-03-16 101 12 uk e 2022-03-22 NULL Copyright @ 2024 Oracle and/or its affiliates. 18
  18. InnoDB representation of a table and PK Table 1 PRIMARY

    KEY column 1 column 2 column x column n 5001 a b 2022-03-14 NULL 3 a c 2022-03-15 NULL 6 be f 2022-03-15 1 27 it d 2022-03-16 101 12 uk e 2022-03-22 NULL This is the clustered index representation: stored by order of Primary Key Copyright @ 2024 Oracle and/or its affiliates. 18
  19. MySQL InnoDB Primary Keys They are mandatory ! Copyright @

    2024 Oracle and/or its affiliates. 19
  20. InnoDB Primary Key InnoDB stores data in table spaces. And

    so far, we know that records are stored and sorted using the clustered index. The Primary Key is a key for the index that uniquely de ned for a row, should be immutable. InnoDB needs a PRIMARY KEY No NULL values are allowed Monotonically increasing use UUID_TO_BIN() if you must use UUIDs, otherwise avoid them Copyright @ 2024 Oracle and/or its affiliates. 20
  21. InnoDB Primary Key (2) What we don't know is that

    all secondary indexes also contain the primary key as the right-most column in the index (even if this is not exposed). That means when a secondary index is used to retrieve a record, two indexes are used: rst the secondary one pointing to the primary key that will be used to nally retrieve the record. Copyright @ 2024 Oracle and/or its affiliates. 21
  22. InnoDB Primary Key (2) What we don't know is that

    all secondary indexes also contain the primary key as the right-most column in the index (even if this is not exposed). That means when a secondary index is used to retrieve a record, two indexes are used: rst the secondary one pointing to the primary key that will be used to nally retrieve the record. When no primary key is de ned, the rst unique not null key is used. And if none is available, InnoDB will create an hidden primary key (6 bytes). Copyright @ 2024 Oracle and/or its affiliates. 21
  23. InnoDB Primary Key (2) What we don't know is that

    all secondary indexes also contain the primary key as the right-most column in the index (even if this is not exposed). That means when a secondary index is used to retrieve a record, two indexes are used: rst the secondary one pointing to the primary key that will be used to nally retrieve the record. When no primary key is de ned, the rst unique not null key is used. And if none is available, InnoDB will create an hidden primary key (6 bytes). The problem with such key is that you don’t have any control on it and worse, this value is global to all tables without primary keys and can be a contention problem if you perform multiple simultaneous writes on such tables (dict_sys->mutex). Copyright @ 2024 Oracle and/or its affiliates. 21
  24. InnoDB Primary Key (2) What we don't know is that

    all secondary indexes also contain the primary key as the right-most column in the index (even if this is not exposed). That means when a secondary index is used to retrieve a record, two indexes are used: rst the secondary one pointing to the primary key that will be used to nally retrieve the record. When no primary key is de ned, the rst unique not null key is used. And if none is available, InnoDB will create an hidden primary key (6 bytes). The problem with such key is that you don’t have any control on it and worse, this value is global to all tables without primary keys and can be a contention problem if you perform multiple simultaneous writes on such tables (dict_sys->mutex). If you use HA solutions, Primary Keys are mandatory ! Copyright @ 2024 Oracle and/or its affiliates. 21
  25. InnoDB Primary Key (3) Primary Keys impact how the values

    are inserted and the size of the secondary indexes. A non sequential PK can lead to many random IOPS. Copyright @ 2024 Oracle and/or its affiliates. 22
  26. Also, it's more and more common to use applications that

    generate complete random primary keys...that means if the Primary Key is not sequential, InnoDB will have to heavily re-balance all the pages on inserts. InnoDB Primary Key (3) Primary Keys impact how the values are inserted and the size of the secondary indexes. A non sequential PK can lead to many random IOPS. Copyright @ 2024 Oracle and/or its affiliates. 22
  27. InnoDB Primary Key (4) If we compare the same load

    (inserts) when using an auto_increment integer as Primary Key, we can see that only the latest pages are recently touched: Generated with h ps://github.com/jeremycole/innodb_ruby from @jeremycole Copyright @ 2024 Oracle and/or its affiliates. 23
  28. InnoDB Primary Key (5) It's possible to return an error

    if a table is created without de ning any Primary Key: sql_require_primary_key. SQL SQL> > SET SET sql_require_primary_key sql_require_primary_key= =1 1 ; ; SQL SQL> > CREATE CREATE TABLE TABLE nopk2 nopk2 ( (i i int int not not null null, , name name varchar varchar( (20 20) )) ); ; ERROR: ERROR: 3750 3750 ( (HY000 HY000) ): Unable : Unable to to create create or or change a change a table table without a without a primary primary key key, , when when the system variable the system variable 'sql_require_primary_key' 'sql_require_primary_key' is is set set. . Add Add a a primary primary key key to to the the table table or or unset this variable unset this variable to to avoid this message avoid this message. . Note that Note that tables tables without a without a primary primary key key can cause performance problems can cause performance problems in in row row- -based based replication replication, , so please consult your DBA before changing this setting so please consult your DBA before changing this setting. . Copyright @ 2024 Oracle and/or its affiliates. 24
  29. Size ma ers So the data is actually stored in

    an index: the clustered index. Copyright @ 2024 Oracle and/or its affiliates. 26
  30. Size ma ers So the data is actually stored in

    an index: the clustered index. Secondary indexes are a copy of some parts of the data, sorted... but with a pointer to the record on the table. Copyright @ 2024 Oracle and/or its affiliates. 26
  31. Size ma ers So the data is actually stored in

    an index: the clustered index. Secondary indexes are a copy of some parts of the data, sorted... but with a pointer to the record on the table. That pointer is the Primary Key ! Copyright @ 2024 Oracle and/or its affiliates. 26
  32. This is why we always say that the right most

    column of a secondary index is the Primary Key and it's infact really included in it. Size ma ers So the data is actually stored in an index: the clustered index. Secondary indexes are a copy of some parts of the data, sorted... but with a pointer to the record on the table. That pointer is the Primary Key ! Copyright @ 2024 Oracle and/or its affiliates. 26
  33. CREATE CREATE TABLE TABLE ` `sbtest1 sbtest1` ` ( (

    ` `id id` ` char char( (200 200) ) NOT NOT NULL NULL, , ` `k k` ` int int NOT NOT NULL NULL DEFAULT DEFAULT '0' '0', , ` `c c` ` char char( (120 120) ) NOT NOT NULL NULL DEFAULT DEFAULT '' '', , ` `pad pad` ` char char( (60 60) ) NOT NOT NULL NULL DEFAULT DEFAULT '' '', , PRIMARY PRIMARY KEY KEY ( (` `id id` `) ), , KEY KEY ` `k_1 k_1` ` ( (` `k k` `) ) ) ) ENGINE ENGINE= =InnoDB InnoDB DEFAULT DEFAULT CHARSET CHARSET= =utf8mb4 utf8mb4 COLLATE COLLATE= =utf8mb4_0900_ai_ci utf8mb4_0900_ai_ci CREATE CREATE TABLE TABLE ` `sbtest2 sbtest2` ` ( ( ` `id id` ` int int NOT NOT NULL NULL AUTO_INCREMENT AUTO_INCREMENT, , ` `k k` ` int int NOT NOT NULL NULL DEFAULT DEFAULT '0' '0', , ` `c c` ` char char( (120 120) ) NOT NOT NULL NULL DEFAULT DEFAULT '' '', , ` `pad pad` ` char char( (60 60) ) NOT NOT NULL NULL DEFAULT DEFAULT '' '', , PRIMARY PRIMARY KEY KEY ( (` `id id` `) ), , KEY KEY ` `k_2 k_2` ` ( (` `k k` `) ) ) ) ENGINE ENGINE= =InnoDB InnoDB DEFAULT DEFAULT CHARSET CHARSET= =utf8mb4 utf8mb4 COLLATE COLLATE= =utf8mb4_0900_ai_ci utf8mb4_0900_ai_ci Size ma ers - example Let's take a look at these two similar tables, both having one Primary Key and one Secondary Key: Copyright @ 2024 Oracle and/or its affiliates. 27
  34. Size ma ers - example (2) Let's have a look

    at the table space's size on disk: [ [root@dynabook sbtest root@dynabook sbtest] ]# ls -lh # ls -lh total 1017M total 1017M -rw-r----- -rw-r----- 1 1 mysql mysql 744M Feb mysql mysql 744M Feb 7 7 09:36 sbtest1.ibd 09:36 sbtest1.ibd -rw-r----- -rw-r----- 1 1 mysql mysql 272M Feb mysql mysql 272M Feb 7 7 09:38 sbtest2.ibd 09:38 sbtest2.ibd Copyright @ 2024 Oracle and/or its affiliates. 28
  35. Size ma ers - example (2) Let's have a look

    at the table space's size on disk: [ [root@dynabook sbtest root@dynabook sbtest] ]# ls -lh # ls -lh total 1017M total 1017M -rw-r----- -rw-r----- 1 1 mysql mysql 744M Feb mysql mysql 744M Feb 7 7 09:36 sbtest1.ibd 09:36 sbtest1.ibd -rw-r----- -rw-r----- 1 1 mysql mysql 272M Feb mysql mysql 272M Feb 7 7 09:38 sbtest2.ibd 09:38 sbtest2.ibd Do you think the size di erence can be just related to the Primary Key di erence and not impacting the secondary index ? Copyright @ 2024 Oracle and/or its affiliates. 28
  36. Size ma ers - example (3) Let's have a look:

    SELECT SELECT NAME NAME, , TABLE_ROWS TABLE_ROWS, , format_bytes format_bytes( (data_length data_length) ) DATA_SIZE DATA_SIZE, , format_bytes format_bytes( (index_length index_length) ) INDEX_SIZE INDEX_SIZE, , format_bytes format_bytes( (data_length data_length+ +index_length index_length) ) TOTAL_SIZE TOTAL_SIZE, , format_bytes format_bytes( (data_free data_free) ) DATA_FREE DATA_FREE, , format_bytes format_bytes( (FILE_SIZE FILE_SIZE) ) FILE_SIZE FILE_SIZE, , format_bytes format_bytes( (( (FILE_SIZE FILE_SIZE/ /10 10 - - ( (data_length data_length/ /10 10 + + index_length index_length/ /10 10) )) )* *10 10) ) WASTED_SIZE WASTED_SIZE FROM FROM information_schema information_schema. .TABLES TABLES as as t t JOIN JOIN information_schema information_schema. .INNODB_TABLESPACES INNODB_TABLESPACES as as it it ON ON it it. .name name = = concat concat( (table_schema table_schema, ,"/" "/", ,table_name table_name) ) WHERE WHERE name name like like 'sbtest/%' 'sbtest/%' ORDER ORDER BY BY ( (data_length data_length + + index_length index_length) ) desc desc limit limit 5 5; ; + +----------------+------------+------------+------------+------------+-----------+------------+-------------+ ----------------+------------+------------+------------+------------+-----------+------------+-------------+ | | NAME NAME | | TABLE_ROWS TABLE_ROWS | | DATA_SIZE DATA_SIZE | | INDEX_SIZE INDEX_SIZE | | TOTAL_SIZE TOTAL_SIZE | | DATA_FREE DATA_FREE | | FILE_SIZE FILE_SIZE | | WASTED_SIZE WASTED_SIZE | | + +----------------+------------+------------+------------+------------+-----------+------------+-------------+ ----------------+------------+------------+------------+------------+-----------+------------+-------------+ | | sbtest sbtest/ /sbtest1 sbtest1 | | 973080 973080 | | 491.00 491.00 MiB MiB | | 239.00 239.00 MiB MiB | | 730.00 730.00 MiB MiB | | 3.00 3.00 MiB MiB | | 744.00 744.00 MiB MiB | | 14.00 14.00 MiB MiB | | | | sbtest sbtest/ /sbtest2 sbtest2 | | 986328 986328 | | 245.72 245.72 MiB MiB | | 15.52 15.52 MiB MiB | | 261.23 261.23 MiB MiB | | 5.00 5.00 MiB MiB | | 272.00 272.00 MiB MiB | | 10.77 10.77 MiB MiB | | + +----------------+------------+------------+------------+------------+-----------+------------+-------------+ ----------------+------------+------------+------------+------------+-----------+------------+-------------+ Copyright @ 2024 Oracle and/or its affiliates. 29
  37. MySQL InnoDB Primary Keys More on Secondary Index & Primary

    Key Copyright @ 2024 Oracle and/or its affiliates. 30
  38. Secondary Index & Primary Key We saw that the any

    Secondary Index includes the primary index at the right-most column (hidden). But this is not always the case... Let's check this example: Copyright @ 2024 Oracle and/or its affiliates. 31
  39. Secondary Index & Primary Key We saw that the any

    Secondary Index includes the primary index at the right-most column (hidden). But this is not always the case... Let's check this example: CREATE CREATE TABLE TABLE ` `t1 t1` ` ( ( ` `a a` ` int int NOT NOT NULL NULL, , ` `b b` ` int int NOT NOT NULL NULL, , ` `c c` ` int int NOT NOT NULL NULL, , ` `d d` ` int int NOT NOT NULL NULL, , ` `e e` ` int int NOT NOT NULL NULL, , ` `f f` ` varchar varchar( (10 10) ) DEFAULT DEFAULT 'aaa' 'aaa', , ` `inserted inserted` ` datetime datetime DEFAULT DEFAULT CURRENT_TIMESTAMP CURRENT_TIMESTAMP, , PRIMARY PRIMARY KEY KEY( (` `a a` `, ,` `b b` `, ,` `c c` `) ) ) ) ENGINE ENGINE= =InnoDB InnoDB; ; Copyright @ 2024 Oracle and/or its affiliates. 31
  40. Secondary Index & Primary Key (2) The table contains only

    the following two records: SELECT SELECT * * FROM FROM t1 t1; ; + +---+---+---+----+----+-----+---------------------+ ---+---+---+----+----+-----+---------------------+ | | a a | | b b | | c c | | d d | | e e | | f f | | inserted inserted | | + +---+---+---+----+----+-----+---------------------+ ---+---+---+----+----+-----+---------------------+ | | 1 1 | | 2 2 | | 3 3 | | 4 4 | | 5 5 | | abc abc | | 2024 2024- -02 02- -11 11 17 17: :37 37: :16 16 | | | | 7 7 | | 8 8 | | 9 9 | | 10 10 | | 11 11 | | def def | | 2024 2024- -02 02- -11 11 17 17: :37 37: :26 26 | | + +---+---+---+----+----+-----+---------------------+ ---+---+---+----+----+-----+---------------------+ Copyright @ 2024 Oracle and/or its affiliates. 32
  41. Secondary Index & Primary Key (3) This is an illutration

    of the table, mind the bold columns that are part of the Primary Key: t1 1 2 3 4 5 abc 2024-02-11 17:37:16 7 8 9 10 11 def 2024-02-11 17:37:26 a b c d e f inserted PRIMARY KEY (`a`, `b`, `c`) Copyright @ 2024 Oracle and/or its affiliates. 33
  42. Secondary Index & Primary Key (4) Now let's create a

    secondary index for the column f: ALTER ALTER TABLE TABLE t1 t1 ADD ADD INDEX INDEX f_idx f_idx( (f f) ); ; This key will then include the Primary Key as the right-most column(s) on the secondary index. Copyright @ 2024 Oracle and/or its affiliates. 34
  43. Secondary Index & Primary Key (5) This is the representation

    of that secondary index's entries: t1 PRIMARY KEY (`a`, `b`, `c`) KEY `f_idx` (`f`) abc 1 2 3 def 7 8 9 1 2 3 4 5 abc 2024-02-11 17:37:16 7 8 9 10 11 def 2024-02-11 17:37:26 a b c d e f inserted Copyright @ 2024 Oracle and/or its affiliates. 35
  44. Secondary Index & Primary Key (6) Let's verify this on

    the InnoDB page for that index: Copyright @ 2024 Oracle and/or its affiliates. 36
  45. Secondary Index & Primary Key (7) This is exaclty what

    we already knew about Secondary Indexes and the Primary Key.... but when we have a Primary Key or Part of a Primary Key included in a Secondary Index, only the eventual missing columns of the Primary Key will be added as right-most and hidden entries to the secondary index. Let's create a secondary index where the column b will be missing: ALTER ALTER TABLE TABLE t1 t1 ADD ADD INDEX INDEX sec_idx sec_idx ( (` `d d` `, ,` `c c` `, ,` `e e` `, ,` `a a` `) ); ; Copyright @ 2024 Oracle and/or its affiliates. 37
  46. Secondary Index & Primary Key (8) This is the illustration

    of such secondary index: t1 PRIMARY KEY (`a`, `b`, `c`) KEY `sec_idx` (`d`, `c`, `e`, `a`) 4 3 5 1 2 10 9 11 7 8 1 2 3 4 5 abc 2024-02-11 17:37:16 7 8 9 10 11 def 2024-02-11 17:37:26 a b c d e f inserted Copyright @ 2024 Oracle and/or its affiliates. 38
  47. Secondary Index & Primary Key (9) We saw that only

    the missing column b is added as right-most hidden column of the secondary index: Copyright @ 2024 Oracle and/or its affiliates. 39
  48. Secondary Index & Primary Key (10) Same for the second

    entry of the index: Copyright @ 2024 Oracle and/or its affiliates. 40
  49. Secondary Index & Primary Key (11) This is documented in

    InnoDB's' source code: Copyright @ 2024 Oracle and/or its affiliates. 41
  50. CREATE CREATE TABLE TABLE ` `t1 t1` ` ( (

    ` `a a` ` varchar varchar( (10 10) ) NOT NOT NULL NULL DEFAULT DEFAULT 'aaaaaaaaaa' 'aaaaaaaaaa', , ` `b b` ` varchar varchar( (10 10) ) NOT NOT NULL NULL DEFAULT DEFAULT 'bbbbbbbbbb' 'bbbbbbbbbb', , ` `c c` ` int int NOT NOT NULL NULL DEFAULT DEFAULT '1' '1', , ` `f f` ` varchar varchar( (10 10) ) DEFAULT DEFAULT NULL NULL, , PRIMARY PRIMARY KEY KEY ( (` `a a` `, ,` `b b` `, ,` `c c` `) ), , KEY KEY ` `sec_idx sec_idx` ` ( (` `c c` `, ,` `f f` `, ,` `a a` `( (2 2) )) ) ) ) ENGINE ENGINE= =InnoDB InnoDB SELECT SELECT * * FROM FROM t1 t1; ; + +------------+------------+---+-----+ ------------+------------+---+-----+ | | a a | | b b | | c c | | f f | | + +------------+------------+---+-----+ ------------+------------+---+-----+ | | aaaaaaaaaa aaaaaaaaaa | | bbbbbbbbbb bbbbbbbbbb | | 1 1 | | abc abc | | | | ccccccccc ccccccccc | | dddddddddd dddddddddd | | 2 2 | | def def | | + +------------+------------+---+-----+ ------------+------------+---+-----+ Even Further with Secondary Index & Primary Key But what will happen, if we just use a pre x part of the Primary Key in the secondary index? We can see that only 2 characters of the column a are used in the secondary index. Copyright @ 2024 Oracle and/or its affiliates. 42
  51. Even Further with Secondary Index & Primary Key If we

    check in the InnoDB page, we can notice that in-fact, the full column will also be added as the right-most hidden part of the secondary index: Copyright @ 2024 Oracle and/or its affiliates. 43
  52. InnoDB Primary Key (5) < > Copyright @ 2024 Oracle

    and/or its affiliates. My legacy application didn't define any Primary Key, adding one (auto_increment) breaks the application ! What can I do ? 45
  53. InnoDB Primary Key (5) < > Copyright @ 2024 Oracle

    and/or its affiliates. My legacy application didn't define any Primary Key, adding one (auto_increment) breaks the application ! What can I do ? Easy, just create a new invisible column and define it as Primary Key ! 45
  54. select select * * from from actors actors; ; +

    +----------------+-----+ ----------------+-----+ | | name name | | age age | | + +----------------+-----+ ----------------+-----+ | | Al Pacino Al Pacino | | 80 80 | | | | Robert De Niro Robert De Niro | | 77 77 | | | | Joe Pesci Joe Pesci | | 78 78 | | | | Sharon Stone Sharon Stone | | 63 63 | | | | Diane Keaton Diane Keaton | | 75 75 | | | | Talia Shire Talia Shire | | 74 74 | | + +----------------+-----+ ----------------+-----+ Invisible column as Primary Key Copyright @ 2024 Oracle and/or its affiliates. 46
  55. select select * * from from actors actors; ; +

    +----------------+-----+ ----------------+-----+ | | name name | | age age | | + +----------------+-----+ ----------------+-----+ | | Al Pacino Al Pacino | | 80 80 | | | | Robert De Niro Robert De Niro | | 77 77 | | | | Joe Pesci Joe Pesci | | 78 78 | | | | Sharon Stone Sharon Stone | | 63 63 | | | | Diane Keaton Diane Keaton | | 75 75 | | | | Talia Shire Talia Shire | | 74 74 | | + +----------------+-----+ ----------------+-----+ Do we have a Primary Key ? Invisible column as Primary Key Copyright @ 2024 Oracle and/or its affiliates. 46
  56. Invisible column as Primary Key (2) Let's nd out by

    listing all tables where the clustered index was generated (internal hidden key): select select i i. .table_id table_id, , t t. .name name from from information_schema information_schema. .innodb_indexes i innodb_indexes i join join information_schema information_schema. .innodb_tables t innodb_tables t on on ( (i i. .table_id table_id = = t t. .table_id table_id) ) where where i i. .name name= ='GEN_CLUST_INDEX' 'GEN_CLUST_INDEX'; ; + +----------+------------------+ ----------+------------------+ | | table_id table_id | | name name | | + +----------+------------------+ ----------+------------------+ | | 1293 1293 | | hollywood hollywood/ /actors actors | | + +----------+------------------+ ----------+------------------+ 1 1 row row in in set set ( (0.0211 0.0211 sec sec) ) Copyright @ 2024 Oracle and/or its affiliates. 47
  57. Invisible column as Primary Key (3) We can verify with

    the table's de nition: show show create create table table actors\G actors\G * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** * 1. 1. row row * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** * Table Table: actors : actors Create Create Table Table: : CREATE CREATE TABLE TABLE ` `actors actors` ` ( ( ` `name name` ` varchar varchar( (20 20) ) DEFAULT DEFAULT NULL NULL, , ` `age age` ` int int unsigned unsigned DEFAULT DEFAULT NULL NULL ) ) ENGINE ENGINE= =InnoDB InnoDB DEFAULT DEFAULT CHARSET CHARSET= =utf8mb4 utf8mb4 COLLATE COLLATE= =utf8mb4_0900_ai_ci utf8mb4_0900_ai_ci Copyright @ 2024 Oracle and/or its affiliates. 48
  58. Invisible column as Primary Key (3) We can verify with

    the table's de nition: show show create create table table actors\G actors\G * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** * 1. 1. row row * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** * Table Table: actors : actors Create Create Table Table: : CREATE CREATE TABLE TABLE ` `actors actors` ` ( ( ` `name name` ` varchar varchar( (20 20) ) DEFAULT DEFAULT NULL NULL, , ` `age age` ` int int unsigned unsigned DEFAULT DEFAULT NULL NULL ) ) ENGINE ENGINE= =InnoDB InnoDB DEFAULT DEFAULT CHARSET CHARSET= =utf8mb4 utf8mb4 COLLATE COLLATE= =utf8mb4_0900_ai_ci utf8mb4_0900_ai_ci Now let's add a hidden column as primary key: alter alter table table actors actors add add id id int int unsigned unsigned auto_increment auto_increment primary primary key key invisible invisible first first; ; Copyright @ 2024 Oracle and/or its affiliates. 48
  59. select select * * from from actors actors; ; +

    +----------------+-----+ ----------------+-----+ | | name name | | age age | | + +----------------+-----+ ----------------+-----+ | | Al Pacino Al Pacino | | 80 80 | | | | Robert De Niro Robert De Niro | | 77 77 | | | | Joe Pesci Joe Pesci | | 78 78 | | | | Sharon Stone Sharon Stone | | 63 63 | | | | Diane Keaton Diane Keaton | | 75 75 | | | | Talia Shire Talia Shire | | 74 74 | | + +----------------+-----+ ----------------+-----+ Invisible column as Primary Key (4) We can now test again our application's queries: Copyright @ 2024 Oracle and/or its affiliates. 49
  60. select select * * from from actors actors; ; +

    +----------------+-----+ ----------------+-----+ | | name name | | age age | | + +----------------+-----+ ----------------+-----+ | | Al Pacino Al Pacino | | 80 80 | | | | Robert De Niro Robert De Niro | | 77 77 | | | | Joe Pesci Joe Pesci | | 78 78 | | | | Sharon Stone Sharon Stone | | 63 63 | | | | Diane Keaton Diane Keaton | | 75 75 | | | | Talia Shire Talia Shire | | 74 74 | | + +----------------+-----+ ----------------+-----+ show show create create table table actors\G actors\G * ** ** ** ** ** ** ** ** ** ** ** ** * 1. 1. row row * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** * Table Table: actors : actors Create Create Table Table: : CREATE CREATE TABLE TABLE ` `actors actors` ` ( ( ` `id id` ` int int unsigned unsigned NOT NOT NULL NULL AUTO_INCREMENT AUTO_INCREMENT /*!80023 INVISIBLE */ /*!80023 INVISIBLE */, , ` `name name` ` varchar varchar( (20 20) ) DEFAULT DEFAULT NULL NULL, , ` `age age` ` int int unsigned unsigned DEFAULT DEFAULT NULL NULL, , PRIMARY PRIMARY KEY KEY ( (` `id id` `) ) ) ) ENGINE ENGINE= =InnoDB InnoDB AUTO_INCREMENT AUTO_INCREMENT= =7 7 DEFAULT DEFAULT CHARSET CHARSET= =utf8mb4 utf8mb4 COLLATE COLLATE= =utf8mb4_0900_ai_ci utf8mb4_0900_ai_ci Invisible column as Primary Key (4) We can now test again our application's queries: Copyright @ 2024 Oracle and/or its affiliates. 49
  61. Invisible column as Primary Key (5) < > Copyright @

    2024 Oracle and/or its affiliates. Great ! But what about inserts without specifying the columns ? 50
  62. Invisible column as Primary Key (5) < > insert insert

    into into actors actors values values ( ('James Caan' 'James Caan', , 81 81) ); ; Query OK Query OK, , 1 1 row row affected affected ( (0.0248 0.0248 sec sec) ) Copyright @ 2024 Oracle and/or its affiliates. Great ! But what about inserts without specifying the columns ? 50
  63. select select id id, , a a. .* * from

    from actors a actors a; ; + +----+----------------+-----+ ----+----------------+-----+ | | id id | | name name | | age age | | + +----+----------------+-----+ ----+----------------+-----+ | | 1 1 | | Al Pacino Al Pacino | | 80 80 | | | | 2 2 | | Robert De Niro Robert De Niro | | 77 77 | | | | 3 3 | | Joe Pesci Joe Pesci | | 78 78 | | | | 4 4 | | Sharon Stone Sharon Stone | | 63 63 | | | | 5 5 | | Diane Keaton Diane Keaton | | 75 75 | | | | 6 6 | | Talia Shire Talia Shire | | 74 74 | | | | 7 7 | | James Caan James Caan | | 81 81 | | + +----+----------------+-----+ ----+----------------+-----+ Invisible column as Primary Key (6) But if needed we have access to that PK id: Copyright @ 2024 Oracle and/or its affiliates. 51
  64. select select id id, , a a. .* * from

    from actors a actors a; ; + +----+----------------+-----+ ----+----------------+-----+ | | id id | | name name | | age age | | + +----+----------------+-----+ ----+----------------+-----+ | | 1 1 | | Al Pacino Al Pacino | | 80 80 | | | | 2 2 | | Robert De Niro Robert De Niro | | 77 77 | | | | 3 3 | | Joe Pesci Joe Pesci | | 78 78 | | | | 4 4 | | Sharon Stone Sharon Stone | | 63 63 | | | | 5 5 | | Diane Keaton Diane Keaton | | 75 75 | | | | 6 6 | | Talia Shire Talia Shire | | 74 74 | | | | 7 7 | | James Caan James Caan | | 81 81 | | + +----+----------------+-----+ ----+----------------+-----+ And this id is sequential, used as clustered index to store the data and externalized for replication ! Invisible column as Primary Key (6) But if needed we have access to that PK id: Copyright @ 2024 Oracle and/or its affiliates. 51
  65. Invisible column as Primary Key - automatic Since MySQL 8.0.30

    you can also enable GIPK mode ! Copyright @ 2024 Oracle and/or its affiliates. 53
  66. Invisible column as Primary Key - automatic Since MySQL 8.0.30

    you can also enable GIPK mode ! Generated Invisible Primary Key Copyright @ 2024 Oracle and/or its affiliates. 53
  67. Invisible column as Primary Key - automatic Since MySQL 8.0.30

    you can also enable GIPK mode ! Generated Invisible Primary Key GIPK mode is controlled by the sql_generate_invisible_primary_key server system variable. When MySQL is running in GIPK mode, a primary key is added to a table by the server, the column and key name is always my_row_id. Copyright @ 2024 Oracle and/or its affiliates. 53
  68. GIPK Mode - example SQL SQL > > SELECT SELECT

    @ @@sql_generate_invisible_primary_key @sql_generate_invisible_primary_key; ; + +--------------------------------------+ --------------------------------------+ | | @ @@sql_generate_invisible_primary_key @sql_generate_invisible_primary_key | | + +--------------------------------------+ --------------------------------------+ | | 1 1 | | + +--------------------------------------+ --------------------------------------+ SQL SQL > > CREATE CREATE TABLE TABLE sweden_mug sweden_mug ( (name name varchar varchar( (20 20) ), , beers beers int int unsigned unsigned) ); ; SQL SQL > > INSERT INSERT INTO INTO sweden_mug sweden_mug VALUES VALUES ( ('Ted' 'Ted', , 5 5) ), , ( ('lefred' 'lefred', ,1 1) ); ; SQL SQL > > SELECT SELECT * * FROM FROM sweden_mug sweden_mug; ; + +--------+-------+ --------+-------+ | | name name | | beers beers | | + +--------+-------+ --------+-------+ | | Ted Ted | | 5 5 | | | | lefred lefred | | 1 1 | | + +--------+-------+ --------+-------+ 2 2 rows rows in in set set ( (0.0002 0.0002 sec sec) ) Copyright @ 2024 Oracle and/or its affiliates. 54
  69. GIPK Mode - example (2) SQL SQL > > SHOW

    SHOW CREATE CREATE TABLE TABLE sweden_mug\G sweden_mug\G * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** * 1. 1. row row * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** * Table Table: sweden_mug : sweden_mug Create Create Table Table: : CREATE CREATE TABLE TABLE ` `sweden_mug sweden_mug` ` ( ( ` `my_row_id my_row_id` ` bigint bigint unsigned unsigned NOT NOT NULL NULL AUTO_INCREMENT AUTO_INCREMENT /*!80023 INVISIBLE */ /*!80023 INVISIBLE */, , ` `name name` ` varchar varchar( (20 20) ) DEFAULT DEFAULT NULL NULL, , ` `beers beers` ` int int unsigned unsigned DEFAULT DEFAULT NULL NULL, , PRIMARY PRIMARY KEY KEY ( (` `my_row_id my_row_id` `) ) ) ) ENGINE ENGINE= =InnoDB InnoDB AUTO_INCREMENT AUTO_INCREMENT= =3 3 DEFAULT DEFAULT CHARSET CHARSET= =utf8mb4 utf8mb4 COLLATE COLLATE= =utf8mb4_0900_ai_ci utf8mb4_0900_ai_ci Copyright @ 2024 Oracle and/or its affiliates. 55
  70. GIPK Mode - example (2) SQL SQL > > SHOW

    SHOW CREATE CREATE TABLE TABLE sweden_mug\G sweden_mug\G * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** * 1. 1. row row * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** * Table Table: sweden_mug : sweden_mug Create Create Table Table: : CREATE CREATE TABLE TABLE ` `sweden_mug sweden_mug` ` ( ( ` `my_row_id my_row_id` ` bigint bigint unsigned unsigned NOT NOT NULL NULL AUTO_INCREMENT AUTO_INCREMENT /*!80023 INVISIBLE */ /*!80023 INVISIBLE */, , ` `name name` ` varchar varchar( (20 20) ) DEFAULT DEFAULT NULL NULL, , ` `beers beers` ` int int unsigned unsigned DEFAULT DEFAULT NULL NULL, , PRIMARY PRIMARY KEY KEY ( (` `my_row_id my_row_id` `) ) ) ) ENGINE ENGINE= =InnoDB InnoDB AUTO_INCREMENT AUTO_INCREMENT= =3 3 DEFAULT DEFAULT CHARSET CHARSET= =utf8mb4 utf8mb4 COLLATE COLLATE= =utf8mb4_0900_ai_ci utf8mb4_0900_ai_ci SQL SQL > > SELECT SELECT * *, , my_row_id my_row_id FROM FROM sweden_mug sweden_mug; ; + +--------+-------+-----------+ --------+-------+-----------+ | | name name | | beers beers | | my_row_id my_row_id | | + +--------+-------+-----------+ --------+-------+-----------+ | | ted ted | | 5 5 | | 1 1 | | | | lefred lefred | | 1 1 | | 2 2 | | + +--------+-------+-----------+ --------+-------+-----------+ Copyright @ 2024 Oracle and/or its affiliates. 55
  71. GIPK Mode - example (3) The information is also part

    of Information_Schema: SQL SQL > > SELECT SELECT COLUMN_NAME COLUMN_NAME, , ORDINAL_POSITION ORDINAL_POSITION, , DATA_TYPE DATA_TYPE, , COLUMN_KEY COLUMN_KEY FROM FROM INFORMATION_SCHEMA INFORMATION_SCHEMA. .COLUMNS COLUMNS WHERE WHERE TABLE_NAME TABLE_NAME = = "sweden_mug" "sweden_mug"; ; + +-------------+------------------+-----------+------------+ -------------+------------------+-----------+------------+ | | COLUMN_NAME COLUMN_NAME | | ORDINAL_POSITION ORDINAL_POSITION | | DATA_TYPE DATA_TYPE | | COLUMN_KEY COLUMN_KEY | | + +-------------+------------------+-----------+------------+ -------------+------------------+-----------+------------+ | | beers beers | | 3 3 | | int int | | | | | | my_row_id my_row_id | | 1 1 | | bigint bigint | | PRI PRI | | | | name name | | 2 2 | | varchar varchar | | | | + +-------------+------------------+-----------+------------+ -------------+------------------+-----------+------------+ Copyright @ 2024 Oracle and/or its affiliates. 56
  72. GIPK Mode - example (4) < > Copyright @ 2024

    Oracle and/or its affiliates. Oh nice ! But I have this legacy application that breaks if there is an extra column part of information_schema. It seems to use it to generate forms! 57
  73. GIPK Mode - example (4) < > Copyright @ 2024

    Oracle and/or its affiliates. Oh nice ! But I have this legacy application that breaks if there is an extra column part of information_schema. It seems to use it to generate forms! No worry, you can use one the variable with the longest name (a part of some pfs related ones): show_gipk_in_create_table_and_information_schema 57
  74. GIPK Mode - example (5) SQL SQL > > SET

    SET show_gipk_in_create_table_and_information_schema show_gipk_in_create_table_and_information_schema = = 0 0; ; SQL SQL > > SELECT SELECT COLUMN_NAME COLUMN_NAME, , ORDINAL_POSITION ORDINAL_POSITION, , DATA_TYPE DATA_TYPE, , COLUMN_KEY COLUMN_KEY FROM FROM INFORMATION_SCHEMA INFORMATION_SCHEMA. .COLUMNS COLUMNS WHERE WHERE TABLE_NAME TABLE_NAME = = "sweden_mug" "sweden_mug"; ; + +-------------+------------------+-----------+------------+ -------------+------------------+-----------+------------+ | | COLUMN_NAME COLUMN_NAME | | ORDINAL_POSITION ORDINAL_POSITION | | DATA_TYPE DATA_TYPE | | COLUMN_KEY COLUMN_KEY | | + +-------------+------------------+-----------+------------+ -------------+------------------+-----------+------------+ | | beers beers | | 3 3 | | int int | | | | | | name name | | 2 2 | | varchar varchar | | | | + +-------------+------------------+-----------+------------+ -------------+------------------+-----------+------------+ Copyright @ 2024 Oracle and/or its affiliates. 58
  75. GIPK Mode - example (6) SQL SQL> > select select

    @ @@show_gipk_in_create_table_and_information_schema @show_gipk_in_create_table_and_information_schema; ; + +----------------------------------------------------+ ----------------------------------------------------+ | | @ @@show_gipk_in_create_table_and_information_schema @show_gipk_in_create_table_and_information_schema | | + +----------------------------------------------------+ ----------------------------------------------------+ | | 0 0 | | + +----------------------------------------------------+ ----------------------------------------------------+ SQL SQL > > SHOW SHOW CREATE CREATE TABLE TABLE sweden_mug\G sweden_mug\G * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** * 1. 1. row row * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** * Table Table: sweden_mug : sweden_mug Create Create Table Table: : CREATE CREATE TABLE TABLE ` `sweden_mug sweden_mug` ` ( ( ` `name name` ` varchar varchar( (20 20) ) DEFAULT DEFAULT NULL NULL, , ` `beers beers` ` int int unsigned unsigned DEFAULT DEFAULT NULL NULL ) ) ENGINE ENGINE= =InnoDB InnoDB DEFAULT DEFAULT CHARSET CHARSET= =utf8mb4 utf8mb4 COLLATE COLLATE= =utf8mb4_0900_ai_ci utf8mb4_0900_ai_ci Copyright @ 2024 Oracle and/or its affiliates. 59
  76. MySQL InnoDB Primary Keys Keep an eye to your Auto-Increment

    values Copyright @ 2024 Oracle and/or its affiliates. 60
  77. Auto-Increment Value Exhaustion When using Auto-Increment columns for Primary Key,

    usually the type is an integer. But MySQL supports many di erent type of integers. They way the auto-increment column is de ned also de nes the maximum value that can be inserted, meaning the maximum records allowed in the table. Copyright @ 2024 Oracle and/or its affiliates. 61
  78. Auto-Increment Value Exhaustion When using Auto-Increment columns for Primary Key,

    usually the type is an integer. But MySQL supports many di erent type of integers. They way the auto-increment column is de ned also de nes the maximum value that can be inserted, meaning the maximum records allowed in the table. If the max is reached, it won't be possible to insert any new row into the table: SQL SQL> > INSERT INSERT INTO INTO t4 t4 ( (id id) ) VALUES VALUES ( (0 0) ); ; ERROR: ERROR: 1062 1062 ( (23000 23000) ): : Duplicate Duplicate entry entry '127' '127' for for key key 't4.PRIMARY' 't4.PRIMARY' Copyright @ 2024 Oracle and/or its affiliates. 61
  79. Auto-Increment Value Exhaustion (2) It's the MySQL DBA's responsability to

    prevent this to happen. Using the following query, you can have an overview of the auto-increment columns: SELECT SELECT TABLE_SCHEMA TABLE_SCHEMA, , TABLE_NAME TABLE_NAME, , COLUMN_NAME COLUMN_NAME, , DATA_TYPE DATA_TYPE, , COLUMN_TYPE COLUMN_TYPE, , IF IF( ( LOCATE LOCATE( ('unsigned' 'unsigned', , COLUMN_TYPE COLUMN_TYPE) ) > > 0 0, , 1 1, , 0 0) ) AS AS IS_UNSIGNED IS_UNSIGNED, , ( ( CASE CASE DATA_TYPE DATA_TYPE WHEN WHEN 'tinyint' 'tinyint' THEN THEN 255 255 WHEN WHEN 'smallint' 'smallint' THEN THEN 65535 65535 WHEN WHEN 'mediumint' 'mediumint' THEN THEN 16777215 16777215 WHEN WHEN 'int' 'int' THEN THEN 4294967295 4294967295 WHEN WHEN 'bigint' 'bigint' THEN THEN 18446744073709551615 18446744073709551615 END END >> >> IF IF( (LOCATE LOCATE( ('unsigned' 'unsigned', , COLUMN_TYPE COLUMN_TYPE) ) > > 0 0, , 0 0, , 1 1) )) ) AS AS MAX_VALUE MAX_VALUE, , AUTO_INCREMENT AUTO_INCREMENT, , CONCAT CONCAT( (ROUND ROUND( ( AUTO_INCREMENT AUTO_INCREMENT / / ( ( CASE CASE DATA_TYPE DATA_TYPE WHEN WHEN 'tinyint' 'tinyint' THEN THEN 255 255 WHEN WHEN 'smallint' 'smallint' THEN THEN 65535 65535 WHEN WHEN 'mediumint' 'mediumint' THEN THEN 16777215 16777215 WHEN WHEN 'int' 'int' THEN THEN 4294967295 4294967295 WHEN WHEN 'bigint' 'bigint' THEN THEN 18446744073709551615 18446744073709551615 END END >> >> IF IF( (LOCATE LOCATE( ('unsigned' 'unsigned', , COLUMN_TYPE COLUMN_TYPE) ) > > 0 0, , 0 0, , 1 1) )) )* *100 100) ), , '%' '%') ) AS AS AUTO_INCREMENT_RATIO AUTO_INCREMENT_RATIO FROM FROM INFORMATION_SCHEMA INFORMATION_SCHEMA. .COLUMNS COLUMNS INNER INNER JOIN JOIN INFORMATION_SCHEMA INFORMATION_SCHEMA. .TABLES TABLES USING USING ( (TABLE_SCHEMA TABLE_SCHEMA, , TABLE_NAME TABLE_NAME) ) WHERE WHERE TABLE_SCHEMA TABLE_SCHEMA NOT NOT IN IN ( ('mysql' 'mysql', , 'INFORMATION_SCHEMA' 'INFORMATION_SCHEMA', , 'performance_schema' 'performance_schema') ) AND AND EXTRA EXTRA= ='auto_increment' 'auto_increment' ORDER ORDER BY BY CAST CAST( (AUTO_INCREMENT_RATIO AUTO_INCREMENT_RATIO AS AS SIGNED SIGNED INTEGER INTEGER) ); ; Copyright @ 2024 Oracle and/or its affiliates. 62
  80. Auto-Increment Value Exhaustion - Sample * ** ** ** **

    ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** * 1. 1. row row * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** * TABLE_SCHEMA: test TABLE_SCHEMA: test TABLE_NAME: t4 TABLE_NAME: t4 COLUMN_NAME: id COLUMN_NAME: id DATA_TYPE: DATA_TYPE: tinyint tinyint COLUMN_TYPE: COLUMN_TYPE: tinyint tinyint IS_UNSIGNED: IS_UNSIGNED: 0 0 MAX_VALUE: MAX_VALUE: 127 127 AUTO_INCREMENT AUTO_INCREMENT: : 124 124 AUTO_INCREMENT_RATIO: AUTO_INCREMENT_RATIO: 98 98% % Copyright @ 2024 Oracle and/or its affiliates. 63
  81. Auto-Increment Value Exhaustion (3) The best is to de ne

    it as BIGINT UNSIGNED if you expect a lot of record: CREATE CREATE TABLE TABLE scott scott ( ( id id BIGINT BIGINT UNSIGNED UNSIGNED AUTO_INCREMENT AUTO_INCREMENT PRIMARY PRIMARY KEY KEY, , inserted inserted DATETIME DATETIME DEFAULT DEFAULT current_timestamp current_timestamp) ); ; Copyright @ 2024 Oracle and/or its affiliates. 64
  82. Auto-Increment Value Exhaustion (3) The best is to de ne

    it as BIGINT UNSIGNED if you expect a lot of record: CREATE CREATE TABLE TABLE scott scott ( ( id id BIGINT BIGINT UNSIGNED UNSIGNED AUTO_INCREMENT AUTO_INCREMENT PRIMARY PRIMARY KEY KEY, , inserted inserted DATETIME DATETIME DEFAULT DEFAULT current_timestamp current_timestamp) ); ; SQL SQL> > SHOW SHOW CREATE CREATE TABLE TABLE scott\G scott\G * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** * 1. 1. row row * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** * Table Table: scott : scott Create Create Table Table: : CREATE CREATE TABLE TABLE ` `scott scott` ` ( ( ` `id id` ` bigint bigint unsigned unsigned NOT NOT NULL NULL AUTO_INCREMENT AUTO_INCREMENT, , ` `inserted inserted` ` datetime datetime DEFAULT DEFAULT CURRENT_TIMESTAMP CURRENT_TIMESTAMP, , PRIMARY PRIMARY KEY KEY ( (` `id id` `) ) ) ) ENGINE ENGINE= =InnoDB InnoDB DEFAULT DEFAULT CHARSET CHARSET= =utf8mb4 utf8mb4 COLLATE COLLATE= =utf8mb4_0900_ai_ci utf8mb4_0900_ai_ci Copyright @ 2024 Oracle and/or its affiliates. 64
  83. What about SERIAL ? You can also de ne your

    auto-increment column using the keyword SERIAL: CREATE CREATE TABLE TABLE scott scott ( ( id id SERIAL SERIAL PRIMARY PRIMARY KEY KEY, , inserted inserted DATETIME DATETIME DEFAULT DEFAULT current_timestamp current_timestamp) ); ; Copyright @ 2024 Oracle and/or its affiliates. 65
  84. What about SERIAL ? You can also de ne your

    auto-increment column using the keyword SERIAL: CREATE CREATE TABLE TABLE scott scott ( ( id id SERIAL SERIAL PRIMARY PRIMARY KEY KEY, , inserted inserted DATETIME DATETIME DEFAULT DEFAULT current_timestamp current_timestamp) ); ; And we can see that MySQL made the right transformation: SQL SQL> > SHOW SHOW CREATE CREATE TABLE TABLE scott\G scott\G [ [. .. .. .] ] Create Create Table Table: : CREATE CREATE TABLE TABLE ` `scott scott` ` ( ( ` `id id` ` bigint bigint unsigned unsigned NOT NOT NULL NULL AUTO_INCREMENT AUTO_INCREMENT, , ` `inserted inserted` ` datetime datetime DEFAULT DEFAULT CURRENT_TIMESTAMP CURRENT_TIMESTAMP, , PRIMARY PRIMARY KEY KEY ( (` `id id` `) ), , UNIQUE UNIQUE KEY KEY ` `id id` ` ( (` `id id` `) ) ) ) ENGINE ENGINE= =InnoDB InnoDB DEFAULT DEFAULT CHARSET CHARSET= =utf8mb4 utf8mb4 COLLATE COLLATE= =utf8mb4_0900_ai_ci utf8mb4_0900_ai_ci Copyright @ 2024 Oracle and/or its affiliates. 65
  85. What about SERIAL ? (2) SERIAL is an alias of

    BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE but as InnoDB requires that the AUTO_INCREMENT column MUST be part of the Primary Key. This is valid: CREATE CREATE TABLE TABLE scott scott ( (id id serial serial, , name name varchar varchar( (10 10) ), , inserted inserted DATETIME DATETIME default default current_timestamp current_timestamp, , primary primary key key( (id id, , name name) )) ); ; Copyright @ 2024 Oracle and/or its affiliates. 66
  86. What about SERIAL ? (3) SQL SQL> > SHOW SHOW

    CREATE CREATE TABLE TABLE scott\G scott\G * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** * 1. 1. row row * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** * Table Table: scott : scott Create Create Table Table: : CREATE CREATE TABLE TABLE ` `scott scott` ` ( ( ` `id id` ` bigint bigint unsigned unsigned NOT NOT NULL NULL AUTO_INCREMENT AUTO_INCREMENT, , ` `name name` ` varchar varchar( (10 10) ) NOT NOT NULL NULL, , ` `inserted inserted` ` datetime datetime DEFAULT DEFAULT CURRENT_TIMESTAMP CURRENT_TIMESTAMP, , PRIMARY PRIMARY KEY KEY ( (` `id id` `, ,` `name name` `) ), , UNIQUE UNIQUE KEY KEY ` `id id` ` ( (` `id id` `) ) ) ) ENGINE ENGINE= =InnoDB InnoDB DEFAULT DEFAULT CHARSET CHARSET= =utf8mb4 utf8mb4 COLLATE COLLATE= =utf8mb4_0900_ai_ci utf8mb4_0900_ai_ci 1 1 row row in in set set ( (0.0017 0.0017 sec sec) ) Copyright @ 2024 Oracle and/or its affiliates. 67
  87. Why is it bad to replicate without PK ? Missing

    Primary Keys when using Row Based Replication (RBR) is the main cause of replication lag. If one runs DML on a table that has no indexes, a full table scan is done. With RBR, the replica will need to scan the full table for each row changed ! Copyright @ 2024 Oracle and/or its affiliates. 69
  88. Why is it bad to replicate without PK ? Example

    SQL SQL> > CREATE CREATE TABLE TABLE ` `nopk nopk` ` ( ( ` `i i` ` int int NOT NOT NULL NULL, , ` `name name` ` varchar varchar( (20 20) ) DEFAULT DEFAULT NULL NULL, , ` `inserted inserted` ` timestamp timestamp NULL NULL DEFAULT DEFAULT CURRENT_TIMESTAMP CURRENT_TIMESTAMP, , ` `g g` ` int int NOT NOT NULL NULL ) ) ENGINE ENGINE= =InnoDB InnoDB DEFAULT DEFAULT CHARSET CHARSET= =utf8mb4 utf8mb4 COLLATE COLLATE= =utf8mb4_0900_ai_ci utf8mb4_0900_ai_ci As you can see, we don't have any Primary Key de ned. Copyright @ 2024 Oracle and/or its affiliates. 70
  89. Why is it bad to replicate without PK ? Example

    (2) SQL SQL> > select select * * from from nopk nopk; ; + +-----+----------+---------------------+---+ -----+----------+---------------------+---+ | | i i | | name name | | inserted inserted | | g g | | + +-----+----------+---------------------+---+ -----+----------+---------------------+---+ | | 100 100 | | 9 9A2B4503 A2B4503 | | 2024 2024- -01 01- -22 22 11 11: :25 25: :45 45 | | 0 0 | | | | 20 20 | | 9 9B004592 B004592 | | 2024 2024- -01 01- -22 22 11 11: :25 25: :46 46 | | 8 8 | | | | 89 89 | | 9 9C37EA4E C37EA4E | | 2024 2024- -01 01- -22 22 11 11: :25 25: :48 48 | | 6 6 | | | | 85 85 | | 9 9CA3C011 CA3C011 | | 2024 2024- -01 01- -22 22 11 11: :25 25: :49 49 | | 0 0 | | | | 98 98 | | 9 9D084BB1 D084BB1 | | 2024 2024- -01 01- -22 22 11 11: :25 25: :50 50 | | 5 5 | | | | 73 73 | | 9 9D739EDB D739EDB | | 2024 2024- -01 01- -22 22 11 11: :25 25: :50 50 | | 0 0 | | | | 90 90 | | 9 9DD30035 DD30035 | | 2024 2024- -01 01- -22 22 11 11: :25 25: :51 51 | | 3 3 | | | | 31 31 | | 9 9E39D1E3 E39D1E3 | | 2024 2024- -01 01- -22 22 11 11: :25 25: :52 52 | | 3 3 | | | | 75 75 | | 9 9EA8DE8E EA8DE8E | | 2024 2024- -01 01- -22 22 11 11: :25 25: :52 52 | | 6 6 | | | | 19 19 | | A0C11A3A A0C11A3A | | 2024 2024- -01 01- -22 22 11 11: :25 25: :56 56 | | 8 8 | | + +-----+----------+---------------------+---+ -----+----------+---------------------+---+ Copyright @ 2024 Oracle and/or its affiliates. 71
  90. Why is it bad to replicate without PK ? Example

    (3) We will now delete some records and check what's happening. First we check the Query Execution Plan: SQL SQL> > explain explain delete delete from from nopk nopk where where g g < < 5 5\G \G * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** * 1. 1. row row * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** * id: id: 1 1 select_type: select_type: DELETE DELETE table table: nopk : nopk partitions: partitions: NULL NULL type type: : ALL ALL possible_keys: possible_keys: NULL NULL key key: : NULL NULL key_len: key_len: NULL NULL ref: ref: NULL NULL rows rows: : 10 10 filtered: filtered: 100 100 Extra: Extra: Using Using where where Copyright @ 2024 Oracle and/or its affiliates. 72
  91. Why is it bad to replicate without PK ? Example

    (4) SQL SQL> > flush flush status status; ; SQL SQL> > delete delete from from nopk nopk where where g g < < 5 5\G \G Query OK Query OK, , 5 5 rows rows affected affected ( (0.0080 0.0080 sec sec) ) SQL SQL> > show show status status like like 'Handler_%' 'Handler_%'; ; + +----------------------------+-------+ ----------------------------+-------+ | | Variable_name Variable_name | | Value Value | | + +----------------------------+-------+ ----------------------------+-------+ | | Handler_commit Handler_commit | | 2 2 | | | | Handler_delete Handler_delete | | 5 5 | | . .. .. . | | Handler_read_rnd_next Handler_read_rnd_next | | 11 11 | | . .. .. . Copyright @ 2024 Oracle and/or its affiliates. 73
  92. #240122 11:34:46 server id 1 end_log_pos 11663 CRC32 0x844fe80a Delete_rows:

    table id 652 flags: STMT_END_F ### DELETE FROM `smug`.`nopk` ### WHERE ### @1=100 /* INT meta=0 nullable=0 is_null=0 */ ### @2='9A2B4503' /* VARSTRING(80) meta=80 nullable=1 is_null=0 */ ### @3=1705919145 /* TIMESTAMP(0) meta=0 nullable=1 is_null=0 */ ### @4=0 /* INT meta=0 nullable=0 is_null=0 */ ### DELETE FROM `smug`.`nopk` ### WHERE ### @1=85 /* INT meta=0 nullable=0 is_null=0 */ ### @2='9CA3C011' /* VARSTRING(80) meta=80 nullable=1 is_null=0 */ ### @3=1705919149 /* TIMESTAMP(0) meta=0 nullable=1 is_null=0 */ ### @4=0 /* INT meta=0 nullable=0 is_null=0 */ ### DELETE FROM `smug`.`nopk` ### WHERE ### @1=73 /* INT meta=0 nullable=0 is_null=0 */ ### @2='9D739EDB' /* VARSTRING(80) meta=80 nullable=1 is_null=0 */ ### @3=1705919150 /* TIMESTAMP(0) meta=0 nullable=1 is_null=0 */ ### @4=0 /* INT meta=0 nullable=0 is_null=0 */ ### DELETE FROM `smug`.`nopk` ### WHERE ### @1=90 /* INT meta=0 nullable=0 is_null=0 */ ### @2='9DD30035' /* VARSTRING(80) meta=80 nullable=1 is_null=0 */ ### @3=1705919151 /* TIMESTAMP(0) meta=0 nullable=1 is_null=0 */ ### @4=3 /* INT meta=0 nullable=0 is_null=0 */ ### DELETE FROM `smug`.`nopk` ### WHERE ### @1=31 /* INT meta=0 nullable=0 is_null=0 */ ### @2='9E39D1E3' /* VARSTRING(80) meta=80 nullable=1 is_null=0 */ ### @3=1705919152 /* TIMESTAMP(0) meta=0 nullable=1 is_null=0 */ ### @4=3 /* INT meta=0 nullable=0 is_null=0 */ # at 11663 #240122 11:34:46 server id 1 end_log_pos 11694 CRC32 0xaf0c107f Xid = 139 COMMIT/*!*/; Copyright @ 2024 Oracle and/or its affiliates. 74
  93. Replication and GIPK Mode GIPK mode supports row-based replication of

    CREATE TABLE ... SELECT; the information wri en to the binary log for this statement in such cases includes the GIPK de nition, and thus is replicated correctly. Statement-based replication of CREATE TABLE ... SELECT is not supported in GIPK mode. It's also possible to de ne a table Primary Key Policy per replication channels using REQUIRE_TABLE_PRIMARY_KEY_CHECK. This can be useful for live migrations. See h ps://dev.mysql.com/doc/refman/8.3/en/change-replication-source-to.html Copyright @ 2024 Oracle and/or its affiliates. 75
  94. Primary Keys and High Availability When using Group Replication (MySQL

    InnoDB Cluster) every table that is to be replicated by the group must have a de ned primary key, or primary key equivalent where the equivalent is a non-null unique key. Such keys are required as a unique identi er for every row within a table, enabling the system to determine which transactions con ict by identifying exactly which rows each transaction has modi ed: the "certi cation" phase. Copyright @ 2024 Oracle and/or its affiliates. 76
  95. MySQL InnoDB Primary Keys MySQL Shell Dump & Load Copyright

    @ 2024 Oracle and/or its affiliates. 77
  96. mysqldump & GIPK When creating or importing dumps of MySQL

    instances in which GIPK mode is in use, it is possible to exclude generated invisible PK columns and values. The --skip-generated-invisible-primary-key option for mysqldump causes GIPK information to be excluded in the program's output. If you are importing a dump le that contains GIPK keys and values, you can also use -- skip-generated-invisible-primary-key. Copyright @ 2024 Oracle and/or its affiliates. 78
  97. mysqldump & GIPK When creating or importing dumps of MySQL

    instances in which GIPK mode is in use, it is possible to exclude generated invisible PK columns and values. The --skip-generated-invisible-primary-key option for mysqldump causes GIPK information to be excluded in the program's output. If you are importing a dump le that contains GIPK keys and values, you can also use -- skip-generated-invisible-primary-key. Avoid the use of mysqldump, and use MySQL Shell ! Copyright @ 2024 Oracle and/or its affiliates. 78
  98. MySQL Shell Dump Utility & GIPK create_invisible_pks compatibility option of

    MySQL Shell Dump Utility adds a ag in the dump metadata to notify MySQL Shell Load Utility to add primary keys in invisible columns, for each table that does not contain a primary key. The dump data is unchanged by this modi cation, as the tables do not contain the invisible columns until they have been processed by the dump loading utility. JS JS> > util util. .dumpSchemas dumpSchemas( ([ ["smug" "smug"] ], , "/tmp/smug" "/tmp/smug", , { {compatibility compatibility: : [ ['create_invisible_pks' 'create_invisible_pks'] ]} }) ) ... .... . NOTE NOTE: : Table Table ` `smug smug` `. .` `nopk nopk` ` does not have a Primary Key does not have a Primary Key, , this this will be will be fixed when the dump is loaded fixed when the dump is loaded ... ... Copyright @ 2024 Oracle and/or its affiliates. 79
  99. MySQL Shell Load Utility & GIPK MySQL Shell's load utility

    option createInvisiblePKs uses the server's GIPK mode to generate invisible primary keys for tables which do not have primary keys. Requires server 8.0.24 or newer. JS JS> > util util. .loadDump loadDump( ("/tmp/smug" "/tmp/smug", , { {createInvisiblePKs createInvisiblePKs: : true true} }) ) Copyright @ 2024 Oracle and/or its affiliates. 80
  100. MySQL Shell Copy Utility & GIPK All the copy utilities

    from MySQL Shell support the compatibilty option to create invisible Primary Keys: util.copyInstance() util.copySchemas() util.copyTables() JS JS> > util util. .copySchemas copySchemas( ([ ["smug" "smug"] ], ,"mysql://smug@smug-host2/" "mysql://smug@smug-host2/", , { {compatibility compatibility: : [ ['create_invisible_pks' 'create_invisible_pks'] ]} }) ) Copyright @ 2024 Oracle and/or its affiliates. 81
  101. MySQL InnoDB Primary Keys What about UUIDs ? Copyright @

    2024 Oracle and/or its affiliates. 82
  102. . generally they are completely random and cause clustered index

    re-banlancing . they are included in each secondary indexes (consuming disk and memory) InnoDB Primary Key - What about UUID ? There are 2 major problems with UUID's as Primary Key: Copyright @ 2024 Oracle and/or its affiliates. 83
  103. InnoDB Primary Key - What about UUID ? (2) Example:

    SQL> CREATE TABLE smug ( uuid VARCHAR(36) DEFAULT (UUID()) PRIMARY KEY, name VARCHAR(20), beers int unsigned); SQL> SELECT * FROM smug; +--------------------------------------+---------+-------+ | uuid | name | beers | +--------------------------------------+---------+-------+ | c15d29cb-b924-11ee-9aaa-f4a475a3749c | Ted | 0 | | c15d2dbd-b924-11ee-9aaa-f4a475a3749c | lefred | 1 | | c15d2ed4-b924-11ee-9aaa-f4a475a3749c | Staffan | 0 | | c15d2fe6-b924-11ee-9aaa-f4a475a3749c | Lenka | 1 | +--------------------------------------+---------+-------+ Copyright @ 2024 Oracle and/or its affiliates. 84
  104. InnoDB Primary Key - What about UUID ? (3) Let's

    insert 2 new records: SQL> INSERT INTO smug (name, beers) VALUES ("Carsten",1), ("Scott",5); Query OK, 2 rows affected (0.0069 sec) Copyright @ 2024 Oracle and/or its affiliates. 85
  105. InnoDB Primary Key - What about UUID ? (3) Let's

    insert 2 new records: SQL> INSERT INTO smug (name, beers) VALUES ("Carsten",1), ("Scott",5); Query OK, 2 rows affected (0.0069 sec) SQL> SELECT * FROM smug; +--------------------------------------+---------+-------+ | uuid | name | beers | +--------------------------------------+---------+-------+ | 4d16b6ef-b925-11ee-9aaa-f4a475a3749c | Carsten | 1 | | 4d16bd15-b925-11ee-9aaa-f4a475a3749c | Scott | 5 | | c15d29cb-b924-11ee-9aaa-f4a475a3749c | Ted | 0 | | c15d2dbd-b924-11ee-9aaa-f4a475a3749c | lefred | 1 | | c15d2ed4-b924-11ee-9aaa-f4a475a3749c | Staffan | 0 | | c15d2fe6-b924-11ee-9aaa-f4a475a3749c | Lenka | 1 | +--------------------------------------+---------+-------+ Copyright @ 2024 Oracle and/or its affiliates. 85
  106. InnoDB Primary Key - What about UUID ? (4) OUPS

    ! We have rebalanced the clustered index ! What does that mean again ?? Copyright @ 2024 Oracle and/or its affiliates. 86
  107. InnoDB Primary Key - What about UUID ? (4) OUPS

    ! We have rebalanced the clustered index ! What does that mean again ?? Let me try to explain this with this high level and simpli ed example: Copyright @ 2024 Oracle and/or its affiliates. 86
  108. Let's imagine one InnoDB Page can store 4 records (this

    is just a ction), and we have inserted some records using a random Primary Key: InnoDB Primary Key - What about UUID ? (5) OUPS ! We have rebalanced the clustered index ! Copyright @ 2024 Oracle and/or its affiliates. 87
  109. Let's imagine one InnoDB Page can store 4 records (this

    is just a ction), and we have inserted some records using a random Primary Key: And now we insert a new record and the Primary Key is AA: All pages were modi ed to “rebalance” the clustered index ! Imagine if this was a 4TB table !! InnoDB Primary Key - What about UUID ? (5) OUPS ! We have rebalanced the clustered index ! Copyright @ 2024 Oracle and/or its affiliates. 87
  110. This an overview of inserts into a table using random

    UUIDs as Primary Key: InnoDB Primary Key - What about UUID ? (6) Copyright @ 2024 Oracle and/or its affiliates. 88
  111. InnoDB Primary Key - What about UUID ? (7) And

    just for info, each entry in the Primary Key Index could take up to 146 bytes(*): SQL SQL > > EXPLAIN EXPLAIN SELECT SELECT * * FROM FROM smug smug WHERE WHERE uuid uuid= ='c15d2dbd-b924-11ee-9aaa-f4a475a3749c' 'c15d2dbd-b924-11ee-9aaa-f4a475a3749c'\G \G * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** * 1. 1. row row * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** * id: id: 1 1 select_type: select_type: SIMPLE SIMPLE table table: smug : smug partitions: partitions: NULL NULL type type: const : const possible_keys: possible_keys: PRIMARY PRIMARY key key: : PRIMARY PRIMARY key_len: key_len: 146 146 ref: const ref: const rows rows: : 1 1 filtered: filtered: 100 100 Extra: Extra: NULL NULL (*) worse case when using characters using 4 bytes each (uft8mb4) Copyright @ 2024 Oracle and/or its affiliates. 89
  112. InnoDB Primary Key - What about UUID ? (8) Recommended

    solution . use a smaller datatype: BINARY(16) . store the UUID sequentially: UUID_TO_BIN(..., swap_ ag) The time-low and time-high parts (the rst and third groups of hexadecimal digits, respectively) are swapped. Copyright @ 2024 Oracle and/or its affiliates. 90
  113. InnoDB Primary Key - What about UUID ? (9) Recommended

    solution - example SQL> CREATE TABLE smug2 ( uuid BINARY(16) DEFAULT (UUID_TO_BIN(UUID(), 1)) PRIMARY KEY, name VARCHAR(20), beers int unsigned); SQL> SELECT * FROM smug2; +------------------------------------+--------+-------+ | uuid | name | beers | +------------------------------------+--------+-------+ | 0x11EEB92771438C999AAAF4A475A3749C | Ted | 0 | | 0x11EEB9277143902F9AAAF4A475A3749C | lefred | 1 | +------------------------------------+--------+-------+ Copyright @ 2024 Oracle and/or its affiliates. 91
  114. InnoDB Primary Key - What about UUID ? (9) Recommended

    solution - example SQL> CREATE TABLE smug2 ( uuid BINARY(16) DEFAULT (UUID_TO_BIN(UUID(), 1)) PRIMARY KEY, name VARCHAR(20), beers int unsigned); SQL> SELECT * FROM smug2; +------------------------------------+--------+-------+ | uuid | name | beers | +------------------------------------+--------+-------+ | 0x11EEB92771438C999AAAF4A475A3749C | Ted | 0 | | 0x11EEB9277143902F9AAAF4A475A3749C | lefred | 1 | +------------------------------------+--------+-------+ SQL > SELECT BIN_TO_UUID(uuid,1), name, beers FROM smug2; +--------------------------------------+--------+-------+ | bin_to_uuid(uuid,1) | name | beers | +--------------------------------------+--------+-------+ | 71438c99-b927-11ee-9aaa-f4a475a3749c | Ted | 0 | | 7143902f-b927-11ee-9aaa-f4a475a3749c | lefred | 1 | +--------------------------------------+--------+-------+ Copyright @ 2024 Oracle and/or its affiliates. 91
  115. InnoDB Primary Key - What about UUID ? (10) Recommended

    solution - example SQL> INSERT INTO smug2 (name, beers) VALUES ("Carsten",1), ("Scott",5); SQL> SELECT * FROM smug2; +------------------------------------+---------+-------+ | uuid | name | beers | +------------------------------------+---------+-------+ | 0x11EEB92771438C999AAAF4A475A3749C | Ted | 0 | | 0x11EEB9277143902F9AAAF4A475A3749C | lefred | 1 | | 0x11EEB9284D66AA559AAAF4A475A3749C | Carsten | 1 | | 0x11EEB9284D66B0439AAAF4A475A3749C | Scott | 5 | +------------------------------------+---------+-------+ Copyright @ 2024 Oracle and/or its affiliates. 92
  116. InnoDB Primary Key - What about UUID ? (10) Recommended

    solution - example SQL> INSERT INTO smug2 (name, beers) VALUES ("Carsten",1), ("Scott",5); SQL> SELECT * FROM smug2; +------------------------------------+---------+-------+ | uuid | name | beers | +------------------------------------+---------+-------+ | 0x11EEB92771438C999AAAF4A475A3749C | Ted | 0 | | 0x11EEB9277143902F9AAAF4A475A3749C | lefred | 1 | | 0x11EEB9284D66AA559AAAF4A475A3749C | Carsten | 1 | | 0x11EEB9284D66B0439AAAF4A475A3749C | Scott | 5 | +------------------------------------+---------+-------+ SQL> SELECT BIN_TO_UUID(uuid,1), name, beers FROM smug2; +--------------------------------------+---------+-------+ | bin_to_uuid(uuid,1) | name | beers | +--------------------------------------+---------+-------+ | 71438c99-b927-11ee-9aaa-f4a475a3749c | Ted | 0 | | 7143902f-b927-11ee-9aaa-f4a475a3749c | lefred | 1 | | 4d66aa55-b928-11ee-9aaa-f4a475a3749c | Carsten | 1 | | 4d66b043-b928-11ee-9aaa-f4a475a3749c | Scott | 5 | +--------------------------------------+---------+-------+ Copyright @ 2024 Oracle and/or its affiliates. 92
  117. InnoDB Primary Key - What about UUID ? (11) Recommended

    solution - example Take a look at the size of each entry in the INDEX (and same amount added to each secondary index) SQL > EXPLAIN SELECT * FROM smug2 WHERE uuid=UUID_TO_BIN("7143902f-b927-11ee-9aaa-f4a475a3749c",1)\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: smug2 partitions: NULL type: const possible_keys: PRIMARY key: PRIMARY key_len: 16 ref: const rows: 1 filtered: 100 Extra: NULL Copyright @ 2024 Oracle and/or its affiliates. 93
  118. MySQL UUID The UUIDs generated by MySQL are UUID v1

    as described in RFC4122. UUID v1 : is a universally unique identi er that is generated using a timestamp and the MAC address of the computer on which it was generated. UUID v4 : is a universally unique identi er that is generated using random numbers. UUID v7 : is a universally unique identi er that is generated using a timestamp and random numbers. Copyright @ 2024 Oracle and/or its affiliates. 94
  119. MySQL UUID The UUIDs generated by MySQL are UUID v1

    as described in RFC4122. UUID v1 : is a universally unique identi er that is generated using a timestamp and the MAC address of the computer on which it was generated. UUID v4 : is a universally unique identi er that is generated using random numbers. UUID v7 : is a universally unique identi er that is generated using a timestamp and random numbers. With UUID v4, it's not possible to generate any sequential output. Copyright @ 2024 Oracle and/or its affiliates. 94
  120. MySQL - More with UUID v1 Do you know you

    can now the exact time when your MySQL UUIDs (v1) were created ?? Copyright @ 2024 Oracle and/or its affiliates. 95
  121. MySQL - More with UUID v1 Do you know you

    can now the exact time when your MySQL UUIDs (v1) were created ?? Take a look at h ps://github.com/lefred/mysql-component-uuid_v1: SQL> select uuid_to_timestamp('2f1d6a6f-b9ee-11ee-8b9b-c8cb9e32df8e') timestamp; +-------------------------+ | timestamp | +-------------------------+ | 2024-01-23 13:51:53.887 | +-------------------------+ SQL> select uuid_to_timestamp_long('2f1d6a6f-b9ee-11ee-8b9b-c8cb9e32df8e') timestamp; +------------------------------+ | timestamp | +------------------------------+ | Tue Jan 23 13:51:53 2024 CET | +------------------------------+ Copyright @ 2024 Oracle and/or its affiliates. 95
  122. MySQL - extra If you would like to play with

    UUIDv4 and UUIDv7, take a look at these MySQL components: h ps://github.com/lefred/mysql-component-uuid_v4 h ps://github.com/lefred/mysql-component-uuid_v7 Copyright @ 2024 Oracle and/or its affiliates. 96
  123. MySQL - extra (2) Example SQL> select now(), uuid(), uuid_v4(),

    uuid_v7(); +---------------------+--------------------------------------+--------------------------------------+--------------------------------------+ | now() | uuid() | uuid_v4() | uuid_v7() | +---------------------+--------------------------------------+--------------------------------------+--------------------------------------+ | 2024-01-23 13:46:22 | 6990aafd-b9ed-11ee-8b9b-c8cb9e32df8e | c16d507f-d62e-4879-956d-d188c062184a | 018d365b-7ef6-7d99-aaf0-673e8d739b3e | +---------------------+--------------------------------------+--------------------------------------+--------------------------------------+ Copyright @ 2024 Oracle and/or its affiliates. 97
  124. MySQL - extra (2) Example SQL> select now(), uuid(), uuid_v4(),

    uuid_v7(); +---------------------+--------------------------------------+--------------------------------------+--------------------------------------+ | now() | uuid() | uuid_v4() | uuid_v7() | +---------------------+--------------------------------------+--------------------------------------+--------------------------------------+ | 2024-01-23 13:46:22 | 6990aafd-b9ed-11ee-8b9b-c8cb9e32df8e | c16d507f-d62e-4879-956d-d188c062184a | 018d365b-7ef6-7d99-aaf0-673e8d739b3e | +---------------------+--------------------------------------+--------------------------------------+--------------------------------------+ SQL> select now(), uuid(), uuid_v4(), uuid_v7(); +---------------------+--------------------------------------+--------------------------------------+--------------------------------------+ | now() | uuid() | uuid_v4() | uuid_v7() | +---------------------+--------------------------------------+--------------------------------------+--------------------------------------+ | 2024-01-23 13:49:00 | c7dfe39c-b9ed-11ee-8b9b-c8cb9e32df8e | 1bc1d087-de40-496a-aa67-aefc87aa05d0 | 018d365d-e907-70d1-a565-ed4017772ed3 | +---------------------+--------------------------------------+--------------------------------------+--------------------------------------+ Copyright @ 2024 Oracle and/or its affiliates. 97
  125. MySQL - extra (2) Example SQL> select now(), uuid(), uuid_v4(),

    uuid_v7(); +---------------------+--------------------------------------+--------------------------------------+--------------------------------------+ | now() | uuid() | uuid_v4() | uuid_v7() | +---------------------+--------------------------------------+--------------------------------------+--------------------------------------+ | 2024-01-23 13:46:22 | 6990aafd-b9ed-11ee-8b9b-c8cb9e32df8e | c16d507f-d62e-4879-956d-d188c062184a | 018d365b-7ef6-7d99-aaf0-673e8d739b3e | +---------------------+--------------------------------------+--------------------------------------+--------------------------------------+ SQL> select now(), uuid(), uuid_v4(), uuid_v7(); +---------------------+--------------------------------------+--------------------------------------+--------------------------------------+ | now() | uuid() | uuid_v4() | uuid_v7() | +---------------------+--------------------------------------+--------------------------------------+--------------------------------------+ | 2024-01-23 13:49:00 | c7dfe39c-b9ed-11ee-8b9b-c8cb9e32df8e | 1bc1d087-de40-496a-aa67-aefc87aa05d0 | 018d365d-e907-70d1-a565-ed4017772ed3 | +---------------------+--------------------------------------+--------------------------------------+--------------------------------------+ SQL> select now(), uuid(), uuid_v4(), uuid_v7(); +---------------------+--------------------------------------+--------------------------------------+--------------------------------------+ | now() | uuid() | uuid_v4() | uuid_v7() | +---------------------+--------------------------------------+--------------------------------------+--------------------------------------+ | 2024-01-23 13:51:53 | 2f1d6a6f-b9ee-11ee-8b9b-c8cb9e32df8e | a41ce1b4-d28a-4288-a5b8-cd7683664631 | 018d3660-8d9f-79eb-a683-8d4365b6f087 | +---------------------+--------------------------------------+--------------------------------------+--------------------------------------+ Copyright @ 2024 Oracle and/or its affiliates. 97
  126. MySQL Document Store JSON & Primary Key: _id Copyright @

    2024 Oracle and/or its affiliates. 98
  127. MySQL Document Store With MySQL, it's also possible to store

    JSON documents and use CRUD operations to deal with them. You can use MySQL without a single line of SQL! Copyright @ 2024 Oracle and/or its affiliates. 99
  128. MySQL Document Store With MySQL, it's also possible to store

    JSON documents and use CRUD operations to deal with them. You can use MySQL without a single line of SQL! $session $session = = mysql_xdevapi mysql_xdevapi\ \getSession getSession( ("mysqlx://fred:MyP@ssw0rd%@localhost" "mysqlx://fred:MyP@ssw0rd%@localhost") ); ; $schema $schema = = $session $session-> ->getSchema getSchema( ("docstore" "docstore") ); ; $collection $collection = = $schema $schema-> ->getCollection getCollection( ("restaurants" "restaurants") ); ; $results $results = = $collection $collection-> ->find find( ("cuisine='italian'" "cuisine='italian'") )-> ->execute execute( () )-> ->fetchAll fetchAll( () ); ; Copyright @ 2024 Oracle and/or its affiliates. 99
  129. MySQL Document Store Collections are special tables that look like

    this in SQL: Table Table: restaurants : restaurants Create Create Table Table: : CREATE CREATE TABLE TABLE ` `restaurants restaurants` ` ( ( ` `doc doc` ` json json DEFAULT DEFAULT NULL NULL, , ` `_id _id` ` varbinary varbinary( (32 32) ) GENERATED ALWAYS GENERATED ALWAYS AS AS ( (json_unquote json_unquote( (json_extract json_extract( (` `doc doc` `, ,_utf8mb4 _utf8mb4'$._id' '$._id') )) )) ) STORED STORED NOT NOT NULL NULL, , ` `_json_schema _json_schema` ` json GENERATED ALWAYS json GENERATED ALWAYS AS AS ( (_utf8mb4 _utf8mb4'{"type":"object"}' '{"type":"object"}') ) VIRTUAL VIRTUAL, , PRIMARY PRIMARY KEY KEY ( (` `_id _id` `) ), , CONSTRAINT CONSTRAINT ` `$val_strict_B38964F8D1504551AB639A4D860DFA5460340231 $val_strict_B38964F8D1504551AB639A4D860DFA5460340231` ` CHECK CHECK ( (json_schema_valid json_schema_valid( (` `_json_schema _json_schema` `, ,` `doc doc` `) )) ) /*!80016 NOT ENFORCED */ /*!80016 NOT ENFORCED */ ) ) ENGINE ENGINE= =InnoDB InnoDB DEFAULT DEFAULT CHARSET CHARSET= =utf8mb4 utf8mb4 COLLATE COLLATE= =utf8mb4_0900_ai_ci utf8mb4_0900_ai_ci Copyright @ 2024 Oracle and/or its affiliates. 100
  130. MySQL Document Store: _id We can see that _id is

    the Primary Key and generated automatically: Copyright @ 2024 Oracle and/or its affiliates. 101
  131. MySQL Document Store: _id: details The generation of Document's _id

    is de ned in WL#10955: The unique pre x is de ned with the variable mysqlx_document_id_unique_pre x. h ps://dev.mysql.com/worklog/task/?id=10955 Copyright @ 2024 Oracle and/or its affiliates. 104
  132. MySQL Document Store: fun with _id You can decode the

    timestamp to nd out when the server that generated the identi er was started: select select FROM_UNIXTIME FROM_UNIXTIME( (conv conv( (hex hex( (substring substring( (UNHEX UNHEX( (_id _id) ), ,3 3, ,4 4) )) ), ,16 16, ,10 10) )) ) server_start_time server_start_time from from restaurants restaurants where where doc doc- ->> >>"$.borough" "$.borough" = = 'Groningen' 'Groningen'; ; + +---------------------+ ---------------------+ | | server_start_time server_start_time | | + +---------------------+ ---------------------+ | | 2024 2024- -04 04- -30 30 11 11: :07 07: :00 00 | | + +---------------------+ ---------------------+ 1 1 row row in in set set ( (0.0170 0.0170 sec sec) ) Copyright @ 2024 Oracle and/or its affiliates. 105
  133. Share your ❤ to MySQL #mysql #MySQLCommunity Join our slack

    channel! bit.ly/mysql-slack Copyright @ 2024 Oracle and/or its affiliates. 106