Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/tools/mysql/mysqllisttables/mysqllisttables.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ const listTablesStatement = `
CROSS JOIN (SELECT @table_names := ?, @output_format := ?) AS variables
WHERE
T.TABLE_SCHEMA NOT IN ('mysql', 'information_schema', 'performance_schema', 'sys')
AND (NULLIF(TRIM(@table_names), '') IS NULL OR FIND_IN_SET(T.TABLE_NAME, @table_names))
AND (NULLIF(TRIM(@table_names), '') IS NULL OR FIND_IN_SET(T.TABLE_NAME, TRIM(REGEXP_REPLACE(@table_names, '[[:space:]]*,[[:space:]]*', ','))))
Comment on lines 176 to +179

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Instead of evaluating REGEXP_REPLACE and TRIM on @table_names for every row in INFORMATION_SCHEMA.TABLES (which can be slow and inefficient), you can perform this normalization once during the variable assignment in the CROSS JOIN clause. This simplifies the WHERE clause and improves query performance. Ensure that the regex and delimiter strings are defined as constants if they are reused elsewhere in the package.

Suggested change
CROSS JOIN (SELECT @table_names := ?, @output_format := ?) AS variables
WHERE
T.TABLE_SCHEMA NOT IN ('mysql', 'information_schema', 'performance_schema', 'sys')
AND (NULLIF(TRIM(@table_names), '') IS NULL OR FIND_IN_SET(T.TABLE_NAME, @table_names))
AND (NULLIF(TRIM(@table_names), '') IS NULL OR FIND_IN_SET(T.TABLE_NAME, TRIM(REGEXP_REPLACE(@table_names, '[[:space:]]*,[[:space:]]*', ','))))
CROSS JOIN (SELECT @table_names := TRIM(REGEXP_REPLACE(?, '[[:space:]]*,[[:space:]]*', ',')), @output_format := ?) AS variables
WHERE
T.TABLE_SCHEMA NOT IN ('mysql', 'information_schema', 'performance_schema', 'sys')
AND (NULLIF(@table_names, '') IS NULL OR FIND_IN_SET(T.TABLE_NAME, @table_names))
References
  1. Use hardcoded string literals for values used only once. Centralize values into constants if they are used across multiple locations to maintain consistency and avoid typos.

AND T.TABLE_TYPE = 'BASE TABLE'
ORDER BY
T.TABLE_SCHEMA, T.TABLE_NAME;
Expand Down
6 changes: 6 additions & 0 deletions tests/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -3083,6 +3083,12 @@ func RunMySQLListTablesTest(t *testing.T, databaseName, tableNameParam, tableNam
wantStatusCode: http.StatusOK,
want: []objectDetails{authTableWant, paramTableWant},
},
{
name: "invoke list_tables with multiple table names and surrounding whitespace",
requestBody: bytes.NewBufferString(fmt.Sprintf(`{"table_names": " %s , %s "}`, tableNameParam, tableNameAuth)),
wantStatusCode: http.StatusOK,
want: []objectDetails{authTableWant, paramTableWant},
},
{
name: "invoke list_tables with one existing and one non-existent table",
requestBody: bytes.NewBufferString(fmt.Sprintf(`{"table_names": "%s,non_existent_table"}`, tableNameAuth)),
Expand Down
Loading