How to Get the Size of a Table in MySQL using Python? Last Updated : 26 Dec, 2020 Comments Improve Suggest changes Like Article Like Report Prerequisite: Python: MySQL Create Table In this article, we are going to see how to get the size of a table in MySQL using Python. Python allows the integration of a wide range of database servers with applications. A database interface is required to access a database from Python. MySQL Connector-Python module is an API in python for communicating with a MySQL database. Approach: Import module.Make a connection request with the database.Create an object for the database cursor.Execute the following MySQL query: SELECT table_name AS `Table`, round(((data_length + index_length) / 1024 / 1024), 2) `Size in MB` FROM information_schema.TABLES WHERE table_schema = 'DataBase_name' AND table_name = 'Table_name'; Example 1: In this example we are using this database table with the following query; Below is the implementation: Python3 # Import required module import mysql.connector # Establish connection # to MySQL database mydb = mysql.connector.connect( host="localhost", user="root", password="root123", database="geeks") # Create cursor object mycursor = mydb.cursor() # Execute query query = "SELECT table_name AS `Table`, \ round(((data_length + index_length) \ / 1024 / 1024), 2) `Size in MB` \ FROM information_schema.TABLES \ WHERE table_schema = 'Geeks' AND \ table_name = 'Persons';" mycursor.execute(query) # Display size of each table myresult = mycursor.fetchall() for item in myresult: print(item[0], "Size in MB: ", item[-1]) Output: Example 2: In this example, we are going to get all table sizes in a database. Below is the implementation: Python3 # Import required module import mysql.connector # Establish connection # to MySQL database mydb = mysql.connector.connect( host="localhost", user="root", password="root123", database="geeks") # Create cursor object mycursor = mydb.cursor() # Execute query query = "SELECT TABLE_NAME AS `Table`, \ ROUND(((DATA_LENGTH + INDEX_LENGTH) \ / 1024 / 1024),2) AS `Size (MB)` \ FROM information_schema.TABLES WHERE \ TABLE_SCHEMA = 'Geeks' ORDER BY \ (DATA_LENGTH + INDEX_LENGTH) DESC;" mycursor.execute(query) # Display size of each table myresult = mycursor.fetchall() for item in myresult: print(item[0], "Size in MB: ", item[-1]) Output: Comment More infoAdvertise with us Next Article How to Get the Size of a Table in MySQL using Python? K kumar_satyam Follow Improve Article Tags : Python Python-mySQL Practice Tags : python Similar Reads How to Copy a Table in MySQL Using Python? In this article, we will create a table in MySQL and will create a copy of that table using Python. We will copy the entire table, including all the columns and the definition of the columns, as well as all rows of data in the table. To connect to MySQL database using python, we need PyMySql module. 3 min read How to Show All Tables in MySQL using Python? A connector is employed when we have to use mysql with other programming languages. The work of mysql-connector is to provide access to MySQL Driver to the required language. Thus, it generates a connection between the programming language and the MySQL Server. In order to make python interact with 1 min read How to Count the Number of Rows in a MySQL Table in Python? MySQL server is an open-source relational database management system which is a major support for web-based applications. Databases and related tables are the main component of many websites and applications as the data is stored and exchanged over the web. In order to access MySQL databases from a 2 min read How to Copy a Table Definition in MySQL Using Python? Python requires an interface to access a database server. Python supports a wide range of interfaces to interact with various databases. To communicate with a MySQL database, MySQL Connector Python module, an API written purely in Python, is used. This module is self-sufficient meaning that it does 6 min read How to Count the Number of Rows of a Given SQLite Table using Python? In this article, we will discuss how we can count the number of rows of a given SQLite Table using Python. We will be using the cursor_obj.fetchall() method to do the same. This method fetches all the rows of a query result. It returns all the rows as a list of tuples. An empty list is returned if t 2 min read Like