PHPackages                             sarfraznawaz2005/aiqueryoptimizer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. sarfraznawaz2005/aiqueryoptimizer

ActiveLaravel-package[Database &amp; ORM](/categories/database)

sarfraznawaz2005/aiqueryoptimizer
=================================

A Laravel package to analyze and optimize SQL queries using AI.

1.0.8(8mo ago)93MITPHPPHP ^8.2CI passing

Since Aug 19Pushed 8mo agoCompare

[ Source](https://github.com/sarfraznawaz2005/aiqueryoptimizer)[ Packagist](https://packagist.org/packages/sarfraznawaz2005/aiqueryoptimizer)[ RSS](/packages/sarfraznawaz2005-aiqueryoptimizer/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (9)Dependencies (7)Versions (10)Used By (0)

[![Latest Version on Packagist](https://camo.githubusercontent.com/c19cb84a814b1cd557128211c76a74e9f5347b4c557069d8e34f16eb792275cb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7361726672617a6e6177617a323030352f616971756572796f7074696d697a65722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/sarfraznawaz2005/aiqueryoptimizer)[![Total Downloads](https://camo.githubusercontent.com/92fe19142f66ca0959a538aa9f8c7838c9cc71f43cc55443671e3ac8e512d89a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7361726672617a6e6177617a323030352f616971756572796f7074696d697a65722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/sarfraznawaz2005/aiqueryoptimizer)[![License: MIT](https://camo.githubusercontent.com/fdf2982b9f5d7489dcf44570e714e3a15fce6253e0cc6b5aa61a075aac2ff71b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d79656c6c6f772e737667)](https://opensource.org/licenses/MIT)

[![Screenshot](screenshot.png)](screenshot.png)

AI Query Optimizer for Laravel
==============================

[](#ai-query-optimizer-for-laravel)

A Laravel development tool that leverages the intelligence of AI to analyze and optimize your application's SQL queries. This package provides real-time, actionable feedback directly in your browser, helping you identify performance bottlenecks and improve your database efficiency.

Features
--------

[](#features)

- **On-Demand Query Analysis**: Get instant feedback on your `SELECT` queries with the click of a button.
- **AI-Powered Suggestions**: Utilizes AI (Gemini or OpenAI) to provide expert-level optimization advice.
- **Intelligent UI**: A non-intrusive, floating button displays the query count and opens a modal for detailed analysis.
- **Table &amp; Index Awareness**: The AI is provided with your database schema context to give relevant and practical suggestions, avoiding redundant index recommendations.
- **Duplicate Query Handling**: Intelligently groups identical queries and shows how many times each was executed.
- **AJAX Support**: Automatically captures queries made during AJAX/fetch requests.
- **Caching**: Caches AI analysis to prevent redundant API calls and reduce costs.
- **Easy Configuration**: Highly configurable, allowing you to specify AI providers, API keys, and application environments.
- **Syntax Highlighting**: Presents SQL queries in a clean, readable format.

Requirements
------------

[](#requirements)

- PHP 8.2+
- Laravel 10, 11, or 12

Installation
------------

[](#installation)

You can install the package via Composer:

```
composer require sarfraznawaz2005/aiqueryoptimizer --dev
```

Next, publish the configuration and assets using the `vendor:publish` command. This will create the `config/ai-query-optimizer.php` file and publish the necessary CSS/JS assets to your `public` directory.

```
php artisan vendor:publish --provider="AIQueryOptimizer\AIQueryOptimizerServiceProvider"
```

Configuration
-------------

[](#configuration)

After publishing, open `config/ai-query-optimizer.php` to configure the package.

### 1. Enable the Package

[](#1-enable-the-package)

By default, the package is enabled. You can disable it by setting the `enabled` option to `false` or by using the `AI_QUERY_OPTIMIZER_ENABLED` environment variable.

```
'enabled' => env('AI_QUERY_OPTIMIZER_ENABLED', true),
```

### 2. Set Allowed Environments

[](#2-set-allowed-environments)

To prevent the package from running in a production environment, it is restricted to specific environments. By default, it only runs in `local`, `development`, and `staging`.

```
'allowed_environments' => ['local', 'development', 'staging'],
```

### 3. Configure Your AI Provider

[](#3-configure-your-ai-provider)

The package supports both Gemini and OpenAI. You need to configure your chosen provider with the appropriate API key and model.

Update your `.env` file, example with Gemini:

```
AI_QUERY_OPTIMIZER_ENABLED=true
AI_PROVIDER=gemini
GEMINI_API_KEY=your_gemini_api_key
GEMINI_MODEL="gemini-2.5-flash"
```

### 4. Caching

[](#4-caching)

Caching is enabled by default to reduce API costs. The analysis for each unique query will be cached for a day. You can disable it or change the duration.

```
'cache' => [
    'enabled' => true,
    'duration_in_minutes' => 60 * 24, // Cache for one day
],
```

Usage
-----

[](#usage)

Once installed and configured, the package works automatically in your specified environments.

1. **Navigate Your Application**: As you use your application, the optimizer will collect all `SELECT` queries in the background.
2. **Floating Button**: You will see a floating "AI Query Check" button at the bottom-right of your screen, showing the number of unique queries captured.
3. **Analyze Queries**: Click the button to open the analysis modal. The package will send the collected queries to the configured AI for analysis.
4. **Review Suggestions**: The modal will display each query along with the AI's analysis and optimization suggestions. Queries that are already optimized are highlighted in green.

How It Works
------------

[](#how-it-works)

The package hooks into Laravel's query execution event to collect all `SELECT` queries. It then injects a small UI component into your application's frontend. When you trigger the analysis, it sends the queries, along with your database table and index metadata, to the AI. The AI, acting as a database expert, returns detailed suggestions which are then displayed in a user-friendly format.

Security
--------

[](#security)

This is a development tool and should **not** be used in a production environment. Always ensure your API keys are stored securely in your `.env` file and are not committed to version control.

Credits
-------

[](#credits)

- [Sarfraz Ahmed](https://github.com/sarfraznawaz2005)
- [All Contributors](https://github.com/sarfraznawaz2005/aiqueryoptimizer/graphs/contributors)

License
-------

[](#license)

Please see the [license file](LICENSE) for more information.

###  Health Score

35

—

LowBetter than 79% of packages

Maintenance59

Moderate activity, may be stable

Popularity9

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

9

Last Release

263d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/3f9bbcf3a6cda5f8bdf9afe10c20f5c6f563fc1a4d628a9af25c5fdec3f9c216?d=identicon)[sarfraznawaz2005](/maintainers/sarfraznawaz2005)

---

Top Contributors

[![sarfraznawaz2005](https://avatars.githubusercontent.com/u/201788?v=4)](https://github.com/sarfraznawaz2005 "sarfraznawaz2005 (31 commits)")

---

Tags

aiartificial-intelligencedatabaselaravellaravel-packageoptimizationoptimizerquerylaraveldatabaseperformanceaisqlqueryopenaiGeminioptimizer

###  Code Quality

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/sarfraznawaz2005-aiqueryoptimizer/health.svg)

```
[![Health](https://phpackages.com/badges/sarfraznawaz2005-aiqueryoptimizer/health.svg)](https://phpackages.com/packages/sarfraznawaz2005-aiqueryoptimizer)
```

###  Alternatives

[bvanhoekelen/performance

PHP performance tool analyser your script on time, memory usage and db query. Support Laravel and Composer for web, web console and command line interfaces.

521774.3k4](/packages/bvanhoekelen-performance)[halilcosdu/laravel-slower

Laravel Slower: Optimize Your DB Queries with AI

40022.0k](/packages/halilcosdu-laravel-slower)[illuminated/db-profiler

Database Profiler for Laravel Web and Console Applications.

168237.4k](/packages/illuminated-db-profiler)[sarfraznawaz2005/indexer

Laravel package to monitor SELECT queries and offer best possible INDEX fields.

562.7k](/packages/sarfraznawaz2005-indexer)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
