diff --git a/config/config.php b/config/config.php index 9bb072f..3a1bff2 100755 --- a/config/config.php +++ b/config/config.php @@ -6,7 +6,7 @@ return [ 'cache' => [ 'key' => env('QUERY_ADVISER_CACHE_KEY', 'query_adviser_recent'), - 'ttl' => env('QUERY_ADVISER_CACHE_TTL', 60), + 'ttl' => env('QUERY_ADVISER_CACHE_TTL', 3600), 'max_entries' => env('QUERY_ADVISER_CACHE_MAX_ENTRIES', 10000) ] ]; \ No newline at end of file diff --git a/resources/views/explain.blade.php b/resources/views/explain.blade.php new file mode 100644 index 0000000..25e0245 --- /dev/null +++ b/resources/views/explain.blade.php @@ -0,0 +1,109 @@ + + + + + + + Laravel Query Adviser + + +

Query Adviser - EXPLAIN

+Back + + +
+
sql
+
{{Socialblue\LaravelQueryAdviser\Helper\QueryBuilderHelper::combineQueryAndBindings($query['sql'], $query['bindings'])}}
+ +
Time
+
{{$query['time']}}
+
Route
+
{{$query['url']}}
+
+ + + +
+ + +
+
+ @foreach($queryParts as $queryPart) +
+ {{$queryPart->table}} +
+
+
+
Select type
+
{{$queryPart->select_type}}
+ +
Type
+
{{$queryPart->type}}
+ +
Possible keys
+
{{$queryPart->possible_keys}}
+ +
Key used
+
{{$queryPart->key}}
+ +
key len
+
{{$queryPart->key_len}}
+ +
rows
+
{{$queryPart->rows}}
+ +
filtered
+
{{$queryPart->filtered}}
+
+
+ @endforeach + +
+
+ + + + + \ No newline at end of file diff --git a/resources/views/index.blade.php b/resources/views/index.blade.php index 90e644c..9113d89 100644 --- a/resources/views/index.blade.php +++ b/resources/views/index.blade.php @@ -17,13 +17,13 @@ @if(is_array($querys[0])) - @foreach($querys as $query) + @foreach($querys as $key => $query)
- {{Socialblue\LaravelQueryAdviser\Helper\QueryBuilderHelper::combineQueryAndBindings($query['sql'] ?? $query[0], $query['bindings'] ?? $query[1])}} + {{$query['time']}} | {{$query['url']}} | {{Socialblue\LaravelQueryAdviser\Helper\QueryBuilderHelper::combineQueryAndBindings($query['sql'] ?? $query[0], $query['bindings'] ?? $query[1])}}
-
- EXEC | EXPLAIN +
@endforeach diff --git a/routes/web.php b/routes/web.php index b0308eb..c00b976 100644 --- a/routes/web.php +++ b/routes/web.php @@ -3,6 +3,14 @@ Route::get('/query', 'QueryController@index'); +Route::get('/query/explain', 'QueryController@explain'); + + Route::prefix('api')->group(function () { Route::get('/query/get', 'QueryController@get'); -}); \ No newline at end of file +}); + +Route::prefix('api')->group(function () { + Route::get('/query/exec', 'QueryController@exec'); +}); + diff --git a/src/Http/Controllers/QueryController.php b/src/Http/Controllers/QueryController.php index 66df659..3884158 100644 --- a/src/Http/Controllers/QueryController.php +++ b/src/Http/Controllers/QueryController.php @@ -5,6 +5,8 @@ use Illuminate\Routing\Controller; use Illuminate\Http\Request; use Illuminate\Support\Facades\Cache; +use Illuminate\Support\Facades\DB; +use Socialblue\LaravelQueryAdviser\Helper\QueryBuilderHelper; class QueryController extends Controller { @@ -19,4 +21,31 @@ public function get(Request $request) { return Cache::get(config('laravel-query-adviser.cache.key'), []); } + + public function exec(Request $request) + { + $data = Cache::get(config('laravel-query-adviser.cache.key'), []); + + if (isset($data[$request->get('time')][$request->get('time-key')])) { + $query = $data[$request->get('time')][$request->get('time-key')]; + return DB::connection()->select($query['sql'], $query['bindings']); + } + + return false; + } + + + public function explain(Request $request) + { + $data = Cache::get(config('laravel-query-adviser.cache.key'), []); + $queryData = []; + $query = ''; + if (isset($data[$request->get('time')][$request->get('time-key')])) { + $query = $data[$request->get('time')][$request->get('time-key')]; + $queryData = DB::connection()->select('EXPLAIN EXTENDED '. $query['sql'], $query['bindings']); + } + + return view('QueryAdviser::explain', ['queryParts' => $queryData, 'query' => $query]); + } + } diff --git a/src/LaravelQueryAdviserServiceProvider.php b/src/LaravelQueryAdviserServiceProvider.php index 1d3ace2..3edaa01 100755 --- a/src/LaravelQueryAdviserServiceProvider.php +++ b/src/LaravelQueryAdviserServiceProvider.php @@ -57,8 +57,14 @@ public function boot() } DB::listen(function($query) { + + $url = url()->current(); + if (strpos($url, '/query-adviser') === 0) { + return; + } + $data = Cache::get(config('laravel-query-adviser.cache.key'), []); - $data[time()][] = ['sql' => $query->sql, 'bindings' => $query->bindings, 'time' => $query->time]; + $data[time()][] = ['sql' => $query->sql, 'bindings' => $query->bindings, 'time' => $query->time, 'url' => $url]; if (count($data) > config('laravel-query-adviser.cache.max_entries')) { array_shift($data);