MySQL FLUSH Statement

The FLUSH statement in MySQL is used to clear the caches.
Following is the syntax of MySQL FLUSH statement −
FLUSH [NO_WRITE_TO_BINLOG | LOCAL] {
   BINARY LOGS | ENGINE LOGS | ERROR LOGS | GENERAL LOGS | HOSTS | LOGS | 
   PRIVILEGES | OPTIMIZER_COSTS | RELAY LOGS | SLOW LOGS | STATUS | USER_RESOURCES
}

Following are various (options) FLUSH statements −
mysql> FLUSH BINARY LOGS;
Query OK, 0 rows affected (0.81 sec)

mysql> FLUSH ENGINE LOGS;
Query OK, 0 rows affected (0.34 sec)

mysql> FLUSH ERROR LOGS;
Query OK, 0 rows affected (0.12 sec)

mysql> FLUSH GENERAL LOGS;
Query OK, 0 rows affected (0.07 sec)

mysql> FLUSH HOSTS;
Query OK, 0 rows affected (0.07 sec)

mysql> FLUSH LOGS;
Query OK, 0 rows affected (1.27 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.13 sec)

mysql> FLUSH OPTIMIZER_COSTS;
Query OK, 0 rows affected (0.29 sec)

mysql> FLUSH RELAY LOGS;
Query OK, 0 rows affected (0.09 sec)

mysql> FLUSH STATUS;
Query OK, 0 rows affected (0.12 sec)

mysql> FLUSH USER_RESOURCES;
Query OK, 0 rows affected (0.78 sec)
Using the FLUSH tables statement you can flush the data in the table or acquire locks.
Following is the syntax of the FLUSH TABLES statement −
FLUSH TABLES {
   tbl_name [, tbl_name] ...
   | WITH READ LOCK
   | tbl_name [, tbl_name] ... WITH READ LOCK
   | tbl_name [, tbl_name] ... FOR EXPORT
}

Suppose we have created a table that contains the sales details along with the contact details of the customers as shown below −
mysql> CREATE TABLE SALES_DETAILS (
   ID INT,
   ProductName VARCHAR(255),
   CustomerName VARCHAR(255),
   DispatchDate date,
   DeliveryTime time,
   Price INT,
   Location VARCHAR(255),
   CustomerAge INT,
   CustomrtPhone BIGINT,
   DispatchAddress VARCHAR(255),
   Email VARCHAR(50)
);
Now, let’s insert 2 records into the above created table using the INSERT statement as −
mysql> insert into SALES_DETAILS values(1, 'Key-Board', 'Raja', DATE('2019-09-01'), TIME('11:00:00'), 7000, 'Hyderabad', 25, '9000012345', 'Hyderabad – Madhapur', 'pujasharma@gmail.com');
Query OK, 1 row affected (0.84 sec)

mysql> insert into SALES_DETAILS values(2, 'Mobile', 'Vanaja', DATE('2019-03-01'), TIME('10:10:52'), 9000, 'Chennai', 30, '90000123654', 'Chennai- TNagar', 'vanajarani@gmail.com');
Query OK, 1 row affected (1.84 sec)
If we want another table with just the contact details of the customer create a table as −
mysql> CREATE TABLE CustContactDetails (
   ID INT,
   Name VARCHAR(255),
   Age INT,
   Phone BIGINT,
   Address VARCHAR(255),
   Email VARCHAR(50)
);
Following query insets records into the CustContactDetails table using the INSERT INTO SELECT statement. Here, we are trying to insert records from the SALES_DETAILS table to CustContactDetails table −
mysql> INSERT INTO CustContactDetails (ID, Name, Age, Phone, Address, Email)
   SELECT
      ID, CustomerName, CustomerAge, CustomrtPhone, DispatchAddress, Email
   FROM
      SALES_DETAILS
   WHERE
      ID = 1 AND CustomerName = 'Raja';
Query OK, 1 row affected (0.16 sec)
Records: 1 Duplicates: 0 Warnings: 0

INSERT INTO CustContactDetails (ID, Name, Age, Phone, Address, Email)
   SELECT
      ID, CustomerName, CustomerAge, CustomrtPhone, DispatchAddress, Email
   FROM
      SALES_DETAILS
   WHERE
      ID = 2 AND CustomerName = 'Vanaja';
Query OK, 1 row affected (0.16 sec)
You can verify the contents of the CustContactDetails table as shown below −
mysql> SELECT * FROM CustContactDetails;
+------+--------+------+-------------+----------------------+----------------------+
| ID   | Name   | Age  | Phone       | Address              | Email                |
+------+--------+------+-------------+----------------------+----------------------+
| 1    | Raja   | 25   | 9000012345  | Hyderabad – Madhapur | pujasharma@gmail.com |
| 2    | Vanaja | 30   | 90000123654 | Chennai- TNagar      | vanajarani@gmail.com |
+------+--------+------+-------------+----------------------+----------------------+
2 rows in set (0.00 sec)
Following set of queries locks the instance flushes the above created tables and unlocks the instance.
mysql> LOCK INSTANCE FOR BACKUP;
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH TABLES emp, custcontactdetails WITH READ LOCK;
Query OK, 0 rows affected (0.05 sec)

mysql> UNLOCK TABLES;
Query OK, 0 rows affected (0.00 sec)

mysql> UNLOCK INSTANCE;
Query OK, 0 rows affected (0.00 sec)

    31 Lectures 
     6 hours 

 Eduonix Learning Solutions

    84 Lectures 
     5.5 hours 

 Frahaan Hussain

    6 Lectures 
     3.5 hours 

 DATAhill Solutions Srinivas Reddy

    60 Lectures 
     10 hours 

 Vijay Kumar Parvatha Reddy

    10 Lectures 
     1 hours 

 Harshit Srivastava

    25 Lectures 
     4 hours 

 Trevoir Williams
 Print
 Add Notes
 Bookmark this page