|
| 1 | +# VT Transactions |
| 2 | + |
| 3 | +The vt transactions command is a sub-command of the vt toolset, designed to analyze query logs, identify transaction patterns, and produce a JSON report summarizing these patterns. |
| 4 | +This tool is particularly useful for understanding complex transaction behaviors, optimizing database performance, choosing sharding strategy, and auditing transactional queries. |
| 5 | + |
| 6 | +## Usage |
| 7 | + |
| 8 | +The basic usage of vt transactions is: |
| 9 | + |
| 10 | +```bash |
| 11 | +vt transactions querylog.log > report.json |
| 12 | +``` |
| 13 | + |
| 14 | + * querylog.log: The input query log file. This can be in various formats, such as SQL files, slow query logs, MySQL general query logs, or VTGate query logs. |
| 15 | + * report.json: The output JSON file containing the transaction patterns. |
| 16 | + |
| 17 | +### Supported Input Types |
| 18 | + |
| 19 | +`vt transactions` supports different input file formats through the --input-type flag: |
| 20 | + * Default: Assumes the input is an SQL file or a slow query log. A SQL script would also fall under this category. |
| 21 | + * MySQL General Query Log: Use --input-type=mysql-log for MySQL general query logs. |
| 22 | + * VTGate Query Log: Use --input-type=vtgate-log for VTGate query logs. |
| 23 | + |
| 24 | +## Understanding the JSON Output |
| 25 | + |
| 26 | +The output JSON file contains an array of transaction patterns, each summarizing a set of queries that commonly occur together within transactions. Here’s a snippet of the JSON output: |
| 27 | + |
| 28 | +```json |
| 29 | +{ |
| 30 | + "query-signatures": [ |
| 31 | + "update pos_reports where id = :0 set `csv`, `error`, intraday, pos_type, ...", |
| 32 | + "update pos_date_requests where cache_key = :1 set cache_value" |
| 33 | + ], |
| 34 | + "predicates": [ |
| 35 | + "pos_date_requests.cache_key = ?", |
| 36 | + "pos_reports.id = ?" |
| 37 | + ], |
| 38 | + "count": 223 |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +### Fields Explanation |
| 43 | + |
| 44 | + * query-signatures: An array of generalized query patterns involved in the transaction. Placeholders like :0, :1, etc., represent variables in the queries. |
| 45 | + * predicates: An array of predicates (conditions) extracted from the queries, generalized to identify patterns. |
| 46 | + * count: The number of times this transaction pattern was observed in the logs. |
| 47 | + |
| 48 | +### Understanding predicates |
| 49 | + |
| 50 | +The predicates array lists the conditions used in the transactional queries, with variables generalized for pattern recognition. |
| 51 | + * Shared Variables: If the same variable is used across different predicates within a transaction, it is assigned a numerical placeholder (e.g., 0, 1, 2). This indicates that the same variable or value is used in these predicates. |
| 52 | + * Unique Variables: Variables that are unique to a single predicate are represented with a ?. |
| 53 | + |
| 54 | +### Example Explained |
| 55 | + |
| 56 | +Consider the following predicates array: |
| 57 | + |
| 58 | +```json |
| 59 | +{ |
| 60 | + "predicates": [ |
| 61 | + "timesheets.day = ?", |
| 62 | + "timesheets.craft_id = ?", |
| 63 | + "timesheets.store_id = ?", |
| 64 | + "dailies.day = 0", |
| 65 | + "dailies.craft_id = 1", |
| 66 | + "dailies.store_id = 2", |
| 67 | + "tickets.day = 0", |
| 68 | + "tickets.craft_id = 1", |
| 69 | + "tickets.store_id = 2" |
| 70 | + ] |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | + * Shared Values: Predicates with the same value across different conditions are assigned numerical placeholders (0, 1, 2), indicating that the same variable or value is used in these predicates. |
| 75 | + * For example, `dailies.craft_id = 1` and `tickets.craft_id = 1` share the same variable or value (represented as 1). |
| 76 | + * Unique Values: Predicates used only once are represented with ?, indicating a unique or less significant variable in the pattern. |
| 77 | + * For example, `timesheets.day = ?` represents a unique value for day. |
| 78 | + |
| 79 | +This numbering helps identify the relationships between different predicates in the transaction patterns and can be used to optimize queries or understand transaction scopes. |
| 80 | + |
| 81 | +## Practical Use Cases |
| 82 | + |
| 83 | + * Optimization: Identify frequently occurring transactions to optimize database performance. |
| 84 | + * Sharding Strategy: When implementing horizontal sharding, it’s crucial to ensure that as many transactions as possible are confined to a single shard. The insights from vt transactions can help in choosing appropriate sharding keys for your tables to achieve this. |
| 85 | + * Audit: Analyze transactional patterns for security audits or compliance checks. |
| 86 | + * Debugging: Understand complex transaction behaviors during development or troubleshooting. |
0 commit comments