Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Slower Laravel Package

halilcosdu/laravel-slower

Detects slow Laravel database queries, logs them, and uses optional AI recommendations to suggest indexes and other optimizations. Configure thresholds, enable/disable monitoring, and run with Laravel 10–13 on PHP 8.2+ for actionable performance insights.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Query Optimization Focus: Aligns well with Laravel applications experiencing database performance bottlenecks, particularly those with complex queries, missing indexes, or inefficient joins.
  • AI-Assisted Recommendations: Leverages OpenAI for query optimization suggestions, reducing manual effort for DBAs or developers. This is a high-value differentiator for teams lacking deep SQL expertise.
  • Event-Driven Capture: Integrates with Laravel’s query logging system (via DB::listen), making it non-intrusive for existing applications.
  • Modular Design: Configurable thresholds, ignored query types (e.g., EXPLAIN, INSERT), and AI toggle provide granular control over monitoring scope.

Integration Feasibility

  • Laravel Compatibility: Supports Laravel 10–13 and PHP 8.2+, ensuring low friction for modern stacks.
  • Database Agnostic: While recommendations are SQL-agnostic, the package assumes PostgreSQL/MySQL (via EXPLAIN ANALYZE). Teams using SQL Server or Oracle may need adjustments.
  • OpenAI Dependency: Requires an OpenAI API key for AI features, introducing cost and latency considerations (e.g., token limits, API timeouts).
  • Storage Overhead: Logs slow queries in a dedicated table (slow_log), adding ~500MB–1GB storage for high-traffic apps (configurable retention via slower:clean).

Technical Risk

Risk Area Severity Mitigation Strategy
AI API Costs High Disable AI (ai_recommendation=false) or use a budgeted OpenAI key. Monitor usage via openai-php/laravel logs.
Query Sampling Bias Medium Validate threshold (SLOWER_THRESHOLD) against real-world slow queries (e.g., 100ms vs. 1s).
Migration Impact Low Zero-downtime: Install during off-peak hours; test with enabled=false first.
Vendor Lock-in Low Exports raw EXPLAIN data; recommendations are SQL-agnostic.
Performance Overhead Medium Benchmark with enabled=true in staging; ignore high-frequency queries (e.g., ignore_insert_queries).

Key Questions for TPM

  1. AI vs. Manual Review:

    • Should we prioritize cost savings (disable AI) or developer productivity (enable AI)?
    • Tradeoff: AI reduces manual effort but adds API costs (~$0.03–$0.06 per query for GPT-4).
  2. Query Threshold:

    • What defines a "slow" query for our use case? (Default: 10ms vs. production needs like 500ms?)
    • Impact: Lower thresholds capture more noise; higher thresholds miss critical issues.
  3. OpenAI Reliability:

    • How will we handle API rate limits or outages? (Fallback: Disable AI or cache recommendations.)
    • Risk: AI unavailability halts optimization suggestions.
  4. Storage and Retention:

    • How long should we retain slow query logs? (Default: 15 days via slower:clean.)
    • Consideration: Longer retention = more historical data but higher storage costs.
  5. Database Compatibility:

    • Are we using PostgreSQL/MySQL, or will we need to adapt EXPLAIN parsing for other DBs?
    • Example: SQL Server uses SET SHOWPLAN_TEXT ON.
  6. Integration with Observability:

    • Should slow queries trigger alerts (e.g., Slack, PagerDuty) or integrate with existing tools (Datadog, New Relic)?
    • Gap: Package lacks native alerting; requires custom logic.
  7. Premium Features:

    • Would an "Auto Indexer" (planned premium feature) justify the cost for our team?
    • Opportunity: Could reduce manual DBA workload by 30–50%.

Integration Approach

Stack Fit

  • Laravel Ecosystem: Seamless integration with Laravel’s query builder, Eloquent, and scheduler.
  • Database Layer: Works with PostgreSQL/MySQL out-of-the-box; requires minimal tweaks for other DBs.
  • Observability Stack: Complements tools like:
    • Laravel Debugbar (for query inspection).
    • Blackfire (for deep profiling).
    • Sentry (for error tracking).
  • CI/CD: Low impact on deployment pipelines (no breaking changes in v2.x).

Migration Path

  1. Pre-Migration:

    • Audit current slow queries using Laravel’s built-in DB::enableQueryLog().
    • Set up OpenAI API key (if using AI) and test with a sandbox environment.
    • Configure SLOWER_THRESHOLD based on baseline query performance.
  2. Installation:

    composer require halilcosdu/laravel-slower
    php artisan vendor:publish --tag="slower-config"
    php artisan vendor:publish --tag="slower-migrations"
    php artisan migrate
    
    • Note: Run migrations in a separate transaction to avoid locking production tables.
  3. Configuration:

    • Disable AI initially (ai_recommendation=false) to avoid costs during ramp-up.
    • Exclude high-frequency queries (e.g., ignore_insert_queries=true).
    • Schedule commands in app/Console/Kernel.php:
      $schedule->command(\HalilCosdu\Slower\Commands\AnalyzeQuery::class)
               ->dailyAt('3:00')
               ->withoutOverlapping();
      $schedule->command(\HalilCosdu\Slower\Commands\SlowLogCleaner::class)
               ->daily()
               ->runInBackground();
      
  4. Post-Migration:

    • Monitor slow_log table growth and adjust slower:clean frequency.
    • Validate recommendations by applying indexes and measuring impact (e.g., EXPLAIN ANALYZE before/after).

Compatibility

Component Compatibility Notes
Laravel 10–13 Fully supported; no breaking changes in v2.x.
PHP 8.2+ Requires named arguments (PHP 8.0+) and attributes (PHP 8.0+).
Databases PostgreSQL/MySQL: Full EXPLAIN ANALYZE support. SQL Server/Oracle: Partial.
OpenAI API Uses openai-php/laravel v0.18.0; may require API key rotation.
Third-Party No conflicts with Laravel Telescope, Debugbar, or similar query tools.

Sequencing

  1. Phase 1: Monitoring-Only Mode (2–4 weeks):

    • Enable logging (enabled=true), disable AI (ai_recommendation=false).
    • Validate slow query capture and log retention.
  2. Phase 2: AI-Assisted Optimization (1–2 weeks):

    • Enable AI recommendations (ai_recommendation=true).
    • Test cost impact with a subset of queries (e.g., SLOWER_THRESHOLD=500).
  3. Phase 3: Automated Indexing (Future):

    • Evaluate premium "Auto Indexer" feature for high-impact tables.

Operational Impact

Maintenance

  • Configuration Drift: Monitor .env and config/slower.php for changes (e.g., threshold, ignore_* rules).
  • OpenAI Key Rotation: Automate key updates via Laravel’s env() or a secrets manager (e.g., AWS Secrets Manager).
  • Schema Updates: Package migrations are backward-compatible; no downtime required for minor updates.
  • Dependency Updates: Regularly update openai-php/laravel to avoid breaking changes (e.g., API deprecations).

Support

  • Troubleshooting:
    • No Slow Queries Logged: Verify DB::listen is enabled globally (check AppServiceProvider).
    • AI Failures: Check OpenAI API status and openai-php/laravel logs for rate limits.
    • High Storage Usage: Adjust slower:clean days or archive logs to S3.
  • Documentation Gaps:
    • Limited guidance on customizing the AI prompt or handling non-PostgreSQL/MySQL DBs.
    • Workaround: Extend HalilCosdu\Slower\Services\Analyzer for DB-specific logic.

Scaling

  • Performance Impact:
    • Logging Overhead: <1% for most apps; test with enabled=true under load.
    • AI Latency: Recommendations add ~2–5s per query (asynchronous via runInBackground).
  • **High-V
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai