|
1 | | -<h4>🐘 MysqlGenius</h4> |
| 1 | +<h4>🐘 MySQLGenius</h4> |
2 | 2 |
|
3 | 3 | <div class="mg-tabs"> |
4 | 4 | <button class="mg-tab active" data-tab="visual">Visual Builder</button> |
5 | 5 | <button class="mg-tab" data-tab="sql">SQL Query</button> |
6 | 6 | <button class="mg-tab" data-tab="slow">Slow Queries</button> |
| 7 | + <button class="mg-tab" data-tab="indexes">Duplicate Indexes</button> |
| 8 | + <button class="mg-tab" data-tab="sizes">Table Sizes</button> |
7 | 9 | </div> |
8 | 10 |
|
9 | 11 | <!-- Visual Builder Tab --> |
|
123 | 125 | </div> |
124 | 126 | </div> |
125 | 127 |
|
| 128 | +<!-- Duplicate Indexes Tab --> |
| 129 | +<div class="mg-tab-content" id="tab-indexes"> |
| 130 | + <div class="mg-row" style="justify-content:space-between;align-items:center;margin-bottom:12px;"> |
| 131 | + <div class="mg-text-muted">Indexes whose columns are a left-prefix of another index on the same table. These are redundant and can safely be dropped. <span id="dup-count" class="mg-badge mg-badge-secondary"></span></div> |
| 132 | + <button id="dup-refresh" class="mg-btn mg-btn-outline-secondary mg-btn-sm">↻ Refresh</button> |
| 133 | + </div> |
| 134 | + <div id="dup-loading" class="mg-text-center mg-hidden"><span class="mg-spinner"></span> Scanning indexes...</div> |
| 135 | + <div id="dup-empty" class="mg-text-center mg-text-muted mg-hidden">No duplicate indexes found.</div> |
| 136 | + <div id="dup-table-wrapper" class="mg-table-wrap mg-hidden"> |
| 137 | + <table class="mg-table"> |
| 138 | + <thead> |
| 139 | + <tr> |
| 140 | + <th>Table</th> |
| 141 | + <th>Duplicate Index</th> |
| 142 | + <th>Columns</th> |
| 143 | + <th>Covered By</th> |
| 144 | + <th>Covered Columns</th> |
| 145 | + <th>DROP Statement</th> |
| 146 | + </tr> |
| 147 | + </thead> |
| 148 | + <tbody id="dup-tbody"></tbody> |
| 149 | + </table> |
| 150 | + </div> |
| 151 | +</div> |
| 152 | + |
| 153 | +<!-- Table Sizes Tab --> |
| 154 | +<div class="mg-tab-content" id="tab-sizes"> |
| 155 | + <div class="mg-row" style="justify-content:space-between;align-items:center;margin-bottom:12px;"> |
| 156 | + <div class="mg-text-muted">Table sizes from <code>information_schema</code>, sorted by total size. Row counts are estimates. <span id="sizes-total" class="mg-badge mg-badge-secondary"></span></div> |
| 157 | + <button id="sizes-refresh" class="mg-btn mg-btn-outline-secondary mg-btn-sm">↻ Refresh</button> |
| 158 | + </div> |
| 159 | + <div id="sizes-loading" class="mg-text-center mg-hidden"><span class="mg-spinner"></span> Loading...</div> |
| 160 | + <div id="sizes-table-wrapper" class="mg-table-wrap mg-hidden"> |
| 161 | + <table class="mg-table"> |
| 162 | + <thead> |
| 163 | + <tr> |
| 164 | + <th>Table</th> |
| 165 | + <th style="text-align:right">Rows (est.)</th> |
| 166 | + <th style="text-align:right">Data</th> |
| 167 | + <th style="text-align:right">Indexes</th> |
| 168 | + <th style="text-align:right">Total</th> |
| 169 | + <th style="text-align:right">Fragmented</th> |
| 170 | + <th style="width:200px"></th> |
| 171 | + </tr> |
| 172 | + </thead> |
| 173 | + <tbody id="sizes-tbody"></tbody> |
| 174 | + </table> |
| 175 | + </div> |
| 176 | +</div> |
| 177 | + |
126 | 178 | <!-- Explain Results --> |
127 | 179 | <div id="explain-results" class="mg-mt mg-hidden"> |
128 | 180 | <div class="mg-card"> |
|
176 | 228 | explain: '<%= mysql_genius.explain_path %>', |
177 | 229 | suggest: '<%= mysql_genius.suggest_path %>', |
178 | 230 | optimize: '<%= mysql_genius.optimize_path %>', |
179 | | - slow_queries: '<%= mysql_genius.slow_queries_path %>' |
| 231 | + slow_queries: '<%= mysql_genius.slow_queries_path %>', |
| 232 | + duplicate_indexes: '<%= mysql_genius.duplicate_indexes_path %>', |
| 233 | + table_sizes: '<%= mysql_genius.table_sizes_path %>' |
180 | 234 | }; |
181 | 235 |
|
182 | 236 | var csrfMeta = document.querySelector('meta[name="csrf-token"]'); |
|
251 | 305 | tab.classList.add('active'); |
252 | 306 | el('tab-' + tab.dataset.tab).classList.add('active'); |
253 | 307 | if (tab.dataset.tab === 'slow') loadSlowQueries(); |
| 308 | + if (tab.dataset.tab === 'indexes') loadDuplicateIndexes(); |
| 309 | + if (tab.dataset.tab === 'sizes') loadTableSizes(); |
254 | 310 | }); |
255 | 311 | }); |
256 | 312 |
|
|
651 | 707 | } |
652 | 708 | }); |
653 | 709 |
|
| 710 | + // --- Duplicate Indexes --- |
| 711 | + |
| 712 | + function loadDuplicateIndexes() { |
| 713 | + show(el('dup-loading')); |
| 714 | + hide(el('dup-empty')); hide(el('dup-table-wrapper')); |
| 715 | + el('dup-tbody').innerHTML = ''; |
| 716 | + |
| 717 | + ajaxGet(ROUTES.duplicate_indexes, {}, function(data) { |
| 718 | + hide(el('dup-loading')); |
| 719 | + if (!data || !data.length) { show(el('dup-empty')); el('dup-count').textContent = '0'; return; } |
| 720 | + el('dup-count').textContent = data.length + ' found'; |
| 721 | + el('dup-tbody').innerHTML = data.map(function(d) { |
| 722 | + var dropSql = 'ALTER TABLE `' + d.table + '` DROP INDEX `' + d.duplicate_index + '`;'; |
| 723 | + return '<tr>' + |
| 724 | + '<td><strong>' + escHtml(d.table) + '</strong></td>' + |
| 725 | + '<td><code>' + escHtml(d.duplicate_index) + '</code>' + (d.unique ? ' <span class="mg-badge mg-badge-warning">UNIQUE</span>' : '') + '</td>' + |
| 726 | + '<td>' + d.duplicate_columns.map(function(c) { return '<code>' + escHtml(c) + '</code>'; }).join(', ') + '</td>' + |
| 727 | + '<td><code>' + escHtml(d.covered_by_index) + '</code></td>' + |
| 728 | + '<td>' + d.covered_by_columns.map(function(c) { return '<code>' + escHtml(c) + '</code>'; }).join(', ') + '</td>' + |
| 729 | + '<td><code style="font-size:11px;user-select:all;cursor:pointer;" title="Click to select">' + escHtml(dropSql) + '</code></td>' + |
| 730 | + '</tr>'; |
| 731 | + }).join(''); |
| 732 | + show(el('dup-table-wrapper')); |
| 733 | + }, function() { |
| 734 | + hide(el('dup-loading')); |
| 735 | + el('dup-empty').textContent = 'Failed to scan indexes.'; |
| 736 | + show(el('dup-empty')); |
| 737 | + }); |
| 738 | + } |
| 739 | + |
| 740 | + el('dup-refresh').addEventListener('click', loadDuplicateIndexes); |
| 741 | + |
| 742 | + // --- Table Sizes --- |
| 743 | + |
| 744 | + function formatMb(mb) { |
| 745 | + if (mb >= 1024) return (mb / 1024).toFixed(2) + ' GB'; |
| 746 | + if (mb >= 1) return mb.toFixed(2) + ' MB'; |
| 747 | + return (mb * 1024).toFixed(0) + ' KB'; |
| 748 | + } |
| 749 | + |
| 750 | + function sizeBar(pct, color) { |
| 751 | + return '<div style="background:#e9ecef;border-radius:3px;height:8px;width:100%;">' + |
| 752 | + '<div style="background:' + color + ';border-radius:3px;height:8px;width:' + Math.max(pct, 1) + '%;"></div></div>'; |
| 753 | + } |
| 754 | + |
| 755 | + function loadTableSizes() { |
| 756 | + show(el('sizes-loading')); |
| 757 | + hide(el('sizes-table-wrapper')); |
| 758 | + el('sizes-tbody').innerHTML = ''; |
| 759 | + |
| 760 | + ajaxGet(ROUTES.table_sizes, {}, function(data) { |
| 761 | + hide(el('sizes-loading')); |
| 762 | + if (!data || !data.length) { el('sizes-total').textContent = '0 tables'; show(el('sizes-table-wrapper')); return; } |
| 763 | + |
| 764 | + var totalMb = data.reduce(function(sum, t) { return sum + t.total_mb; }, 0); |
| 765 | + el('sizes-total').textContent = data.length + ' tables, ' + formatMb(totalMb) + ' total'; |
| 766 | + var maxMb = data[0].total_mb || 1; |
| 767 | + |
| 768 | + el('sizes-tbody').innerHTML = data.map(function(t) { |
| 769 | + var pct = (t.total_mb / maxMb) * 100; |
| 770 | + var color = t.total_mb >= 100 ? '#dc3545' : t.total_mb >= 10 ? '#ffc107' : '#28a745'; |
| 771 | + var rows = t.rows != null ? Number(t.rows).toLocaleString() : '?'; |
| 772 | + return '<tr>' + |
| 773 | + '<td><strong>' + escHtml(t.table) + '</strong></td>' + |
| 774 | + '<td style="text-align:right">' + rows + '</td>' + |
| 775 | + '<td style="text-align:right">' + formatMb(t.data_mb) + '</td>' + |
| 776 | + '<td style="text-align:right">' + formatMb(t.index_mb) + '</td>' + |
| 777 | + '<td style="text-align:right"><strong>' + formatMb(t.total_mb) + '</strong></td>' + |
| 778 | + '<td style="text-align:right">' + (t.fragmented_mb > 0 ? formatMb(t.fragmented_mb) : '-') + '</td>' + |
| 779 | + '<td>' + sizeBar(pct, color) + '</td>' + |
| 780 | + '</tr>'; |
| 781 | + }).join(''); |
| 782 | + show(el('sizes-table-wrapper')); |
| 783 | + }, function() { |
| 784 | + hide(el('sizes-loading')); |
| 785 | + el('sizes-total').textContent = 'Failed to load'; |
| 786 | + }); |
| 787 | + } |
| 788 | + |
| 789 | + el('sizes-refresh').addEventListener('click', loadTableSizes); |
| 790 | + |
654 | 791 | // --- SQL to Builder sync --- |
655 | 792 |
|
656 | 793 | function parseSqlToBuilder(sql) { |
|
0 commit comments