Tuesday 3 December 2019

MySQL DESCRIBE

MySQL DESCRIBE command provides information about how a particular table is constructed or how a particular query is executed. EXPLAIN and DESC are synonyms of DESCRIBE in MySQL and these three commands work the same way in all aspects.
In this article, we will look at examples to learn the usage of MySQL DESCRIBE to explain table structures and query executions.

MySQL DESCRIBE table – Syntax

Given below is the syntax to explain table structures using MySQL DESCRIBE command:
  1. EXPLAIN | DESCRIBE | DESC
  2. table_name
  3. [column_name | wild]
MySQL Describe table – Syntax
Here, column_name is optional and if supplied DESCRIBE command will provide information only about that particular column of the table. Instead of mentioning exact column_name, we can also mention strings with ‘%’ to match the column_name through a wild search.

MySQL DESCRIBE query – Syntax

The syntax to explain query execution using MySQL DESCRIBE command is shown below:
  1. EXPLAIN | DESCRIBE | DESC
  2. [FORMAT = type]
  3. statement
MySQL Describe query – Syntax
The SQL statement, whose execution plan is required has to be mentioned in place of “statement”. The “FORMAT” part is optional and it can be used to modify the format of output fetched. “TRADITIONAL”, “JSON” and “TREE” are three available options, which can be used here in place of “type”.

Example

Let’s say we have two tables: “Organisation” and “OrganisationBranchMap” with the following structure.
  1. CREATE TABLE Organisation
  2. (
  3. ID INT PRIMARY KEY,
  4. Name VARCHAR(100)
  5. );
  6. CREATE TABLE OrganisationBranchMap
  7. (
  8. ID INT PRIMARY KEY,
  9. Name VARCHAR(100),
  10. Address VARCHAR(300),
  11. Organisation_ID INT,
  12. FOREIGN KEY (Organisation_ID) REFERENCES Organisation(ID)
  13. );
  14. INSERT INTO Organisation(ID,Name) VALUES
  15. (1,'AAA'),
  16. (2,'BBB'),
  17. (3,'CCC');
  18. INSERT INTO OrganisationBranchMap(ID,Name, Address, Organisation_ID) VALUES
  19. (1,'123','70 Well Lane, PARTON',1),
  20. (2,'456','96 Newmarket Road, HAWTHORN HILL', 1),
  21. (3,'689','81 Cunnery Rd, MALLWD',2),
  22. (4,'246','87 Shannon Way, CHICKLADE',3);
Now, we query the structure of “OrganisationBranchMap” table using MySQL DESCRIBE command.
  1. DESCRIBE
  2. OrganisationBranchMap
MySQL Describe “OrganisationBranchMap”
Result:
“OrganisationBranchMap” table structure – all columns
We can also query only for columns with its name including ‘ID’ in it by executing the following query:
  1. DESCRIBE
  2. OrganisationBranchMap
  3. ‘%ID%’
MySQL Describe “OrganisationBranchMap” column_names with ‘%ID%’
Result:
“OrganisationBranchMap” table structure – column_names with ‘%ID%’
As the next step, we will query for execution plan of a SELECT statement in JSON format as given below:
  1. EXPLAIN
  2. FORMAT= JSON
  3. SELECT
  4. ob.ID,ob.Name,ob.Address,o.Name
  5. FROM
  6. OrganisationBranchMap ob
  7. INNER JOIN
  8. Organisation o
  9. ON
  10. ob.Organisation_ID = o.ID;
MySQL Describe “SELECT” statement
Result:
{\n "query_block": {\n "select_id": 1,\n "cost_info": {\n "query_cost": "7.07"\n },\n "nested_loop": [\n {\n "table": {\n "table_name": "o",\n "access_type": "ALL",\n "possible_keys": [\n "PRIMARY"\n ],\n "rows_examined_per_scan": 3,\n "rows_produced_per_join": 3,\n "filtered": "100.00",\n "cost_info": {\n "read_cost": "2.01",\n "eval_cost": "0.60",\n "prefix_cost": "2.61",\n "data_read_per_join": "1K"\n },\n "used_columns": [\n "ID",\n "Name"\n ]\n }\n },\n {\n "table": {\n "table_name": "ob",\n "access_type": "ALL",\n "possible_keys": [\n "Organisation_ID"\n ],\n "rows_examined_per_scan": 4,\n "rows_produced_per_join": 3,\n "filtered": "25.00",\n "using_join_buffer": "Block Nested Loop",\n "cost_info": {\n "read_cost": "2.05",\n "eval_cost": "0.60",\n "prefix_cost": "7.07",\n "data_read_per_join": "4K"\n },\n "used_columns": [\n "ID",\n "Name",\n "Address",\n "Organisation_ID"\n ],\n "attached_condition": "(`test`.`ob`.`Organisation_ID` = `test`.`o`.`ID`)"\n }\n }\n ]\n }\n}
MySQL Describe “SELECT” statement output in JSON format
From this, we will be able to understand the steps involved in the execution of the “SELECT” statement and the query cost related information at each step.

0 comments:

Post a Comment