I need to get list of all tables on server in all databases.
I found out 2 ways for doing that.
1). Execute SHOW FULL TABLES from <each database name> WHERE table_type = 'BASE TABLE';
2). Execute SELECT table_name, table_schema FROM information_schema.tables WHERE TABLE_TYPE = "BASE TABLE";
Questions:
1). Is there any other method then mentioned above that can perform better?
2). Is there any performance difference in executing above two methods?
3). Which of the above two methods is better to execute?
Chapter 26 INFORMATION_SCHEMA Tables INFORMATION_SCHEMA provides access to database metadata, information about the MySQL server such as the name of a database or table, the data type of a column, or access privileges. Other terms that are sometimes used for this information are data dictionary and system catalog.
The INFORMATION_SCHEMA database is an ANSI standard set of views we can find in SQL Server, but also MySQL. Other database systems also have either exactly such or similar database implemented. It provides the read-only access to details related to databases and their objects (tables, constraints, procedures, views…)
SHOW TABLES lists the non- TEMPORARY tables in a given database. You can also get this list using the mysqlshow db_name command. The LIKE clause, if present, indicates which table names to match.
Of course information_schema
.tables
is more preferable than SHOW TABLES statement (which was used in old MySQL versions < 5.0). It gives more useful information, it is standard system schema (you can find similar schema in other databases, e.g. SQL Server). You can use standard SELECT statement to retrieve information from this schema, I mean you can use WHERE, GROUP BY, ORDER BY and other clauses and functions. But sometimes, on big databases, the performance of information_schema
may be bad.
Have a look at the article about the information_schema
performance: INFORMATION_SCHEMA Optimization.
Where possible, I would use information_schema
. It is an ANSI standard, however, access to proprietary features of MySql may require use of the SHOW* functions on occasion.
So I guess it depends on your particular situation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With