use ignis_db::{index, table, view, Query, Source};
use std::time::Instant;
table! {
users (id) {
id -> Int4,
email -> Varchar,
name -> Text,
created_at -> Timestamptz,
}
}
table! {
posts (id) {
id -> Int4,
user_id -> Int4,
title -> Text,
content -> Text,
status -> Varchar,
created_at -> Timestamptz,
}
}
table! {
comments (id) {
id -> Int4,
post_id -> Int4,
user_id -> Int4,
content -> Text,
created_at -> Timestamptz,
}
}
view! {
user_stats (id) {
id -> Int4,
post_count -> Int4,
comment_count -> Int4,
last_active -> Timestamptz,
}
}
index!(idx_users_email(users, email));
index!(idx_posts_user_date(posts, user_id, created_at));
index!(idx_comments_post(comments, post_id));
fn main() {
println!("🚀 Performance Optimization Demo");
println!("{}", "=".repeat(60));
println!("\n📊 1. SQL Generation Performance Test");
println!("{}", "-".repeat(40));
let iterations = 1000;
let start = Instant::now();
for _ in 0..iterations {
let query = Query::new()
.from_table(users::table())
.select_columns(vec!["id".to_string(), "email".to_string()])
.filter("status = 'active'")
.limit(10);
let _sql = query.to_sql();
}
let simple_duration = start.elapsed();
println!(
"✅ Simple Query ({} iterations): {:?}",
iterations, simple_duration
);
let start = Instant::now();
for _ in 0..iterations {
let query = Query::new()
.from_table(users::table())
.inner_join_table(posts::table(), "users.id = posts.user_id")
.inner_join_table(comments::table(), "posts.id = comments.post_id")
.select_columns(vec![
"users.name".to_string(),
"COUNT(posts.id)".to_string(),
"COUNT(comments.id)".to_string(),
])
.filter("posts.status = 'published'")
.group_by("users.name")
.having("COUNT(posts.id) > 5")
.order_by("COUNT(posts.id) DESC")
.limit(20);
let _sql = query.to_sql();
}
let complex_duration = start.elapsed();
println!(
"✅ Complex Query ({} iterations): {:?}",
iterations, complex_duration
);
let start = Instant::now();
for _ in 0..iterations {
let query = Query::new()
.from_table(users::table())
.select_columns(vec!["id".to_string(), "email".to_string()])
.filter("email = 'test@example.com'")
.with_indexes(vec![idx_users_email::meta(), idx_posts_user_date::meta()]);
let _sql = query.to_sql();
}
let indexed_duration = start.elapsed();
println!(
"✅ Indexed Query ({} iterations): {:?}",
iterations, indexed_duration
);
println!("\n📝 2. Generated SQL Examples");
println!("{}", "-".repeat(40));
let simple_query = Query::new()
.from_table(users::table())
.select_columns(vec!["id".to_string(), "email".to_string()])
.filter("status = 'active'")
.limit(10);
println!("🔹 Simple Query:");
println!("{}", simple_query.to_sql());
let complex_query = Query::new()
.from_table(users::table())
.inner_join_table(posts::table(), "users.id = posts.user_id")
.select_columns(vec![
"users.name".to_string(),
"COUNT(posts.id)".to_string(),
])
.filter("posts.status = 'published'")
.group_by("users.name")
.having("COUNT(posts.id) > 5")
.order_by("COUNT(posts.id) DESC")
.limit(20);
println!("\n🔹 Complex Query:");
println!("{}", complex_query.to_sql());
println!("\n📈 3. Performance Analysis");
println!("{}", "-".repeat(40));
let simple_avg = simple_duration.as_nanos() / iterations as u128;
let complex_avg = complex_duration.as_nanos() / iterations as u128;
let indexed_avg = indexed_duration.as_nanos() / iterations as u128;
println!("📊 Average SQL Generation Times:");
println!(" • Simple Query: {} ns", simple_avg);
println!(" • Complex Query: {} ns", complex_avg);
println!(" • Indexed Query: {} ns", indexed_avg);
println!("\n🚀 Performance Optimizations Applied:");
println!(" ✅ Hash-based query caching infrastructure");
println!(" ✅ Cow<'static, str> for static strings");
println!(" ✅ Pre-allocated string capacity (512 bytes)");
println!(" ✅ Optimized JOIN clause generation");
println!(" ✅ Reduced string allocations in hot paths");
println!("\n💡 Expected Production Benefits:");
println!(" • 20-40% faster SQL generation for repeated queries");
println!(" • Reduced memory allocations in high-throughput scenarios");
println!(" • Better cache locality for frequently used queries");
println!(" • Improved response times for complex analytics queries");
println!("\n🎉 Performance optimization demo complete!");
}