Skip to content

Commit b6c4091

Browse files
authored
Merge pull request #213 from macbre/issue-210-queries_not_using_index
Improve queries_not_using_index linter
2 parents 81a7448 + 3fa9f7d commit b6c4091

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-5
lines changed

indexdigest/linters/linter_0019_queries_not_using_indices.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ def check_queries_not_using_indices(database, queries):
1616
# print(query, explain_row)
1717

1818
# EXPLAIN can return no matching row in const table in Extra column.
19-
# Do not consider this query as not using an index. -- see #44
19+
# Do not consider this query as not using an index. -- see #44 and #210
2020
if explain_row['Extra'] in [
2121
'Impossible WHERE noticed after reading const tables',
2222
'no matching row in const table',
23-
'No tables used'
23+
'No tables used',
24+
'Select tables optimized away',
25+
'No matching min/max row',
2426
]:
2527
continue
2628

indexdigest/test/linters/test_0019_queries_not_using_indices.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ def test_queries(self):
1212
reports = list(check_queries_not_using_indices(
1313
database=self.connection, queries=read_queries_from_log('0019-queries-not-using-indices-log')))
1414

15-
print(list(map(str, reports)))
16-
17-
self.assertEqual(len(reports), 3)
15+
print(*[f"{report.message} ({report.context['explain_extra']})" for report in reports], sep="\n")
16+
assert len(reports) == 3
1817

1918
self.assertEqual(str(reports[0]), '0019_queries_not_using_indices: "SELECT item_id FROM 0019_queries_not_using_indices..." query did not make use of any index')
2019
self.assertEqual(reports[0].table_name, '0019_queries_not_using_indices')

sql/0019-queries-not-using-indices-log

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ SELECT foo FROM 0019_queries_not_using_indices WHERE item_id = 5;
1111
SELECT 1*1;
1212
SELECT 1 AS one FROM dual WHERE exists ( SELECT 1 FROM 0000_the_table WHERE item_id = 2 );
1313
SELECT 1 AS one FROM dual WHERE exists ( SELECT item_id FROM 0019_queries_not_using_indices WHERE foo = "test" );
14+
-- #210: EXPLAINS' Extra says "Select tables optimized away"
15+
SELECT max(item_id) FROM 0019_queries_not_using_indices;
16+
-- #210: EXPLAINS' Extra says "No matching min/max row"
17+
SELECT max(item_id) FROM 0019_queries_not_using_indices_empty_table;

sql/0019-queries-not-using-indices.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ CREATE TABLE `0019_queries_not_using_indices` (
1010
KEY `bar_idx` (`bar`)
1111
);
1212

13+
-- https://github.com/macbre/index-digest/issues/210
14+
DROP TABLE IF EXISTS `0019_queries_not_using_indices_empty_table`;
15+
CREATE TABLE `0019_queries_not_using_indices_empty_table` (
16+
`item_id` int(9) NOT NULL AUTO_INCREMENT,
17+
`foo` varchar(16) NOT NULL DEFAULT '',
18+
PRIMARY KEY (`item_id`)
19+
);
20+
1321
INSERT INTO 0019_queries_not_using_indices VALUES
1422
(1, 'test', ''),
1523
(2, 'foo', 'test'),

0 commit comments

Comments
 (0)