Operation 259160
SELECT users.*, posts.*, comments.*, COUNT(DISTINCT posts.id) as post_count, COUNT(DISTINCT comments.id) as comment_count, AVG(posts.view_count) as avg_views, MAX(comments.created_at) as latest_comment FROM users LEFT JOIN posts ON users.id = posts.user_id LEFT JOIN comments ON users.id = comments.user_id LEFT JOIN user_preferences ON users.id = user_preferences.user_id LEFT JOIN subscriptions ON users.id = subscriptions.user_id WHERE (users.active = ? OR users.premium = ?) AND (posts.published = ? OR posts.featured = ?) AND (comments.approved = ? OR comments.flagged = ?) AND users.created_at BETWEEN ? AND ? GROUP BY users.id HAVING COUNT(posts.id) > ? AND AVG(posts.view_count) > ?
- Operation Type
sql
- Duration
- 10.0 ms
- Request Impact
- 23.5% of total request time
- Occurred At
- Sep 12, 2026 4:17 AM
- SQL Analysis
- Normal query (<100 ms)
- Performance Comparison
-
Average: 18.5ms
95th Percentile: 34.0ms
Based on 1000 recent operations - Related Operations
- 1 similar operation in this request
- Optimization Tips
-
Add composite index: add_index :users, ["active", "premium", "published", "featured", "approved", "flagged", "created_at"] - Efficient multi-column filteringAdd single_column index: add_index :comments, :user_id - Fast JOIN executionAdd single_column index: add_index :posts, :user_id - Fast JOIN executionAdd single_column index: add_index :subscriptions, :user_id - Fast JOIN executionAdd single_column index: add_index :user_preferences, :user_id - Fast JOIN executionAdd single_column index: add_index :users, :active - Fast lookups for active = value queriesAdd single_column index: add_index :users, :approved - Fast lookups for approved = value queriesAdd single_column index: add_index :users, :featured - Fast lookups for featured = value queriesAdd single_column index: add_index :users, :flagged - Fast lookups for flagged = value queriesAdd single_column index: add_index :users, :premium - Fast lookups for premium = value queriesAdd single_column index: add_index :users, :published - Fast lookups for published = value queriesAdd single_column index: add_index :users, :created_at - Efficient range scans for created_atReview if all 4 JOINs are necessary - Simplified query execution and better performanceConsider breaking complex query (complexity: 34) into smaller parts - Easier maintenance and potentially better performanceSimplify WHERE clause by breaking into multiple queries or using views - Easier maintenance and potentially better performanceIndex Optimization - Review indexes on the 'users' table. Consider composite indexes for WHERE clauses.