Skip to content

Commit 3942671

Browse files
authored
Merge pull request #105 from macbre/not_used_tables-add-context
not_used_tables: report rows count, table size and its schema
2 parents ddc2b93 + 43eb2a2 commit 3942671

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,19 @@ not_used_indices → table affected: 0002_not_used_indices
161161
- not_used_index: KEY test_id_idx (test, id)
162162
163163
------------------------------------------------------------
164-
not_used_tables → table affected: 0000_the_table
164+
not_used_tables → table affected: 0020_big_table
165165
166-
✗ "0000_the_table" table was not used by provided queries
166+
✗ "0020_big_table" table was not used by provided queries
167+
168+
- schema: CREATE TABLE `0020_big_table` (
169+
`id` int(9) NOT NULL AUTO_INCREMENT,
170+
`val` int(9) NOT NULL,
171+
`text` char(5) NOT NULL,
172+
PRIMARY KEY (`id`),
173+
KEY `text_idx` (`text`)
174+
) ENGINE=InnoDB AUTO_INCREMENT=100001 DEFAULT CHARSET=utf8
175+
- table_size_mb: 5.03125
176+
- rows_estimated: 100405
167177
168178
------------------------------------------------------------
169179
insert_ignore → table affected: 0070_insert_ignore

indexdigest/linters/linter_0006_not_used_columns_and_tables.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44
import logging
55

6-
from collections import defaultdict
6+
from collections import defaultdict, OrderedDict
77
from sql_metadata import get_query_columns, get_query_tables
88

99
from indexdigest.database import IndexDigestQueryError
@@ -63,8 +63,17 @@ def check_not_used_tables(database, queries):
6363

6464
# generate reports
6565
for table in not_used_tables:
66+
metadata = database.get_table_metadata(table)
67+
context = OrderedDict()
68+
69+
context['schema'] = database.get_table_schema(table)
70+
context['table_size_mb'] = \
71+
1. * (metadata['data_size'] + metadata['index_size']) / 1024 / 1024
72+
context['rows_estimated'] = database.get_table_rows_estimate(table)
73+
6674
yield LinterEntry(linter_type='not_used_tables', table_name=table,
67-
message='"{}" table was not used by provided queries'.format(table))
75+
message='"{}" table was not used by provided queries'.format(table),
76+
context=context)
6877

6978

7079
def check_not_used_columns(database, queries):

indexdigest/test/linters/test_0006_not_used_columns_and_tables.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ def test_not_used_tables(self):
3232
self.assertEqual(str(reports[0]), '0006_not_used_tables: "0006_not_used_tables" table was not used by provided queries')
3333
self.assertEqual(reports[0].table_name, '0006_not_used_tables')
3434

35+
assert str(reports[0].context['schema']).startswith('CREATE TABLE `0006_not_used_tables` (\n')
36+
37+
# these are estimates, can't assert a certain value
38+
assert reports[0].context['table_size_mb'] > 0.0001
39+
assert reports[0].context['rows_estimated'] > 0
40+
3541
def test_get_used_tables_from_queries(self):
3642
queries = [
3743
'SELECT /* a comment */ foo FROM `0006_not_used_columns` WHERE id = 1;',

sql/0006-not-used-columns-and-tables.sql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,7 @@ CREATE TABLE `0006_not_used_tables` (
2222
PRIMARY KEY (`id`)
2323
);
2424

25-
INSERT INTO 0006_not_used_tables VALUES(1, 'foo');
25+
INSERT INTO 0006_not_used_tables VALUES
26+
(1, 'foo'),
27+
(2, 'foo'),
28+
(3, 'foo');

0 commit comments

Comments
 (0)