Skip to content

Commit

Permalink
Merge pull request #17 from dimafe6/main
Browse files Browse the repository at this point in the history
Added explain analyze query plan to recommendation prompt
  • Loading branch information
halilcosdu authored Aug 10, 2024
2 parents 76199a0 + aaea166 commit 1529d09
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 19 deletions.
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

0 comments on commit 1529d09

Please sign in to comment.