Skip to content

Commit 29d756d

Browse files
authored
[DBMON-5463] Switch sql index query resolution over to a static version check (#20514)
* Switch over to a static version check * Add changelog
1 parent fc05b9c commit 29d756d

File tree

3 files changed

+16
-19
lines changed

3 files changed

+16
-19
lines changed

mysql/changelog.d/20514.fixed

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Avoid querying INFORMATION_SCHEMA.COLUMNS in favor of a static version check

mysql/datadog_checks/mysql/databases_data.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,9 @@
2020
SQL_COLUMNS,
2121
SQL_DATABASES,
2222
SQL_FOREIGN_KEYS,
23-
SQL_INDEXES,
24-
SQL_INDEXES_8_0_13,
25-
SQL_INDEXES_EXPRESSION_COLUMN_CHECK,
2623
SQL_PARTITION,
2724
SQL_TABLES,
25+
get_indexes_query,
2826
)
2927

3028
from .util import get_list_chunks
@@ -169,8 +167,7 @@ def _cursor_run(self, cursor, query, params=None):
169167
@tracked_method(agent_check_getter=agent_check_getter)
170168
def _fetch_database_data(self, cursor, start_time, db_name):
171169
tables = self._get_tables(db_name, cursor)
172-
tables_chunks = list(get_list_chunks(tables, self.TABLES_CHUNK_SIZE))
173-
for tables_chunk in tables_chunks:
170+
for tables_chunk in get_list_chunks(tables, self.TABLES_CHUNK_SIZE):
174171
schema_collection_elapsed_time = time.time() - start_time
175172
if schema_collection_elapsed_time > self._max_execution_time:
176173
self._data_submitter.submit()
@@ -357,12 +354,7 @@ def _populate_with_columns_data(self, table_name_to_table_index, table_list, tab
357354

358355
@tracked_method(agent_check_getter=agent_check_getter)
359356
def _populate_with_index_data(self, table_name_to_table_index, table_list, table_names, db_name, cursor):
360-
self._cursor_run(cursor, query=SQL_INDEXES_EXPRESSION_COLUMN_CHECK)
361-
query = (
362-
SQL_INDEXES_8_0_13.format(table_names)
363-
if cursor.fetchone()["column_count"] > 0
364-
else SQL_INDEXES.format(table_names)
365-
)
357+
query = get_indexes_query(self._check.version, self._check.is_mariadb, table_names)
366358
self._cursor_run(cursor, query=query, params=db_name)
367359
rows = cursor.fetchall()
368360
if not rows:

mysql/datadog_checks/mysql/queries.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,6 @@
120120
WHERE table_schema = %s AND table_name IN ({});
121121
"""
122122

123-
SQL_INDEXES_EXPRESSION_COLUMN_CHECK = """
124-
SELECT COUNT(*) as column_count
125-
FROM INFORMATION_SCHEMA.COLUMNS
126-
WHERE TABLE_SCHEMA = 'information_schema'
127-
AND TABLE_NAME = 'STATISTICS'
128-
AND COLUMN_NAME = 'EXPRESSION';
129-
"""
130-
131123
SQL_INDEXES = """\
132124
SELECT
133125
table_name as `table_name`,
@@ -268,3 +260,15 @@ def show_primary_replication_status_query(version, is_mariadb):
268260
return "SHOW BINARY LOG STATUS;"
269261
else:
270262
return "SHOW MASTER STATUS;"
263+
264+
265+
def get_indexes_query(version, is_mariadb, table_names):
266+
"""
267+
Get the appropriate indexes query based on MySQL version and flavor.
268+
The EXPRESSION column was introduced in MySQL 8.0.13 for functional indexes.
269+
MariaDB doesn't support functional indexes.
270+
"""
271+
if not is_mariadb and version.version_compatible((8, 0, 13)):
272+
return SQL_INDEXES_8_0_13.format(table_names)
273+
else:
274+
return SQL_INDEXES.format(table_names)

0 commit comments

Comments
 (0)