Skip to content

Commit

Permalink
Merge pull request #15 from socialblue/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
mbroersen committed Jun 6, 2019
2 parents 80bdff1 + ecf7765 commit fbf5b59
Show file tree
Hide file tree
Showing 8 changed files with 1,172 additions and 29 deletions.
18 changes: 15 additions & 3 deletions resources/views/explain.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,19 @@
<summary>
<dl>
<dt>sql</dt>
<dd>{{Socialblue\LaravelQueryAdviser\Helper\QueryBuilderHelper::combineQueryAndBindings($query['sql'], $query['bindings'])}}</dd>
<dd>{!!
Socialblue\LaravelQueryAdviser\Helper\SqlFormatter::format(Socialblue\LaravelQueryAdviser\Helper\QueryBuilderHelper::combineQueryAndBindings($query['sql'], $query['bindings']))
!!}</dd>

<dt>sql</dt>
<dd>{!!
Socialblue\LaravelQueryAdviser\Helper\SqlFormatter::format(Socialblue\LaravelQueryAdviser\Helper\QueryBuilderHelper::combineQueryAndBindings($query['sql'], $query['bindings']))
!!}</dd>

<dt onclick="document.querySelector('#opt').style.display='block';">show optimized query</dt>
<dd id="opt" style="display: none">{!!
Socialblue\LaravelQueryAdviser\Helper\SqlFormatter::format($optimized)
!!}</dd>

<dt>Time</dt>
<dd>{{$query['time']}}</dd>
Expand All @@ -36,10 +48,10 @@
<div class="query">
<dl>
<dt>Select type</dt>
<dd>{{$queryPart->select_type}}</dd>
<dd>{{Socialblue\LaravelQueryAdviser\Enum\SelectType::get($queryPart->select_type) }}</dd>

<dt>Type</dt>
<dd>{{$queryPart->type}}</dd>
<dd>{{Socialblue\LaravelQueryAdviser\Enum\JoinType::get($queryPart->type)}}</dd>

<dt>Possible keys</dt>
<dd>{{$queryPart->possible_keys}}</dd>
Expand Down
4 changes: 2 additions & 2 deletions resources/views/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
@foreach($querys as $key => $query)
<div class="query">
<div class="text">
{{$query['time']}} | {{$query['url']}} | {{Socialblue\LaravelQueryAdviser\Helper\QueryBuilderHelper::combineQueryAndBindings($query['sql'] ?? $query[0], $query['bindings'] ?? $query[1])}}
{{$query['time']}} | {{$query['url']}} | {!!Socialblue\LaravelQueryAdviser\Helper\SqlFormatter::format(Socialblue\LaravelQueryAdviser\Helper\QueryBuilderHelper::combineQueryAndBindings($query['sql'] ?? $query[0], $query['bindings'] ?? $query[1]))!!}
</div>
<div class="btn" data-time="{{$time}}}" data-time-key="{{$key}}">
<a target="_blank" href="/query-adviser/api/query/exec/?time={{$time}}&time-key={{$key}}">EXEC</a> | <a href="/query-adviser/query/explain/?time={{$time}}&time-key={{$key}}">EXPLAIN</a>
Expand Down Expand Up @@ -58,7 +58,7 @@
.query .text {
position: relative;
width: calc(95% - 100px);
max-height: 40px;
max-height: 100px;
padding: 10px 4px 4px 10px;
overflow-y: scroll;
}
Expand Down
30 changes: 30 additions & 0 deletions src/Enum/JoinType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Socialblue\LaravelQueryAdviser\Enum;

final class JoinType
{
public CONST SYSTEM = 'the table has only zero or one row';
public CONST CONST = 'the table has only one matching row which is indexed. This is the fastest type of join because the table only has to be read once and the column’s value can be treated as a constant when joining other tables';
public CONST EQ_REF = 'all parts of an index are used by the join and the index is PRIMARY KEY or UNIQUE NOT NULL. This is the next best possible join type.';
public CONST REF = 'all of the matching rows of an indexed column are read for each combination of rows from the previous table. This type of join appears for indexed columns compared using = or <=> operators.';
public CONST FULLTEXT = 'the join uses the table’s FULLTEXT index';
public CONST REF_OR_NULL = 'this is the same as ref but also contains rows with a null value for the column.';
public CONST DEPENDENT_UNION = 'the second or later SELECT of a UNION is dependent on an outer query';
public CONST INDEX_MERGE = 'SELECT is a result of a UNION';
public CONST UNION_RESULT = 'the join uses a list of indexes to produce the result set. The key column of EXPLAIN‘s output will contain the keys used';
public CONST UNIQUE_SUBQUERY = 'an IN subquery returns only one result from the table and makes use of the primary key';
public CONST INDEX_SUBQUERY = 'the same as unique_subquery but returns more than one result row.';
public CONST RANGE = 'an index is used to find matching rows in a specific range, typically when the key column is compared to a constant using operators like BETWEEN, IN, >, >=, etc.';
public CONST INDEX = 'he entire index tree is scanned to find matching row';
public CONST ALL = 'the entire table is scanned to find matching rows for the join. This is the worst join type and usually indicates the lack of appropriate indexes on the table.';

public static function get($type):string
{
$type = strtoupper($type);
$type = str_replace(" ", "_", $type);

return (string) constant("self::$type");
}

}
25 changes: 25 additions & 0 deletions src/Enum/SelectType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Socialblue\LaravelQueryAdviser\Enum;

final class SelectType
{
public CONST SIMPLE = 'simple SELECT query without any subqueries or UNIONs';
public CONST PRIMARY = 'SELECT is in the outermost query in a JOIN';
public CONST SUBQUERY = 'SELECT is part of a subquery within a FROM clause';
public CONST DEPENDENT_SUBQUERY = 'a subquery which is dependent upon on outer query';
public CONST UNCACHEABLE_SUBQUERY = 'a subquery which is not cacheable (there are certain conditions for a query to be cacheable)';
public CONST UNION = 'the SELECT is the second or later statement of a UNION';
public CONST DEPENDENT_UNION = 'the second or later SELECT of a UNION is dependent on an outer query';
public CONST UNION_RESULT = 'SELECT is a result of a UNION';
public CONST DERIVED = 'DERIVED';


public static function get($type):string
{
$type = strtoupper($type);
$type = str_replace(" ", "_", $type);

return (string) constant("self::$type");
}
}
26 changes: 4 additions & 22 deletions src/Helper/QueryBuilderHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,29 +49,8 @@ public static function combineQueryAndBindings($sql, $bindings)
*
id – a sequential identifier for each SELECT within the query (for when you have nested subqueries)
select_type – the type of SELECT query. Possible values are:
SIMPLE – the query is a simple SELECT query without any subqueries or UNIONs
PRIMARY – the SELECT is in the outermost query in a JOIN
DERIVED – the SELECT is part of a subquery within a FROM clause
SUBQUERY – the first SELECT in a subquery
DEPENDENT SUBQUERY – a subquery which is dependent upon on outer query
UNCACHEABLE SUBQUERY – a subquery which is not cacheable (there are certain conditions for a query to be cacheable)
UNION – the SELECT is the second or later statement of a UNION
DEPENDENT UNION – the second or later SELECT of a UNION is dependent on an outer query
UNION RESULT – the SELECT is a result of a UNION
table – the table referred to by the row
type – how MySQL joins the tables used. This is one of the most insightful fields in the output because it can indicate missing indexes or how the query is written should be reconsidered. Possible values are:
system – the table has only zero or one row
const – the table has only one matching row which is indexed. This is the fastest type of join because the table only has to be read once and the column’s value can be treated as a constant when joining other tables.
eq_ref – all parts of an index are used by the join and the index is PRIMARY KEY or UNIQUE NOT NULL. This is the next best possible join type.
ref – all of the matching rows of an indexed column are read for each combination of rows from the previous table. This type of join appears for indexed columns compared using = or <=> operators.
fulltext – the join uses the table’s FULLTEXT index.
ref_or_null – this is the same as ref but also contains rows with a null value for the column.
index_merge – the join uses a list of indexes to produce the result set. The key column of EXPLAIN‘s output will contain the keys used.
unique_subquery – an IN subquery returns only one result from the table and makes use of the primary key.
index_subquery – the same as unique_subquery but returns more than one result row.
range – an index is used to find matching rows in a specific range, typically when the key column is compared to a constant using operators like BETWEEN, IN, >, >=, etc.
index – the entire index tree is scanned to find matching rows.
all – the entire table is scanned to find matching rows for the join. This is the worst join type and usually indicates the lack of appropriate indexes on the table.
possible_keys – shows the keys that can be used by MySQL to find rows from the table, though they may or may not be used in practice. In fact, this column can often help in optimizing queries since if the column is NULL, it indicates no relevant indexes could be found.
key – indicates the actual index used by MySQL. This column may contain an index that is not listed in the possible_key column. MySQL optimizer always look for an optimal key that can be used for the query. While joining many tables, it may figure out some other keys which is not listed in possible_key but are more optimal.
key_len – indicates the length of the index the Query Optimizer chose to use. For example, a key_len value of 4 means it requires memory to store four characters. Check out MySQL’s data type storage requirements to know more about this.
Expand Down Expand Up @@ -123,6 +102,9 @@ protected static function addBindingsToQueryByBuilder($builder): string




public static function print_pretty($sql)
{
explode($sql, " ");
}

}
Loading

0 comments on commit fbf5b59

Please sign in to comment.