Index and explain

Directory index is like the book. By indexing can quickly locate a piece of data.

In MySQL addition to B + tree index, there are some other index types. For example: full-text index, (DB and DD index called R-tree index). P is a MySQL cluster-tree index, the engine is used in the memory hash index. Bitmap indexes in Oracle in MySQL is not.
Ninety-five percent of the time in dealing with the B + tree index. Up B + tree index is used.

Pointer points to the next layer is called the fan-out (fanout)

B + tree index is not necessarily in the physical order, for example: the insert 28, it will be possible at the back 30.
Logically ordered, it is ordered by the pointer for the logical, in-page data is ordered, between pages are ordered.

 

index from the Orders Show \ G
********************** 1.row ******************* *****
the Table: Orders
NON_UNIQUE: 0   - indicates unique
Key_name: PRIMARY - key name is the Primary
Sql_in_index:. 1
Column_name: o_orderkey
the Collation: a
the cardinality: 1417233    - base, the unique values of the column Numerical
Sbu_part: NULL
Packed: NULL
Null:
index_type: BTREE    - index type is BTree
the Comment:
Index_comment:
********************** 2.row *** *********************
the Table: the Orders
NON_UNIQUE: 1    - represents not only
the Key_name: i_o_orderdate
Sql_in_index: 1
Column_name: o_orderDATE
Collation: a
The Cardinality: 2047   - base, this value will not be repeated column values
Sbu_part: NULL
Packed: NULL
Null: YES
index_type: BTREE   - index type is the BTree
it Comment:
Index_comment:

Cardinality (base)

  Cardinality indicates how many different recorded on the index column, this is has been estimated value is sampled (triggered by INNODB, randomly sampled 20 page, make estimates, there are parameters you can set how many pages samples), the larger value is better, i.e. when Cardinality / RowNumber closer to 1 as possible. It indicates that the column was highly selective.
- record relevant information Cardinality in information_schema library STATISTICS table

ID card, phone number, name, order number, etc.: high selectivity
low selectivity: gender, age, etc.

mysql> create table test_index_2(a int,b int,c int);
Query OK, 0 rows affected (0.10 sec)

mysql> alter table test_index_2 add index idx_mul_ab(a,b);
Query OK, 0 rows affected (0.50 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> insert into test_index_2 values
  -> (1,1,10),
  -> (1,2,9),
  -> (2,1,8),
  -> (2,4,15),
  -> (3,1,6),
  -> (3,2,17);
Query OK, 6 rows affected (0.00 sec)
Records: 6 Duplicates: 0 Warnings: 0

mysql> explain select * from test_index_2 where a=1 and b=2\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: test_index_2
partitions: NULL
type: ref    --此时走了索引
possible_keys: idx_mul_ab
key: idx_mul_ab
key_len: 10
ref: const,const
rows: 1
filtered: 100.00
Extra: NULL
1 row in set, 1 warning (0.00 sec)

mysql> explain select * from test_index_2 where b=2\G    -- 只查询b
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: test_index_2
partitions: NULL
type: ALL   -- 没有使用索引
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 6
filtered: 16.67
Extra: Using where
1 row in set, 1 warning (0.00 sec)


mysql> explain select * from test_index_2 where a = 1 or b = 2 \ G - used or, result set is required and set
********************** Row *************************** 1. *****
ID:. 1
SELECT_TYPE: the SIMPLE
Table: test_index_2
Partitions: NULL
type: ALL - do not use the index, because there is no index b, so b is to take full table scan, now go full table scan, a value can also be filtered with no need to go once a check of the index.
possible_keys: idx_mul_ab
Key: NULL
key_len: NULL
ref: NULL
rows: 6
Filtered: 30.56
Extra: the Using the WHERE
1 Row in the SET, 1 warning (0.00 sec)

---- particular example
---- or just use the b column do range queries, take the index found a
---- Note that the query is COUNT (*)
MySQL> EXPLAIN the SELECT COUNT (*) from the WHERE b test_index_2 >. 1 and B <. 3 \ G
*************************** 1. Row ************ ***************
the above mentioned id: 1
SELECT_TYPE: SIMPLE
the Table: test_index_2
Partitions: NULL
of the type: index ---- gone index
possible_keys: NULL
Key: idx_mul_ab
key_len: 10
ref: NULL
rows :. 6
Filtered: 16.67
Extra: the Using WHERE; ---- the Using index cover index
1 row in set, 1 warning ( 0.01 sec)

---- because the requirement is count (*), and requires that all records.
---- That index that contains a record of all that the scan (a, b) an index can also get count (*) of


MySQL> EXPLAIN SELECT * WHERE from test_index_2 B> B. 1 and <. 3 \ G
*************************** 1. Row *** ************************
the above mentioned id: 1
SELECT_TYPE: SIMPLE
the Table: test_index_2
Partitions: NULL
of the type: * ALL ---- query would not be able to use ( a, b) of the index, the value needs to scan the entire table column b.
possible_keys: NULL
Key: NULL
key_len: NULL
ref: NULL
rows: 6
Filtered: 16.67
Extra: the Using the WHERE
1 Row in the SET, 1 warning (0.00 sec)

MySQL> EXPLAIN SELECT * WHERE from test_index_2 and A = C = 10. 1 \ G
*************************** 1. Row *** ************************
the above mentioned id: 1
SELECT_TYPE: SIMPLE
the Table: test_index_2
Partitions: NULL
of the type: ---- ref is taking the index, first use take a result set index, then c = 10 to filter
the possible_keys: idx_mul_ab
Key: idx_mul_ab
the key_len:. 5
REF: const
rows: 2
filtered: 16.67
Extra: the Using WHERE
. 1 in sET Row, warning. 1 (0.01 sec)


INFORMATION_SCHEMA

---- information_schema database equivalent of a data dictionary. Save the meta-information table.

mysql> select * from key_column_usage limit 3\G   --显示了哪个索引使用了哪个列
*************************** 1. row ***************************
CONSTRAINT_CATALOG: def
CONSTRAINT_SCHEMA: employees
CONSTRAINT_NAME: PRIMARY
TABLE_CATALOG: def
TABLE_SCHEMA: employees
TABLE_NAME: departments   -- 表名
COLUMN_NAME: dept_no   -- 索引的名称
ORDINAL_POSITION: 1
POSITION_IN_UNIQUE_CONSTRAINT: NULL
REFERENCED_TABLE_SCHEMA: NULL
REFERENCED_TABLE_NAME: NULL
REFERENCED_COLUMN_NAME: NULL
*************************** 2. row ***************************
CONSTRAINT_CATALOG: def
CONSTRAINT_SCHEMA: employees
CONSTRAINT_NAME: dept_name
TABLE_CATALOG: def
TABLE_SCHEMA: employees
TABLE_NAME: departments
COLUMN_NAME: dept_name
ORDINAL_POSITION: 1
POSITION_IN_UNIQUE_CONSTRAINT: NULL
REFERENCED_TABLE_SCHEMA: NULL
REFERENCED_TABLE_NAME: NULL
REFERENCED_COLUMN_NAME: NULL
*************************** 3. row ***************************
CONSTRAINT_CATALOG: def
CONSTRAINT_SCHEMA: employees
CONSTRAINT_NAME: PRIMARY
TABLE_CATALOG: def
TABLE_SCHEMA: employees
TABLE_NAME: dept_emp
COLUMN_NAME: emp_no
ORDINAL_POSITION: 1
POSITION_IN_UNIQUE_CONSTRAINT: NULL
REFERENCED_TABLE_SCHEMA: NULL
REFERENCED_TABLE_NAME: NULL
REFERENCED_COLUMN_NAME: NULL
3 rows in set (0.66 sec)


EXPLAIN

explain explain the SQL statement execution plans that show how the SQL statement is executed. Use explain, it can also be used desc.

Version 5.6 supports DML statements explain explain
5.6 version began to support JSON output format

Note: EXPLAIN view of the implementation plan, do SQL parsing, not to really perform; and later to 5.7 subquery not to do it.

Parameters extended

MySQL> Extended EXPLAIN SELECT * WHERE from test_index_2 B> B. 1 and <. 3 \ G
*************************** 1. Row ** *************************
ID:. 1
SELECT_TYPE: the SIMPLE
Table: test_index_2
Partitions: NULL
type: ALL
The possible_keys: NULL
Key: NULL
The key_len: NULL
REF : NULL
rows:. 6
Filtered: 16.67
Extra: the Using wHERE
. 1 in SET Row, 2 Represents warnings (0.00 sec) has warnings, where information corresponding to a return

mysql> show warnings\G
*************************** 1. row ***************************
Level: Warning
Code: 1681
Message: 'EXTENDED' is deprecated and will be removed in a future release. -- 即将被启用
*************************** 2. row *************************** -- 显示真正的执行语句。
Level: Note
Code: 1003
Message: /* select#1 */ select `school`.`test_index_2`.`a` AS `a`,`school`.`test_index_2`.`b` AS `b`,`school`.`test_index_2`.`c` AS `c` from `school`.`test_index_2` where ((`school`.`test_index_2`.`b` > 1) and (`school`.`test_index_2`.`b` < 3))
2 rows in set (0.00 sec)


FORMAT parameters
  using FORMAT = JSON format not only for output, as well as other useful information display.
  And when the version 5.6, using MySQL workbench, can be displayed using visual Explain detailed illustration information.

mysql> explain format=json select * from test_index_2 where b > 1 and b<3\G
*************************** 1. row ***************************
EXPLAIN: {
  "query_block": {
  "select_id": 1,
  "cost_info": {
  "query_cost": "2.20"    -- 总成本
  },
  "table": {
  "table_name": "test_index_2",
  "access_type": "ALL",
  "rows_examined_per_scan": 6,
  "rows_produced_per_join": 1,
  "filtered": "16.67",
  "cost_info": {
  "read_cost": "2.00",
  "eval_cost": "0.20",
  "prefix_cost": "2.20",
  "data_read_per_join": "16"
  },
  "used_columns": [
  "a",
  "b",
  "c"
  ],
  "attached_condition": "((`school`.`test_index_2`.`b` > 1) and (`school`.`test_index_2`.`b` < 3))"
  }
 }
}
1 row in set, 1 warning (0.00 sec)


MySQL5.6 not installed by default library sys, sys libraries can also be installed, but the amount of data table is 88. 5.7 sys library is 101 tables. This is because the 5.7 performance_schema added a few new table, 5.6 is not.
sys library table similar to the performance views in Oracle. His view is based on tables created performance_schema.


mysql> use information_schema

mysql> show create table STATISTICS\G
*************************** 1. row ***************************
Table: STATISTICS
Create Table: CREATE TEMPORARY TABLE `STATISTICS` (
`TABLE_CATALOG` varchar(512) NOT NULL DEFAULT '',
`TABLE_SCHEMA` varchar(64) NOT NULL DEFAULT '', -- 表所在的库
`TABLE_NAME` varchar(64) NOT NULL DEFAULT '', -- 表名
`NON_UNIQUE` bigint(1) NOT NULL DEFAULT '0',
`INDEX_SCHEMA` varchar(64) NOT NULL DEFAULT '',
`INDEX_NAME` varchar(64) NOT NULL DEFAULT '', -- 索引名
`SEQ_IN_INDEX` bigint(2) NOT NULL DEFAULT '0', -- 索引的序号
`COLUMN_NAME` varchar(64) NOT NULL DEFAULT '',
`COLLATION` varchar(1) DEFAULT NULL,
here we find Cardinality`CARDINALITY` bigint (21) DEFAULT NULL
`SUB_PART` bigint(3) DEFAULT NULL,
`PACKED` varchar(10) DEFAULT NULL,
`NULLABLE` varchar(3) NOT NULL DEFAULT '',
`INDEX_TYPE` varchar(16) NOT NULL DEFAULT '',
`COMMENT` varchar(16) DEFAULT NULL,
`INDEX_COMMENT` varchar(1024) NOT NULL DEFAULT ''
) ENGINE=MEMORY DEFAULT CHARSET=utf8
1 row in set (0.00 sec)


---- before we can show index from table_name way to see the index

mysql> show index from salaries\G
*************************** 1. row ***************************
Table: salaries
Non_unique: 0
Key_name: PRIMARY
Seq_in_index: 1    ---- 索引序号为1
Column_name: emp_no
Collation: A
Cardinality: 258047   ---- Cardinality值
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:
Index_comment:
*************************** 2. row ***************************
Table: salaries
Non_unique: 0
Key_name: PRIMARY
Seq_in_index: 2  ---- 索引序号为2
Column_name: from_date
Collation: A
Cardinality: 2494090   ---- Cardinality值
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:
Index_comment:


---- can now view information about a particular table in the library by INFORMATION_SCHEMA STATISTICS Table


mysql> select * from STATISTICS where table_name='salaries'\G
*************************** 1. row ***************************
TABLE_CATALOG: def
TABLE_SCHEMA: employees
TABLE_NAME: salaries
NON_UNIQUE: 0
INDEX_SCHEMA: employees
INDEX_NAME: PRIMARY
SEQ_IN_INDEX: 1    -- 索引序号为1
COLUMN_NAME: emp_no
COLLATION: A
CARDINALITY: 258047   -- Cardinality值
SUB_PART: NULL
PACKED: NULL
NULLABLE:
INDEX_TYPE: BTREE
COMMENT:
INDEX_COMMENT:
*************************** 2. row ***************************
TABLE_CATALOG: def
TABLE_SCHEMA: employees
TABLE_NAME: salaries
NON_UNIQUE: 0
INDEX_SCHEMA: employees
INDEX_NAME: PRIMARY
SEQ_IN_INDEX: 2    -- 索引序号为2
COLUMN_NAME: from_date
COLLATION: A
CARDINALITY: 2494090   -- Cardinality值
SUB_PART: NULL
PACKED: NULL
NULLABLE:
INDEX_TYPE: BTREE
COMMENT:
INDEX_COMMENT:


------ As can be seen, the value obtained Cardinality of the above two methods are equal
------ information_schema.STATISTICS conclusion that this table records information Cardinality.

1.表的信息如table_schema, table_name ,table_rows等在information_schema.TABLES中。
mysql> show create table TABLES\G
*************************** 1. row ***************************
Table: TABLES
Create Table: CREATE TEMPORARY TABLE `TABLES` (
`TABLE_CATALOG` varchar(512) NOT NULL DEFAULT '',
`TABLE_SCHEMA` varchar(64) NOT NULL DEFAULT '', -- 表所在的库
`TABLE_NAME` varchar(64) NOT NULL DEFAULT '', -- 表名
`TABLE_TYPE` varchar(64) NOT NULL DEFAULT '',
`ENGINE` varchar(64) DEFAULT NULL,
`VERSION` bigint(21) unsigned DEFAULT NULL,
`ROW_FORMAT` varchar(10) DEFAULT NULL,
`TABLE_ROWS` bigint(21) unsigned DEFAULT NULL, -- 表的记录数
`AVG_ROW_LENGTH` bigint(21) unsigned DEFAULT NULL,
`DATA_LENGTH` bigint(21) unsigned DEFAULT NULL,
`MAX_DATA_LENGTH` bigint(21) unsigned DEFAULT NULL,
`INDEX_LENGTH` bigint(21) unsigned DEFAULT NULL,
`DATA_FREE` bigint(21) unsigned DEFAULT NULL,
`AUTO_INCREMENT` bigint(21) unsigned DEFAULT NULL,
`CREATE_TIME` datetime DEFAULT NULL,
`UPDATE_TIME` datetime DEFAULT NULL,
`CHECK_TIME` datetime DEFAULT NULL,
`TABLE_COLLATION` varchar(32) DEFAULT NULL,
`CHECKSUM` bigint(21) unsigned DEFAULT NULL,
`CREATE_OPTIONS` varchar(255) DEFAULT NULL,
`TABLE_COMMENT` varchar(2048) NOT NULL DEFAULT ''
) ENGINE=MEMORY DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

Table_schema presence and table_name information 2.information.STATISTICS

3. STATISTICS TABLES and table_schema and table_name table by associating Cardinality and table_rows calculated, to obtain the corresponding index names selectivity

3.1 because there is a composite index, so we have to remove the composite index of the largest such Cardinality value out of that seq is the greatest

*************************** 98. row ******************** *******
table_schema: School
table_name: test_index_2
index_name: idx_mul_ab - this is the last test composite index created index
max (seq_in_index): 2 - remove the biggest seq

3.2 to give the greatest seq, can be taken corresponding cardinality

*************************** 91. row ***************************
table_schema: school
table_name: child
index_name: par_ind
cardinality: 0
*************************** 92. row ***************************
table_schema: school
table_name: customer
index_name: PRIMARY
cardinality: 0
*************************** 93. row ***************************
table_schema: school
table_name: parent
index_name: PRIMARY
cardinality: 1
*************************** 94. row ***************************
table_schema: school
table_name: product
index_name: PRIMARY
cardinality: 0


3.3 Finally table_schema and table_name make the above information and associate TABLES Table

MySQL> SELECT
-> t.TABLE_SCHEMA, t.TABLE_NAME, INDEX_NAME is, the CARDINALITY, TABLE_ROWS,
-> the CARDINALITY / TABLE_ROWS the SELECTIVITY the AS - selectively obtained
-> the FROM
-> TABLES T,
-> (
-> SELECT
-> TABLE_SCHEMA,
- > table_name,
-> index_name,
-> with cardinality
-> from STATISTICS
-> WHERE (TABLE_SCHEMA, table_name, index_name, seq_in_index) the IN (
-> SELECT TABLE_SCHEMA,
-> table_name,
-> index_name,
-> MAX (seq_in_index)
-> from
-> STATISTICS
-> Group by TABLE_SCHEMA, table_name, index_name)
->) S - table II query, the query result is above 3.2
-> where t.table_schema = s.table_schema - linked through the library
-> and t.table_name = s.table_name - by table variables
-> and t.table_schema = 'employees' - Specifies the name of a library
-> order by SELECTIVITY;

+--------------+--------------+------------+-------------+------------+-------------+
| TABLE_SCHEMA | TABLE_NAME | index_name | cardinality | TABLE_ROWS | SELECTIVITY |
+--------------+--------------+------------+-------------+------------+-------------+
| employees | dept_emp | dept_no | 8 | 330400 | 0.0000 |
| employees | salaries | emp_no | 274911 | 2494090 | 0.1102 |
| employees | dept_manager | dept_no | 9 | 24 | 0.3750 |
| employees | titles | emp_no | 298025 | 441607 | 0.6749 |
| employees | dept_emp | emp_no | 299687 | 330400 | 0.9070 |
| employees | titles | PRIMARY | 441607 | 441607 | 1.0000 |
| employees | dept_manager | emp_no | 24 | 24 | 1.0000 |
| employees | departments | dept_name | 9 | 9 | 1.0000 |
| employees | salaries | PRIMARY | 2494090 | 2494090 | 1.0000 |
| employees | dept_emp | PRIMARY | 330400 | 330400 | 1.0000 |
| employees | dept_manager | PRIMARY | 24 | 24 | 1.0000 |
| employees | departments | PRIMARY | 9 | 9 | 1.0000 |
| employees | employees | PRIMARY | 298303 | 298303 | 1.0000 |
+--------------+--------------+------------+-------------+------------+-------------+
13 rows in set (0.58 sec)

---- whether by close to 1 SELECTIVITY last column, determine whether it is reasonable to create the index
Note: The
value Cardinality and table_rows, are by random sampling, estimates obtained
before and after when analyze, Cardinality value of the difference between the more, indicating that the index should not be created (recorded on page uneven distribution of value)

Recommended SELECTIVITY more than 15% is appropriate

The index is to be sorted, the more indexing, sorting and maintenance costs will be great, insert the data speed will be very slow, so much, not only a waste of space indexing, but also reduce performance, increase disk IO.

Note: There is a problem MySQL5.6 versions STATISTICS data, as of 5.6.28 persists, official characterized as BUG


MySQL5.6 install sys library
shell> git clone https://github.com/mysql/mysql-sys.git
shell> LS | grep sys_56.sql
sys sys_56.sql # this is what we want to install to the mysql5.6

shell> mysql -uroot -S /tmp/mysql.sock_56 <sys_56.sql # can be introduced directly into


EXPLAIN (two)
1.Explain output Introduction

Program execution flag id id
type of select_type SELECT
table output record table
partitions in line with the partition, [PARTITIONS]
type of the JOIN type
possible_keys optimizer might use the index to
the index key optimizer actually selected
key_len byte length using the index
ref comparing the index column
number of records estimated rows optimizer
filtered according to the filter conditions to obtain the percentage recorded [the EXTENDED]
extra additional display options


(1) .id
appreciated from above, a large number is not necessarily performed first id

( 2) .select_type
the sIMPLE simple SELECT (not using UNION or subqueries, etc.)
a PRIMARY SELECT outermost
second or later in the select statement UNION UNION
DEPENDENT UNION UNION second select statement or later, depending on the outside of the query
result of UNION RESULT UNION
SUBQUERY first subquery SELECT
first subquery SELECT DEPENDENT SUBQUERY, depending on the outside of the query
DERIVED derived table SELECT (FROM clause subquery)
mATERIALIZED materialized subquery
UNCACHEABLE sUBQUERY not be cached for each row and the outer query recalculated subquery
second Uncacheable UNION belongs can not be cached in the UNION or the back of the SELECT statement


MATERIALIZED
  generates intermediate temporary table (entity)
  a temporary table to automatically create indexes and other table to associate, to improve the performance
  difference and sub-queries, the statement optimizer will be able to MATERIALIZED automatically rewrite the JOIN, and automatically create an index

(3) .table
usually the user operates user table
<unionM, N> obtained results in table UNION
row green sheet, produced by id = N statement
by the sub-tables generated materialized query generated by the statement id = N

 (4).type

(. 5) .extra
the Using filesort require additional sort result obtained
Using index using an index optimizer need only have the results
Using index optimizer uses for condition Condition Index Condition Pushdown Optimization
Using index for group by the optimizer can handle only the index need to use group by or distinct statement
using join buffer optimization requires the use of the Join Buffer, join_buffer_size
the using MRR optimizer uses MRR optimization
using temporary optimization requires the use of a temporary table
using where the optimizer uses where filtering

① using filesort: using compound index filesort optimized and improve performance.
② Using index: an index such as using a cover
③ Using where: where filtering condition using

information Extra is optimized as tips, but more of a description of the optimizer to optimize

 

Guess you like

Origin www.cnblogs.com/green-frog-2019/p/11391743.html
Recommended