Skip to content

Commit f1d0eb6

Browse files
committed
feat: display size of each index and paint a color bar behind it
Refs #1500
1 parent a434066 commit f1d0eb6

5 files changed

Lines changed: 64 additions & 1 deletion

File tree

source/dbconnection.pas

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ TTableKey = class(TPersistent)
8989
public
9090
Name, OldName: String;
9191
IndexType, OldIndexType, Algorithm, Comment: String;
92+
Size: Int64;
9293
Columns, SubParts, Collations: TStringList;
9394
Modified, Added, Visible: Boolean;
9495
constructor Create(AOwner: TDBConnection);
@@ -110,6 +111,7 @@ TTableKey = class(TPersistent)
110111
TTableKeyList = class(TObjectList<TTableKey>)
111112
public
112113
procedure Assign(Source: TTableKeyList);
114+
function MaxSize: Int64;
113115
end;
114116
TKeyCache = TDictionary<String,TTableKeyList>;
115117

@@ -6174,6 +6176,7 @@ function TMySQLConnection.GetTableKeys(Table: TDBObject): TTableKeyList;
61746176
var
61756177
KeyQuery, ColQuery: TDBQuery;
61766178
NewKey: TTableKey;
6179+
SizeQuery: String;
61776180
begin
61786181
Result := TTableKeyList.Create(True);
61796182

@@ -6252,7 +6255,20 @@ function TMySQLConnection.GetTableKeys(Table: TDBObject): TTableKeyList;
62526255
NewKey.Visible := SameText(KeyQuery.Col('Visible'), 'yes')
62536256
else if KeyQuery.ColumnExists('Ignored') then // mariadb 10.6
62546257
NewKey.Visible := SameText(KeyQuery.Col('Ignored'), 'NO');
6258+
6259+
if FSqlProvider.Has(qIndexSize) then begin
6260+
try
6261+
SizeQuery := FSqlProvider.GetSql(qIndexSize, [
6262+
EscapeString(Table.Database),
6263+
EscapeString(Table.Name),
6264+
EscapeString(NewKey.Name)
6265+
]);
6266+
NewKey.Size := StrToInt64Def(GetVar(SizeQuery), NewKey.Size);
6267+
except
6268+
end;
6269+
end;
62556270
end;
6271+
62566272
if KeyQuery.ColumnExists('Expression') and (not KeyQuery.IsNull('Expression')) then begin
62576273
// Functional key part: enclose expression within parentheses to distinguish them from columns (issue #1777)
62586274
NewKey.Columns.Add('('+KeyQuery.Col('Expression')+')');
@@ -11092,6 +11108,7 @@ constructor TTableKey.Create(AOwner: TDBConnection);
1109211108
Subparts.OnChange := Modification;
1109311109
Collations.OnChange := Modification;
1109411110
Visible := True;
11111+
Size := -1
1109511112
end;
1109611113

1109711114
destructor TTableKey.Destroy;
@@ -11115,6 +11132,7 @@ procedure TTableKey.Assign(Source: TPersistent);
1111511132
Algorithm := s.Algorithm;
1111611133
Comment := s.Comment;
1111711134
Visible := s.Visible;
11135+
Size := s.Size;
1111811136
Columns.Assign(s.Columns);
1111911137
SubParts.Assign(s.SubParts);
1112011138
Collations.Assign(s.Collations);
@@ -11264,6 +11282,15 @@ procedure TTableKeyList.Assign(Source: TTableKeyList);
1126411282
end;
1126511283
end;
1126611284

11285+
function TTableKeyList.MaxSize: Int64;
11286+
var
11287+
Item: TTableKey;
11288+
begin
11289+
Result := -1;
11290+
for Item in Self do begin
11291+
Result := Max(Result, Item.Size);
11292+
end;
11293+
end;
1126711294

1126811295

1126911296

source/dbstructures.mysql.pas

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3428,6 +3428,13 @@ function TMySqlProvider.GetSql(AId: TQueryId): string;
34283428
'CAST(%s AS CHAR)',
34293429
''
34303430
);
3431+
qIndexSize: Result := IfThen(
3432+
(IsMySQL and (FServerVersion >= 50600)) or IsMariaDB,
3433+
'SELECT stat_value * @@innodb_page_size' +
3434+
' FROM mysql.innodb_index_stats'+
3435+
' WHERE database_name=%s AND table_name=%s AND index_name=%s AND stat_name=''size''',
3436+
''
3437+
);
34313438
else Result := inherited;
34323439
end;
34333440
end;

source/dbstructures.pas

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ interface
5252
qShowFunctionStatus, qShowProcedureStatus, qShowTriggers, qShowEvents, qShowCreateTrigger,
5353
qHelpKeyword, qShowWarnings, qGetEnumTypes,
5454
qDropUser, qCreateRole, qDropRole, qReloadPrivileges, qGrantRole, qRevokeRole, qSetDefaultRole,
55-
qAutoInc, qIndexVisible, qIndexInvisible, qGetAuthPlugins, qCastAsText);
55+
qAutoInc, qIndexVisible, qIndexInvisible, qGetAuthPlugins, qCastAsText, qIndexSize);
5656
TSqlProvider = class
5757
strict protected
5858
FNetType: TNetType;

source/table_editor.dfm

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ object frmTableEditor: TfrmTableEditor
242242
Margins.Top = 0
243243
Margins.Bottom = 0
244244
Align = alClient
245+
Alignment = taRightJustify
245246
DefaultNodeHeight = 19
246247
DragMode = dmAutomatic
247248
EditDelay = 0
@@ -255,6 +256,7 @@ object frmTableEditor: TfrmTableEditor
255256
TreeOptions.MiscOptions = [toAcceptOLEDrop, toEditable, toFullRepaintOnResize, toGridExtensions, toInitOnSave, toToggleOnDblClick, toWheelPanning, toEditOnClick]
256257
TreeOptions.PaintOptions = [toHotTrack, toShowButtons, toShowDropmark, toShowRoot, toShowTreeLines, toShowVertGridLines, toThemeAware, toUseBlendedImages, toGhostedIfUnfocused, toFullVertGridLines, toUseExplorerTheme, toHideTreeLinesIfThemed]
257258
TreeOptions.SelectionOptions = [toExtendedFocus, toRightClickSelect]
259+
OnBeforeCellPaint = treeIndexesBeforeCellPaint
258260
OnBeforePaint = treeIndexesBeforePaint
259261
OnClick = AnyTreeClick
260262
OnCreateEditor = treeIndexesCreateEditor
@@ -301,6 +303,12 @@ object frmTableEditor: TfrmTableEditor
301303
item
302304
Position = 5
303305
Text = 'Visibility'
306+
end
307+
item
308+
Alignment = taRightJustify
309+
Position = 6
310+
Text = 'Size'
311+
Width = 10
304312
end>
305313
end
306314
object tlbIndexes: TToolBar

source/table_editor.pas

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ TfrmTableEditor = class(TFrame)
117117
procedure listColumnsPaintText(Sender: TBaseVirtualTree; const TargetCanvas: TCanvas; Node: PVirtualNode;
118118
Column: TColumnIndex; TextType: TVSTTextType);
119119
procedure listColumnsCreateEditor(Sender: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex; out EditLink: IVTEditLink);
120+
procedure treeIndexesBeforeCellPaint(Sender: TBaseVirtualTree;
121+
TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex;
122+
CellPaintMode: TVTCellPaintMode; CellRect: TRect; var ContentRect: TRect);
120123
procedure treeIndexesInitNode(Sender: TBaseVirtualTree; ParentNode, Node: PVirtualNode; var InitialStates: TVirtualNodeInitStates);
121124
procedure treeIndexesGetText(Sender: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType;
122125
var CellText: String);
@@ -243,6 +246,7 @@ TfrmTableEditor = class(TFrame)
243246
const IndexColNumComment = 3;
244247
const IndexColNumDirection = 4;
245248
const IndexColNumVisibility = 5;
249+
const IndexColNumSize = 6;
246250
procedure ValidateColumnControls;
247251
procedure ValidateIndexControls;
248252
procedure MoveFocusedIndexPart(NewIdx: Cardinal);
@@ -1935,6 +1939,19 @@ procedure TfrmTableEditor.listColumnsCreateEditor(Sender: TBaseVirtualTree;
19351939
end;
19361940
end;
19371941

1942+
procedure TfrmTableEditor.treeIndexesBeforeCellPaint(Sender: TBaseVirtualTree;
1943+
TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex;
1944+
CellPaintMode: TVTCellPaintMode; CellRect: TRect; var ContentRect: TRect);
1945+
var
1946+
Key: TTableKey;
1947+
begin
1948+
// Only paint bar in size column
1949+
if Column in [IndexColNumSize] then begin
1950+
Key := FKeys[Node.Index];
1951+
MainForm.PaintColorBar(Key.Size, FKeys.MaxSize, TargetCanvas, CellRect);
1952+
end;
1953+
end;
1954+
19381955

19391956
procedure TfrmTableEditor.editNumEditChange(Sender: TObject);
19401957
var
@@ -2147,6 +2164,10 @@ procedure TfrmTableEditor.treeIndexesGetText(Sender: TBaseVirtualTree;
21472164
DBObject.Connection.SqlProvider.GetSql(qIndexVisible),
21482165
DBObject.Connection.SqlProvider.GetSql(qIndexInvisible)
21492166
);
2167+
IndexColNumSize: if TblKey.Size >= 0 then
2168+
CellText := FormatByteNumber(TblKey.Size)
2169+
else
2170+
CellText := '';
21502171
end;
21512172
end;
21522173
1: begin

0 commit comments

Comments
 (0)