mystus/laravel-query-insights 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mystus/laravel-query-insights

Composer 安装命令:

composer require mystus/laravel-query-insights

包简介

Monitor, analyse, and optimise database query performance in Laravel applications with SQL Server support

README 文档

README

Latest Version on Packagist Total Downloads License

A Laravel package for monitoring, analysing, and optimising database query performance. Built from 20+ years of experience optimising SQL Server and MySQL databases in production environments.

Features

  • 🔍 Automatic Query Monitoring - Captures all database queries with execution times
  • Slow Query Detection - Configurable threshold for identifying performance bottlenecks
  • 🔄 N+1 Query Detection - Automatically identifies potential N+1 query patterns
  • 💡 Actionable Suggestions - Get specific recommendations for query optimisation
  • 🗄️ SQL Server Support - Includes SQL Server-specific optimisation hints
  • 📊 Artisan Reporting - Generate reports from stored query data
  • 🎯 Zero Configuration - Works out of the box with sensible defaults

Installation

composer require mystus/laravel-query-insights

The package will auto-register its service provider.

Publish Configuration (Optional)

php artisan vendor:publish --tag=query-insights-config

Quick Start

Query Insights starts monitoring automatically. Access the data anywhere in your application:

use Mystus\QueryInsights\Facades\QueryInsights;

// Get all recorded queries
$queries = QueryInsights::getQueries();

// Get only slow queries
$slowQueries = QueryInsights::getSlowQueries();

// Get queries with optimisation suggestions
$needsWork = QueryInsights::getQueriesWithSuggestions();

// Get a summary
$summary = QueryInsights::getSummary();

Configuration

// config/query-insights.php

return [
    // Enable/disable monitoring
    'enabled' => env('QUERY_INSIGHTS_ENABLED', true),

    // Queries slower than this (ms) are flagged
    'slow_threshold' => env('QUERY_INSIGHTS_SLOW_THRESHOLD', 100),

    // Log slow queries to your log file
    'log_slow_queries' => env('QUERY_INSIGHTS_LOG_SLOW', true),

    // Store queries for later analysis
    'store_queries' => env('QUERY_INSIGHTS_STORE', false),

    // Days to retain stored data
    'retention_days' => env('QUERY_INSIGHTS_RETENTION_DAYS', 7),

    // Similar queries before N+1 warning
    'n_plus_one_threshold' => env('QUERY_INSIGHTS_N_PLUS_ONE_THRESHOLD', 5),
];

What It Detects

Performance Issues

Issue Severity Description
SELECT * usage Medium Recommends selecting specific columns
N+1 queries High Detects repeated similar queries
Leading wildcard LIKE Medium LIKE '%term' prevents index usage
Subqueries in WHERE Medium Suggests JOIN alternatives
ORDER BY without LIMIT Low Warns about potential memory issues

SQL Server Specific

Issue Severity Description
Implicit conversions High Type mismatches that prevent index usage
Missing NOLOCK hints Low Suggests for read-heavy queries

Safety Checks

Issue Severity Description
UPDATE without WHERE Critical Warns about full-table updates
DELETE without WHERE Critical Warns about full-table deletes

Usage Examples

Display in Development

Add to your AppServiceProvider or create a custom middleware:

use Mystus\QueryInsights\Facades\QueryInsights;

// In a view composer or at end of request
$summary = QueryInsights::getSummary();

// $summary contains:
// [
//     'total_queries' => 45,
//     'total_time' => 234.56,
//     'average_time' => 5.21,
//     'slow_queries' => 2,
//     'queries_with_suggestions' => 5,
//     'slowest_query' => [...],
// ]

Temporary Monitoring

use Mystus\QueryInsights\Facades\QueryInsights;

// Disable during bulk operations
QueryInsights::disable();

// ... bulk insert operations ...

QueryInsights::enable();

Adjust Threshold Dynamically

// Be more strict for critical paths
QueryInsights::setSlowThreshold(50);

// Process order...

// Reset for less critical operations
QueryInsights::setSlowThreshold(200);

Generate Reports

Enable storage in your config or .env:

QUERY_INSIGHTS_STORE=true

Then run reports:

# Today's report
php artisan query-insights:report

# Specific date
php artisan query-insights:report --date=2025-01-02

# Only slow queries
php artisan query-insights:report --slow-only

# Only queries with suggestions
php artisan query-insights:report --with-suggestions

Testing

composer test

Real-World Performance Tips

This package was built from experience optimising databases serving 170+ hospitals with millions of records. Here are some hard-won lessons:

  1. Indexes aren't magic - They help reads but hurt writes. Profile your actual workload.

  2. N+1 is the silent killer - A page loading 100 products with $product->category->name makes 101 queries. Use eager loading.

  3. COUNT(*) on large tables is expensive - Consider caching counts or using estimates for UI.

  4. SQL Server parameter sniffing - First execution plan gets cached. Use OPTION (RECOMPILE) for highly variable queries.

  5. Don't trust ORMs blindly - Always check ->toSql() or enable query logging during development.

Contributing

Contributions are welcome! Please see CONTRIBUTING.md for details.

License

The MIT License (MIT). Please see License File for more information.

Credits

  • Matthew Fritz
  • Built with lessons learned from healthcare technology systems in South Africa

mystus/laravel-query-insights 适用场景与选型建议

mystus/laravel-query-insights 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 01 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「database」 「performance」 「monitoring」 「laravel」 「sql-server」 「query-optimization」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 mystus/laravel-query-insights 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 mystus/laravel-query-insights 我们能提供哪些服务?
定制开发 / 二次开发

基于 mystus/laravel-query-insights 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 23
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-03