Creating a LookupVindex

    This guide follows on from the Get Started guides. Please make sure that you have an Operator or local installation ready. Make sure you are at the point where you have the sharded keyspace called customer setup.

    LookupVindex create uses a VReplication workflow used to create and backfill a Lookup Vindex automatically for a table that already exists, and may have a significant amount of data in it already.

    Internally, the LookupVindex create command uses VReplication for the backfill process, until the lookup Vindex is “in sync”. Then the normal process for adding/deleting/updating rows in the lookup Vindex via the usual transactional flow when updating the “owner” table for the Vindex takes over.

    In this guide, we will walk through the process of using the LookupVindex create command, and give some insight into what happens underneath the covers.

    You can see the details of the LookupVindex create command in the reference docs.

    In the context of the customer database that is part of the Vitess examples we started earlier, let’s add some rows into the customer.corder table, and then look at an example command:

    1. $ mysql -P 15306 -h 127.0.0.1 -u root --binary-as-hex=false -A
    2. Welcome to the MySQL monitor. Commands end with ; or \g.
    3. ...
    1. mysql> use customer;
    2. Database changed
    3. mysql> show tables;
    4. +-----------------------+
    5. | Tables_in_vt_customer |
    6. +-----------------------+
    7. | corder |
    8. | customer |
    9. +-----------------------+
    10. 2 rows in set (0.00 sec)
    11. mysql> desc corder;
    12. +-------------+----------------+------+-----+---------+-------+
    13. | Field | Type | Null | Key | Default | Extra |
    14. +-------------+----------------+------+-----+---------+-------+
    15. | order_id | bigint | NO | PRI | NULL | |
    16. | customer_id | bigint | YES | | NULL | |
    17. | sku | varbinary(128) | YES | | NULL | |
    18. | price | bigint | YES | | NULL | |
    19. +-------------+----------------+------+-----+---------+-------+
    20. 4 rows in set (0.01 sec)
    21. mysql> insert into corder (order_id, customer_id, sku, price) values (1, 1, "Product_1", 100);
    22. Query OK, 1 row affected (0.01 sec)
    23. mysql> insert into corder (order_id, customer_id, sku, price) values (2, 1, "Product_2", 101);
    24. Query OK, 1 row affected (0.01 sec)
    25. mysql> insert into corder (order_id, customer_id, sku, price) values (3, 2, "Product_3", 102);
    26. Query OK, 1 row affected (0.01 sec)
    27. mysql> insert into corder (order_id, customer_id, sku, price) values (4, 3, "Product_4", 103);
    28. Query OK, 1 row affected (0.01 sec)
    29. mysql> insert into corder (order_id, customer_id, sku, price) values (5, 4, "Product_5", 104);
    30. Query OK, 1 row affected (0.03 sec)
    31. mysql> select * from corder;
    32. +----------+-------------+-----------+-------+
    33. | order_id | customer_id | sku | price |
    34. +----------+-------------+-----------+-------+
    35. | 1 | 1 | Product_1 | 100 |
    36. | 2 | 1 | Product_2 | 101 |
    37. | 3 | 2 | Product_3 | 102 |
    38. | 4 | 3 | Product_4 | 103 |
    39. | 5 | 4 | Product_5 | 104 |
    40. +----------+-------------+-----------+-------+
    41. 5 rows in set (0.01 sec)

    If we look at the VSchema for the customer.corder table, we will see there is a hash index on the customer_id column:

    1. $ vtctldclient GetVSchema customer
    2. {
    3. "sharded": true,
    4. "vindexes": {
    5. "hash": {
    6. "type": "hash",
    7. "params": {},
    8. "owner": ""
    9. }
    10. },
    11. "tables": {
    12. "corder": {
    13. "type": "",
    14. "column_vindexes": [
    15. {
    16. "column": "customer_id",
    17. "name": "hash",
    18. "columns": []
    19. }
    20. ],
    21. "auto_increment": {
    22. "column": "order_id",
    23. "sequence": "order_seq"
    24. },
    25. "columns": [],
    26. "pinned": "",
    27. "column_list_authoritative": false,
    28. "source": ""
    29. },
    30. "customer": {
    31. "type": "",
    32. "column_vindexes": [
    33. {
    34. "column": "customer_id",
    35. "name": "hash",
    36. "columns": []
    37. }
    38. ],
    39. "auto_increment": {
    40. "column": "customer_id",
    41. "sequence": "customer_seq"
    42. },
    43. "columns": [],
    44. "pinned": "",
    45. "column_list_authoritative": false,
    46. "source": ""
    47. }
    48. },
    49. "require_explicit_routing": false
    50. }

    We can now see that 4 of our 5 rows have ended up on the -80 shard with the 5th row on the 80- shard:

    1. mysql> use customer/-80
    2. Database changed
    3. mysql> select * from corder;
    4. +----------+-------------+-----------+-------+
    5. | order_id | customer_id | sku | price |
    6. +----------+-------------+-----------+-------+
    7. | 1 | 1 | Product_1 | 100 |
    8. | 2 | 1 | Product_2 | 101 |
    9. | 3 | 2 | Product_3 | 102 |
    10. | 4 | 3 | Product_4 | 103 |
    11. +----------+-------------+-----------+-------+
    12. 4 rows in set (0.00 sec)
    13. mysql> use customer/80-
    14. Database changed
    15. mysql> select * from corder;
    16. +----------+-------------+-----------+-------+
    17. | order_id | customer_id | sku | price |
    18. +----------+-------------+-----------+-------+
    19. | 5 | 4 | Product_5 | 104 |
    20. +----------+-------------+-----------+-------+
    21. 1 row in set (0.01 sec)

    Note that this skewed distribution is completely coincidental — for larger numbers of rows we would expect the distribution to be approximately even for a hash index.

    Now let’s say we want to add a lookup Vindex on the sku column. We can use a consistent_lookup or consistent_lookup_unique Vindex type. In our example we will use consistent_lookup_unique.

    Note that as mentioned above, we do not have to tell Vitess about how to shard the actual backing table for the lookup Vindex or any schema to create as it will do it automatically. Now, let us actually execute the LookupVindex create command:

    1. vtctldclient --server localhost:15999 LookupVindex --name customer_region_lookup --table-keyspace main create --keyspace main --type consistent_lookup_unique --table-owner customer --table-owner-columns=id --tablet-types=PRIMARY

    Note:

    • We are specifying a tablet_type of RDONLY; meaning it is going to run the VReplication streams from tablets of the RDONLY type only. If tablets of this type cannot be found, in a shard, the lookup Vindex population will fail.

    Now, in our case, the table is tiny, so the copy will be instant, but in a real-world case this might take hours. To monitor the process, we can use the usual VReplication commands. However, the VReplication status commands needs to operate on individual tablets. Let’s check which tablets we have in our environment, so we know which tablets to issue commands against:

    1. $ vtctldclient --server localhost:15999 GetTablets --keyspace customer
    2. zone1-0000000300 customer -80 primary localhost:15300 localhost:17300 [] 2020-08-13T01:23:15Z
    3. zone1-0000000301 customer -80 replica localhost:15301 localhost:17301 [] <null>
    4. zone1-0000000302 customer -80 rdonly localhost:15302 localhost:17302 [] <null>
    5. zone1-0000000400 customer 80- primary localhost:15400 localhost:17400 [] 2020-08-13T01:23:15Z
    6. zone1-0000000401 customer 80- replica localhost:15401 localhost:17401 [] <null>
    7. zone1-0000000402 customer 80- rdonly localhost:15402 localhost:17402 [] <null>

    Now we can look what happened in greater detail:

    • VReplication streams were setup from the primary tablets zone1-0000000300 and zone1-0000000400; pulling data from the RDONLY source tablets zone1-0000000302 and zone1-0000000402.
    • Note that each primary tablet will start streams from each source tablet, for a total of 4 streams in this case.

    Lets observe the VReplication streams that got created using the show sub-command.

    The created vreplication workflow will have a generated name of <target_table_name>_vdx. So in our example here: corder_lookup_vdx.

    1. $ vtctldclient --server localhost:15999 LookupVindex --name customer_region_lookup --table-keyspace main show --include-logs=false
    2. {
    3. "workflows": [
    4. {
    5. "name": "customer_region_lookup",
    6. "source": {
    7. "keyspace": "main",
    8. "shards": [
    9. "0"
    10. ]
    11. },
    12. "target": {
    13. "keyspace": "main",
    14. "shards": [
    15. "0"
    16. ]
    17. },
    18. "max_v_replication_lag": "0",
    19. "shard_streams": {
    20. "0/zone1-0000000100": {
    21. "streams": [
    22. {
    23. "id": "1",
    24. "shard": "0",
    25. "tablet": {
    26. "cell": "zone1",
    27. "uid": 100
    28. },
    29. "binlog_source": {
    30. "keyspace": "main",
    31. "shard": "0",
    32. "tablet_type": "UNKNOWN",
    33. "key_range": null,
    34. "tables": [],
    35. "filter": {
    36. "rules": [
    37. {
    38. "match": "customer_region_lookup",
    39. "filter": "select id as id, keyspace_id() as keyspace_id from customer where in_keyrange(id, 'main.xxhash', '-') group by id, keyspace_id",
    40. "convert_enum_to_text": {},
    41. "convert_charset": {},
    42. "source_unique_key_columns": "",
    43. "target_unique_key_columns": "",
    44. "source_unique_key_target_columns": "",
    45. "convert_int_to_enum": {}
    46. }
    47. ],
    48. "field_event_mode": "ERR_ON_MISMATCH",
    49. "workflow_type": "0",
    50. "workflow_name": ""
    51. },
    52. "on_ddl": "IGNORE",
    53. "external_mysql": "",
    54. "stop_after_copy": false,
    55. "external_cluster": "",
    56. "source_time_zone": "",
    57. "target_time_zone": ""
    58. },
    59. "position": "63c84d28-6888-11ee-93b0-81b2fbd12545:1-63",
    60. "stop_position": "",
    61. "state": "Running",
    62. "db_name": "vt_main",
    63. "transaction_timestamp": {
    64. "seconds": "1697064644",
    65. "nanoseconds": 0
    66. },
    67. "time_updated": {
    68. "seconds": "1697064646",
    69. "nanoseconds": 0
    70. },
    71. "message": "",
    72. "copy_states": [],
    73. "logs": [],
    74. "log_fetch_error": "",
    75. "tags": [],
    76. "rows_copied": "0",
    77. "throttler_status": {
    78. "component_throttled": "",
    79. "time_throttled": {
    80. "seconds": "0",
    81. "nanoseconds": 0
    82. }
    83. }
    84. }
    85. ],
    86. "tablet_controls": [],
    87. "is_primary_serving": true
    88. }
    89. },
    90. "workflow_type": "CreateLookupIndex",
    91. "workflow_sub_type": "None",
    92. "max_v_replication_transaction_lag": "0",
    93. "defer_secondary_keys": false
    94. }
    95. ]
    96. }

    There is a lot going on in this output, but the most important parts are the state and message fields which say Stopped and Stopped after copy. for all four of the streams. This means that the VReplication streams finished their copying/backfill of the lookup table.

    Note that if the tables were large and the copy was still in progress, the state field would say Copying — you can see the state/progress as part of Workflow show json output.

    We can verify the result of the backfill by looking at the customer keyspace again in the MySQL client:

    1. mysql> show tables;
    2. +-----------------------+
    3. | Tables_in_vt_customer |
    4. +-----------------------+
    5. | corder |
    6. | corder_lookup |
    7. | customer |
    8. +-----------------------+
    9. 3 rows in set (0.01 sec)

    Note there is now a new table, corder_lookup; which was created as the backing table for the lookup Vindex. Lets look at this table:

    1. mysql> desc corder_lookup;
    2. +-------------+----------------+------+-----+---------+-------+
    3. | Field | Type | Null | Key | Default | Extra |
    4. +-------------+----------------+------+-----+---------+-------+
    5. | sku | varbinary(128) | NO | PRI | NULL | |
    6. | keyspace_id | varbinary(128) | YES | | NULL | |
    7. +-------------+----------------+------+-----+---------+-------+
    8. 2 rows in set (0.01 sec)
    9. mysql> select sku, hex(keyspace_id) from corder_lookup;
    10. +-----------+------------------+
    11. | sku | hex(keyspace_id) |
    12. +-----------+------------------+
    13. | Product_2 | 166B40B44ABA4BD6 |
    14. | Product_3 | 06E7EA22CE92708F |
    15. | Product_1 | 166B40B44ABA4BD6 |
    16. | Product_4 | 4EB190C9A2FA169C |
    17. | Product_5 | D2FD8867D50D2DFE |
    18. +-----------+------------------+

    Basically, this shows exactly what we expected. Now, we have to clean-up the artifacts of the backfill. The ExternalizeVindex command will delete the VReplication streams and also clear the write_only flag from the Vindex indicating that it is not backfilling anymore.

    1. $ vtctldclient --server localhost:15999 LookupVindex --name customer_region_lookup --table-keyspace main externalize
    2. LookupVindex customer_region_lookup has been externalized and the customer_region_lookup VReplication workflow has been deleted

    Next, to confirm the lookup Vindex is doing what we think it should, we can use the vexplain plan SQL statement:

    1. mysql> vexplain plan select * from corder where customer_id = 1;
    2. +---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    3. | JSON |
    4. +---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    5. | {
    6. "OperatorType": "Route",
    7. "Variant": "EqualUnique",
    8. "Keyspace": {
    9. "Name": "customer",
    10. "Sharded": true
    11. },
    12. "FieldQuery": "select order_id, customer_id, sku, price from corder where 1 != 1",
    13. "Query": "select order_id, customer_id, sku, price from corder where customer_id = 1",
    14. "Table": "corder",
    15. "Values": [
    16. "INT64(1)"
    17. ],
    18. "Vindex": "hash"
    19. } |
    20. +---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    21. 1 row in set (0.00 sec)

    Since the above select statement is doing a lookup using the primary Vindex on the corder table, this query does not Scatter (variant is SelectEqualUnique), as expected. Let’s try a scatter query to see what that looks like:

    1. mysql> vexplain select * from corder;
    2. +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    3. | JSON |
    4. +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    5. | {
    6. "OperatorType": "Route",
    7. "Variant": "Scatter",
    8. "Keyspace": {
    9. "Name": "customer",
    10. "Sharded": true
    11. },
    12. "FieldQuery": "select order_id, customer_id, sku, price from corder where 1 != 1",
    13. "Query": "select order_id, customer_id, sku, price from corder",
    14. "Table": "corder"
    15. } |
    16. +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    17. 1 row in set (0.00 sec)

    So now we see the expectied variant of SelectScatter for a scatter query. Let’s try a lookup on a column that does not have a primary or secondary (lookup) Vindex, e.g. the price column:

    1. mysql> vexplain select * from corder where price = 103\G
    2. *************************** 1. row ***************************
    3. JSON: {
    4. "OperatorType": "Route",
    5. "Variant": "Scatter",
    6. "Keyspace": {
    7. "Name": "customer",
    8. "Sharded": true
    9. },
    10. "FieldQuery": "select order_id, customer_id, sku, price from corder where 1 != 1",
    11. "Query": "select order_id, customer_id, sku, price from corder where price = 103",
    12. "Table": "corder"
    13. }
    14. 1 row in set (0.00 sec)

    That also scatters, as expected, because there’s no Vindex on the column.

    Now, let’s try a lookup on the sku column, which we have created our lookup Vindex on:

    1. mysql> vexplain select * from corder where sku = "Product_1";
    2. +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    3. | JSON |
    4. +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    5. | {
    6. "OperatorType": "VindexLookup",
    7. "Variant": "EqualUnique",
    8. "Keyspace": {
    9. "Name": "customer",
    10. "Sharded": true
    11. },
    12. "Values": [
    13. "VARCHAR(\"Product_1\")"
    14. ],
    15. "Vindex": "corder_lookup",
    16. "Inputs": [
    17. {
    18. "OperatorType": "Route",
    19. "Variant": "IN",
    20. "Keyspace": {
    21. "Name": "customer",
    22. "Sharded": true
    23. },
    24. "FieldQuery": "select sku, keyspace_id from corder_lookup where 1 != 1",
    25. "Query": "select sku, keyspace_id from corder_lookup where sku in ::__vals",
    26. "Table": "corder_lookup",
    27. "Values": [
    28. ":sku"
    29. ],
    30. "Vindex": "binary_md5"
    31. },
    32. {
    33. "OperatorType": "Route",
    34. "Variant": "ByDestination",
    35. "Keyspace": {
    36. "Name": "customer",
    37. "Sharded": true
    38. },
    39. "FieldQuery": "select order_id, customer_id, sku, price from corder where 1 != 1",
    40. "Query": "select order_id, customer_id, sku, price from corder where sku = 'Product_1'",
    41. "Table": "corder"
    42. }
    43. ]
    44. } |
    45. +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    46. 1 row in set (0.00 sec)

    As expected, we can see it is not scattering anymore, which it would have before we executed the CreateLookupVindex command.

    Lastly, let’s ensure that the lookup Vindex is being updated appropriately when we insert and delete rows:

    1. mysql> select * from corder;
    2. +----------+-------------+-----------+-------+
    3. | order_id | customer_id | sku | price |
    4. +----------+-------------+-----------+-------+
    5. | 5 | 4 | Product_5 | 104 |
    6. | 1 | 1 | Product_1 | 100 |
    7. | 2 | 1 | Product_2 | 101 |
    8. | 3 | 2 | Product_3 | 102 |
    9. | 4 | 3 | Product_4 | 103 |
    10. +----------+-------------+-----------+-------+
    11. 5 rows in set (0.00 sec)
    12. mysql> delete from corder where customer_id = 1 and sku = "Product_1";
    13. Query OK, 1 row affected (0.03 sec)
    14. mysql> select * from corder;
    15. +----------+-------------+-----------+-------+
    16. | order_id | customer_id | sku | price |
    17. +----------+-------------+-----------+-------+
    18. | 2 | 1 | Product_2 | 101 |
    19. | 3 | 2 | Product_3 | 102 |
    20. | 4 | 3 | Product_4 | 103 |
    21. | 5 | 4 | Product_5 | 104 |
    22. +----------+-------------+-----------+-------+
    23. 4 rows in set (0.01 sec)
    24. mysql> select sku, hex(keyspace_id) from corder_lookup;
    25. +-----------+------------------+
    26. | sku | hex(keyspace_id) |
    27. +-----------+------------------+
    28. | Product_4 | 4EB190C9A2FA169C |
    29. | Product_5 | D2FD8867D50D2DFE |
    30. | Product_2 | 166B40B44ABA4BD6 |
    31. | Product_3 | 06E7EA22CE92708F |
    32. +-----------+------------------+
    33. 4 rows in set (0.01 sec)

    We deleted a row from the corder table, and the matching lookup Vindex row is gone. Now we can try adding a row:

    1. mysql> insert into corder (order_id, customer_id, sku, price) values (6, 1, "Product_6", 105);
    2. Query OK, 1 row affected (0.02 sec)
    3. mysql> select * from corder;
    4. +----------+-------------+-----------+-------+
    5. | order_id | customer_id | sku | price |
    6. +----------+-------------+-----------+-------+
    7. | 2 | 1 | Product_2 | 101 |
    8. | 3 | 2 | Product_3 | 102 |
    9. | 4 | 3 | Product_4 | 103 |
    10. | 6 | 1 | Product_6 | 105 |
    11. | 5 | 4 | Product_5 | 104 |
    12. +----------+-------------+-----------+-------+
    13. 5 rows in set (0.00 sec)
    14. mysql> select sku, hex(keyspace_id) from corder_lookup;
    15. +-----------+------------------+
    16. | sku | hex(keyspace_id) |
    17. +-----------+------------------+
    18. | Product_4 | 4EB190C9A2FA169C |
    19. | Product_5 | D2FD8867D50D2DFE |
    20. | Product_6 | 166B40B44ABA4BD6 |
    21. | Product_2 | 166B40B44ABA4BD6 |
    22. | Product_3 | 06E7EA22CE92708F |
    23. +-----------+------------------+
    24. 5 rows in set (0.00 sec)

    We added a new row to the corder table, and now we have a new row in the lookup table!