From 853e613301a86ed70402a665e9d0ce662bf97a6d Mon Sep 17 00:00:00 2001 From: Yaning Zhu Date: Mon, 23 Jan 2017 09:51:40 -0800 Subject: [PATCH] Faster retrieval of MySQL schema Because `information_schema.tables` and `information_schema.columns` do not have indexes on them, when they get large, joining them becomes painful. We have a database with over 10k tables and more than 200k columns. The old query takes forever. The new query do not look at `information_schema.tables` at all. This works if your `information_schema.columns` table is in good shape in terms of data integrity (and it should be). Tested on MySQL 5.5, 5.6(Percona), 5.7(Percona). The schema has not changed since then. --- query_runner/mysql.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/query_runner/mysql.py b/query_runner/mysql.py index 9b644d43db..8a7cf2f203 100644 --- a/query_runner/mysql.py +++ b/query_runner/mysql.py @@ -93,12 +93,7 @@ def _get_tables(self, schema): col.table_name, col.column_name FROM `information_schema`.`columns` col - INNER JOIN - (SELECT table_schema, - TABLE_NAME - FROM information_schema.tables - WHERE table_type <> 'SYSTEM VIEW' AND table_schema NOT IN ('performance_schema', 'mysql')) tables ON tables.table_schema = col.table_schema - AND tables.TABLE_NAME = col.TABLE_NAME; + WHERE col.table_schema NOT IN ('information_schema', 'performance_schema', 'mysql'); """ results, error = self.run_query(query, None)