VTExplain

VTExplain Tool

The vtexplain tool provides information about how Vitess will execute a statement (the Vitess version of MySQL “EXPLAIN”).

Prerequisites

You will need to build the vtexplain binary in your environment. To find instructions on how to build this binary please refer to the Build From Source guide.

Explaining a Query

In order to explain a query you will need to first collect a sql schema for the various tables and a vschema json file containing a map of keyspace to the set of vindexes / tables in the vschema.

For example, let’s use the following:

Schema:

  1. CREATE TABLE users(
  2. user_id bigint,
  3. name varchar(128),
  4. primary key(user_id)
  5. );
  6. CREATE TABLE users_name_idx(
  7. user_id bigint,
  8. name varchar(128),
  9. primary key(name, user_id)
  10. );

VSchema:

  1. {
  2. "mainkeyspace": {
  3. "sharded": true,
  4. "vindexes": {
  5. "hash": {
  6. "type": "hash"
  7. },
  8. "md5": {
  9. "type": "unicode_loose_md5",
  10. "params": {},
  11. "owner": ""
  12. },
  13. "users_name_idx": {
  14. "type": "lookup_hash",
  15. "params": {
  16. "from": "name",
  17. "table": "users_name_idx",
  18. "to": "user_id"
  19. },
  20. "owner": "users"
  21. }
  22. },
  23. "tables": {
  24. "users": {
  25. "column_vindexes": [
  26. {
  27. "column": "user_id",
  28. "name": "hash"
  29. },
  30. {
  31. "column": "name",
  32. "name": "users_name_idx"
  33. }
  34. ],
  35. "auto_increment": null
  36. },
  37. "users_name_idx": {
  38. "type": "",
  39. "column_vindexes": [
  40. {
  41. "column": "name",
  42. "name": "md5"
  43. }
  44. ],
  45. "auto_increment": null
  46. }
  47. }
  48. }
  49. }

These can be passed to vtexplain either on the command line or (more readily) through files.

Then you can use the tool like this:

Select:

  1. vtexplain -shards 8 -vschema-file /tmp/vschema.json -schema-file /tmp/schema.sql -replication-mode "ROW" -output-mode text -sql "SELECT * from users"

SELECT * from users

1 mainkeyspace/-20: select from users limit 100011 mainkeyspace/20-40: select from users limit 100011 mainkeyspace/40-60: select from users limit 100011 mainkeyspace/60-80: select from users limit 100011 mainkeyspace/80-a0: select from users limit 100011 mainkeyspace/a0-c0: select from users limit 100011 mainkeyspace/c0-e0: select from users limit 100011 mainkeyspace/e0-: select from users limit 10001


The output shows the sequence of queries run.

In this case, the query planner is a scatter query to all shards, and each line shows:

  • the logical sequence of the query
  • the keyspace/shard
  • the query that was executedThe fact that each query runs at time 1 shows that vitess executes these in parallel, and the limit 10001 is automatically added as a protection against large results.

Insert:

  1. vtexplain -shards 128 -vschema-file /tmp/vschema.json -schema-file /tmp/schema.sql -replication-mode "ROW" -output-mode text -sql "INSERT INTO users (user_id, name) VALUES(1, 'john')"
  2. ----------------------------------------------------------------------
  3. INSERT INTO users (user_id, name) VALUES(1, 'john')
  4. 1 mainkeyspace/22-24: begin
  5. 1 mainkeyspace/22-24: insert into users_name_idx(name, user_id) values ('john', 1) /* vtgate:: keyspace_id:22c0c31d7a0b489a16332a5b32b028bc */
  6. 2 mainkeyspace/16-18: begin
  7. 2 mainkeyspace/16-18: insert into users(user_id, name) values (1, 'john') /* vtgate:: keyspace_id:166b40b44aba4bd6 */
  8. 3 mainkeyspace/22-24: commit
  9. 4 mainkeyspace/16-18: commit
  10. ----------------------------------------------------------------------

This example shows how Vitess handles an insert into a table with a secondary lookup vindex:

  • At time 1, a transaction is opened on one shard to insert the row into the users_name_idx table.
  • Then at time 2 a second transaction is opened on another shard with the actual insert into the users table.
  • Finally each transaction is committed at time 3 and 4.Configuration Options

The —shards option specifies the number of shards to simulate. vtexplain will always allocate an evenly divided key range to each.

The —replication-mode option controls whether to simulate row-based or statement-based replication.

You can find more usage of vtexplain by executing the following command:

  1. vtexplain --help