| Crates.io | slowlog |
| lib.rs | slowlog |
| version | 0.1.0 |
| created_at | 2025-12-23 13:04:28.355575+00 |
| updated_at | 2025-12-23 13:04:28.355575+00 |
| description | A parser for MySQL slow query logs that anonymises queries by replacing literals with placeholders. |
| homepage | |
| repository | https://github.com/tpyo/slowlog |
| max_upload_size | |
| id | 2001464 |
| size | 70,758 |
A Rust library for parsing and analysing MySQL slow query logs. This library anonymises SQL queries by replacing literal values with placeholders, making it easy to identify and group similar queries for performance analysis.
cargo add slowlog
use slowlog::process_slow_log_file;
fn main() {
process_slow_log_file("path/to/slow.log", |query| {
println!("Original: {}", query.query);
println!("Formatted: {}", query.formatted);
println!("Fingerprint: {}", query.fingerprint);
println!("Query time: {:.2}s", query.stats.query_time);
println!("Rows examined: {}", query.stats.rows_examined);
}).unwrap();
}
use slowlog::process_slow_log_reader;
use std::fs::File;
use std::io::BufReader;
fn main() {
let file = File::open("slow.log").unwrap();
let reader = BufReader::new(file);
process_slow_log_reader(reader, |query| {
println!("{:?}", query);
}).unwrap();
}
The library replaces all literal values with ? placeholders:
| Original Query | Anonymised Query |
|---|---|
SELECT * FROM users WHERE id = 123 |
SELECT * FROM users WHERE id = ? |
UPDATE users SET name = 'John' WHERE age > 18 |
UPDATE users SET name = ? WHERE age > ? |
INSERT INTO users (name, age) VALUES ('Alice', 25) |
INSERT INTO users (name, age) VALUES (?, ?) |
DELETE FROM users WHERE age BETWEEN 18 AND 65 |
DELETE FROM users WHERE age BETWEEN ? AND ? |
The library extracts the following statistics from slow log entries:
process_slow_log_file<Q>(path: &str, query_callback: Q) -> io::Result<()>Processes a slow log file and calls the callback for each query found.
Parameters:
path: Path to the slow log filequery_callback: Function called for each parsed queryExample:
use slowlog::process_slow_log_file;
process_slow_log_file("slow.log", |query| {
println!("{:?}", query);
}).unwrap();
process_slow_log_reader<R, Q>(reader: R, query_callback: Q) -> io::Result<()>Processes slow log data from any BufRead source.
Parameters:
reader: Any type implementing BufReadquery_callback: Function called for each parsed queryExample:
use slowlog::process_slow_log_reader;
use std::io::BufReader;
let reader = BufReader::new(file);
process_slow_log_reader(reader, |query| {
println!("{:?}", query);
}).unwrap();
Querypub struct Query {
pub query: String, // Original query
pub formatted: String, // Anonymised query with placeholders
pub fingerprint: String, // SHA1 hash of formatted query
pub stats: QueryStats, // Query execution statistics
}
QueryStatspub struct QueryStats {
pub user: String, // Database user
pub host: String, // Client host
pub time: DateTime<Utc>, // Execution timestamp
pub rows_examined: u64, // Rows scanned
pub rows_sent: u64, // Rows returned
pub query_time: f64, // Execution time (seconds)
pub lock_time: f64, // Lock wait time (seconds)
}
QueryErrorpub enum QueryError {
ParseError(String), // SQL parsing failed
InvalidQuery, // No valid SQL statement found
}
MIT
https://github.com/tpyo/slowlog
Don Schonknecht don@tpyo.net