Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added explain analyze query plan to recommendation prompt #17

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion config/slower.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
],
'ai_recommendation' => env('SLOWER_AI_RECOMMENDATION', true),
'recommendation_model' => env('SLOWER_AI_RECOMMENDATION_MODEL', 'gpt-4'),
'recommendation_use_explain' => env('SLOWER_AI_RECOMMENDATION_USE_EXPLAIN', true),
'ignore_explain_queries' => env('SLOWER_IGNORE_EXPLAIN_QUERIES', true),
'ignore_insert_queries' => env('SLOWER_IGNORE_INSERT_QUERIES', true),
'open_ai' => [
'api_key' => env('OPENAI_API_KEY'),
'organization' => env('OPENAI_ORGANIZATION'),
'request_timeout' => env('OPENAI_TIMEOUT'),
],
'prompt' => env('SLOWER_PROMPT', 'As a distinguished database optimization expert, your expertise is invaluable for refining SQL queries to achieve maximum efficiency. Please examine the SQL statement provided below. Based on your analysis, could you recommend sophisticated indexing techniques or query modifications that could significantly improve performance and scalability?'),
'prompt' => env('SLOWER_PROMPT', 'As a distinguished database optimization expert, your expertise is invaluable for refining SQL queries to achieve maximum efficiency. Schema json provide list of indexes and column definitions for each table in query. Also analyse the output of EXPLAIN ANALYSE and provide recommendations to optimize query. Please examine the SQL statement provided below including EXPLAIN ANALYSE query plan. Based on your analysis, could you recommend sophisticated indexing techniques or query modifications that could significantly improve performance and scalability?'),
];
52 changes: 37 additions & 15 deletions src/Services/RecommendationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,24 @@ public function __construct(protected Client $client)

public function getRecommendation($record): ?string
{
$schema = $this->extractIndexesAndSchemaFromRecord($record);
$userMessage = 'The query execution took ' . $record->time . ' milliseconds.' . PHP_EOL .
'Connection: ' . $record->connection . PHP_EOL .
'Connection Name: ' . $record->connection_name . PHP_EOL .
'Schema: ' . json_encode($schema, JSON_PRETTY_PRINT) . PHP_EOL .
'Sql: ' . $record->sql . PHP_EOL;

[$indexes, $schema] = $this->extractIndexesAndSchemaFromRecord($record);
if (config('slower.recommendation_use_explain', false)) {
$plan = collect(DB::select('explain analyse ' . $record->raw_sql))->implode('QUERY PLAN', PHP_EOL);

$userMessage .= 'EXPLAIN ANALYSE output: ' . $plan . PHP_EOL;
}

$result = $this->client->chat()->create([
'model' => config('slower.recommendation_model', 'gpt-4'),
'messages' => [
['role' => 'system', 'content' => config('slower.prompt')],
['role' => 'user', 'content' => 'The query execution took '.$record->time.' milliseconds.'.PHP_EOL.
'Connection: '.$record->connection.PHP_EOL.
'Current Indexes: '.json_encode($indexes, JSON_PRETTY_PRINT).PHP_EOL.
'Schema: '.json_encode($schema, JSON_PRETTY_PRINT).PHP_EOL.
'Connection Name: '.$record->connection_name.PHP_EOL.
'Sql: '.$record->raw_sql,
],
['role' => 'user', 'content' => $userMessage],
],
]);

Expand All @@ -43,18 +47,36 @@ public function getRecommendation($record): ?string

private function extractIndexesAndSchemaFromRecord($record): array
{
$schemaBuilder = DB::connection($record->getConnectionName())->getSchemaBuilder();
$schemaBuilder = DB::connection($record->connection_name)->getSchemaBuilder();

$columns = $schemaBuilder->getColumnListing($record->getTable());
$schema = [];

$indexes = $schemaBuilder->getIndexes($record->getTable());
$tables = $this->getTableNamesFromRawQuery($record->raw_sql);
foreach ($tables as $tableName) {
$columns = $schemaBuilder->getColumnListing($tableName);
$schema[$tableName]['indexes'] = $schemaBuilder->getIndexes($tableName);

$schema = [];
foreach ($columns as $column) {
$schema[$tableName]['columns'][] = [$column => $schemaBuilder->getColumnType($tableName, $column)];
}
}

return $schema;
}

private function getTableNamesFromRawQuery(string $sqlQuery): array
{
// Regular expression to match table names
$pattern = '/(?:FROM|JOIN|INTO|UPDATE)\s+(\S+)(?:\s+(?:AS\s+)?\w+)?(?:\s+ON\s+[^ ]+)?/i';

preg_match_all($pattern, $sqlQuery, $matches);

foreach ($columns as $column) {
$schema[$column] = $schemaBuilder->getColumnType($record->getTable(), $column);
// Extract table names from the matches
$tableNames = [];
foreach ($matches[1] as $tableName) {
$tableNames[] = str_replace(['`', '"'], '', $tableName);
}

return [$indexes, $schema];
return array_unique($tableNames);
}
}
18 changes: 15 additions & 3 deletions src/SlowerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,21 @@ public function packageRegistered(): void
private function registerDatabaseListener(): void
{
if (config('slower.enabled')) {
DB::whenQueryingForLongerThan(config('slower.threshold', 10000), function (Connection $connection, QueryExecuted $event) {
$this->createRecord($event, $connection);
$this->notify($event, $connection);
DB::listen(function (QueryExecuted $event) {
if($event->time < config('slower.threshold', 10000)) {
return;
}

if(config('slower.ignore_explain_queries', true) && Str::startsWith($event->sql, 'EXPLAIN')) {
return;
}

if(config('slower.ignore_insert_queries', true) && stripos($event->sql, 'insert') === 0) {
return;
}

$this->createRecord($event, $event->connection);
$this->notify($event, $event->connection);
});
}
}
Expand Down