当前位置:网站首页>MySQL information schema learning (I) -- general table
MySQL information schema learning (I) -- general table
2022-07-06 19:30:00 【Night hunter - Demon King】
Sometimes there are certain needs , For example, you need to view the fields of specific conditions in the statistical database , Before, I would analyze statistics one by one , Actually information schema It provides very powerful functions , You can study it .
Reference material , Official document :
MySQL :: MySQL 5.7 Reference Manual :: 24 INFORMATION_SCHEMA Tableshttps://dev.mysql.com/doc/refman/5.7/en/information-schema.htmlinformation schema There's something in it , Look at the documentation :
INFORMATION_SCHEMA
Provide access to the database Metadata 、 of MySQL Server information ( For example, the name of a database or table 、 The data type or access rights of the column ) The interview of .information The table inside is the view , No data files :INFORMATION_SCHEMA
Is each MySQL A database in the instance , This location stores information about MySQL Information about all other databases maintained by the server . The INFORMATION_SCHEMA
The database contains several read-only tables . They're actually views , Instead of the base table , So there are no files associated with them , And you can't set triggers on them . Besides , There is no database directory with that name .
Take a general look at schema What are the tables below :
It can be seen that the main reason is database metadata permissions , Variables and a large part INNODB Storage engine related tables .
Mainly learn about commonly used tables
1. TABLES
TABLES | Table information |
tables Table mainly describes the metadata of database tables , You can get some information about the table , For example, the number of rows , Take up space, etc .
The main fields are as follows :
TABLE_CATALOG
The name of the directory to which the table belongs . The value is alwaysdef
.TABLE_SCHEMA
The schema to which the table belongs ( database ) The name of .TABLE_NAME
The name of the table .TABLE_TYPE
BASE TABLE/SYSTEM VIEW
ENGINE
Table storage engine . For partitioned tables ,ENGINE
Displays the name of the storage engine used by all partitions .VERSION
.frm
Table file Version number of .ROW_FORMAT
Row storage format (Fixed
,Dynamic
,Compressed
,Redundant
,Compact
).TABLE_ROWS
Row number . Some storage engines , for exampleMyISAM
, Store the exact count . For other storage engines , for exampleInnoDB
, This value is an approximation , It may be different from the actual value 40% To 50%. under these circumstances , Please useSELECT COUNT(*)
To get an accurate count .AVG_ROW_LENGTH
The average President .DATA_LENGTH
aboutMyISAM
,DATA_LENGTH
Is the length of the data file , In bytes .about
InnoDB
,DATA_LENGTH
Is the approximate amount of space allocated for the clustered index , In bytes . say concretely , It is the size of the clustered index ( In pages ) multiplyInnoDB
Page size .Information about other storage engines , Please refer to the notes at the end of this section .
MAX_DATA_LENGTH
aboutMyISAM
,MAX_DATA_LENGTH
Is the maximum length of the data file . Given the size of the data pointer used , This is the total number of bytes of data that can be stored in the table .INDEX_LENGTH
aboutMyISAM
,INDEX_LENGTH
Is the length of the index file , In bytes .about
InnoDB
,INDEX_LENGTH
Is the approximate amount of space allocated for nonclustered indexes , In bytes . say concretely , It is the sum of the nonclustered index sizes ( In pages ) multiplyInnoDB
Page sizeAUTO_INCREMENT
nextAUTO_INCREMENT
valueCREATE_TIME
When the table was createdTABLE_COMMENT
Comments used when creating tables
according to data_length+index_length You can roughly count the space occupied by the table .
For example, calculate the space size of all tables
SELECT TABLE_NAME, sum(DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024 / 1024 as size FROM information_schema.`TABLES` GROUP BY TABLE_NAME ORDER BY size desc;
2.COLUMNS
COLUMNS | Columns in each table |
Provide information about the columns in the table . Has the following columns :
TABLE_CATALOG
The name of the directory to which the table containing this column belongs . The value is alwaysdef
.TABLE_SCHEMA
The schema of the table containing this column ( database ) The name of .TABLE_NAME
The name of the table containing the column .COLUMN_NAME
Column name .ORDINAL_POSITION
The position of the column in the table .COLUMN_DEFAULT
Default value of column . This is aNULL
If the column has an explicit defaultNULL
, Or the column definition does not containDEFAULT
Clause .IS_NULLABLE
Column nullability . The value isYES
whetherNULL
You can store values in columns ,NO
If not .DATA_TYPE
Column data type .CHARACTER_MAXIMUM_LENGTH
For character string Columns , The maximum length in characters .CHARACTER_OCTET_LENGTH
For character string Columns , Maximum length ( In bytes ).NUMERIC_PRECISION
For numeric Columns , Numerical accuracy .NUMERIC_SCALE
For columns of numbers , Digital scale .DATETIME_PRECISION
For the time column , Decimal second precision .CHARACTER_SET_NAME
For character string Columns , Character set name .COLLATION_NAME
For character string Columns , Collation name .COLUMN_TYPE
COLUMN_TYPE
The value contains the type name and possible additional information , Such as precision or length .COLUMN_KEY
Whether the column is indexed :If
COLUMN_KEY
It's empty , Then the column is either not indexed , Or just as multiple columns 、 Auxiliary columns in non unique indexes are indexed .If
COLUMN_KEY
yesPRI
, Then the column isPRIMARY KEY
or Is one of many columnsPRIMARY KEY
.If
COLUMN_KEY
yesUNI
, Then the column isUNIQUE
The first column of the index .(UNIQUE
Multiple indexes are allowedNULL
value , But you canNULL
clear throughNull
Column to determine whether the column allows .)If
COLUMN_KEY
yesMUL
, Then this column is the first column of the non unique index , The given value is allowed to appear more than once in this column .
PRIVILEGES
Your permissions on this column .COLUMN_COMMENT
Any comments contained in the column definition .
adopt columns Tables can be generated in batches DDL Statement etc. . For example, the string field length of all tables under the database 40 Expand to 60 Then you can query column_type=varchar(40) What are the column , And then batch generate ddl sql.
3.PARTITIONS
PARTITIONS | Table partition information |
Data stored in the partition table , The main field :
PARTITION_NAME
The name of the partition .PARTITION_METHOD
valueRANGE
,LIST
,HASH
,LINEAR HASH
,KEY
, orLINEAR KEY
;TABLE_ROWS
Number of table rows in partition . You can roughly count the number of rows in the partitioned table
4.PROCESSLIST
PROCESSLIST | Information about the current execution thread |
show processlist; You can view the active connections of the current database , Threads, etc , It is often used to check the current running state of the database .
MySQL The process list indicates the operations currently performed by the thread set executing in the server .
Core column :
ID
Connection identifier . have access to KILL Statement kills the thread .USER
Issuing statements MySQL user .HOST
The hostname of the client that issued the statementDB
Thread's default database , perhapsNULL
If there is no choice .COMMAND
The thread represents the type of command executed by the client , perhapsSleep
Whether the session is idle .TIME
The time that the thread is in the current state ( In seconds ).STATE
An action that indicates what the thread is doing 、 An event or state . Most states correspond to very fast operations . If a thread stays in a given state for a few seconds , There may be problems that need to be investigated .INFO
The statement that the thread is executing , perhapsNULL
If it does not execute any statements . If this statement executes other statements , Then the statement may be a statement sent to the server , Or the innermost statement . for example , IfCALL
Statement execution is executing SELECT Statement stored procedure , ThenINFO
The value shows this SELECT sentence .
Don't say anything , Each of these fields is often used , Here's to learn command and STATE state ,
COMMAND The values are mainly as follows :
Threads can have any of the following Command
value :
Binlog Dump
This is a thread on the replication source , Used to send binary log contents to the copy .Create DB
The thread is executing the create database operation .Daemon
The thread is inside the server , Instead of a thread that serves client connections .Fetch
The thread is getting results from executing the prepared statement .Field List
This thread is retrieving information about table columns .Long Data
The thread retrieves long data in the result of executing the prepared statement .Processlist
This thread is generating information about the server thread .Refresh
The thread is refreshing the table 、 Log or cache , Or reset state variables or copy server information .Register Slave
This thread is registering the replica server .Set option
The thread is setting or resetting the client statement execution options .Sleep
The thread is waiting for the client to send it a new statement .Statistics
The thread is generating server status information .
STATE The main states are as follows :
After create
When the thread creates the table at the end of the function that creates the table ( Include internal temporary tables ) when , That's what happens . Even if the table cannot be created due to some errors , This state will also be used .altering table
The server is performing in place ALTER TABLE.- checking permissions The thread is checking whether the server has the permissions required to execute the statement .
Checking table
The thread is performing a table check operation .cleaning up
The thread has processed a command and is ready to free memory and reset some state variables .closing tables
The thread is flushing the changed table data to disk and closing the used table . This should be a quick operation . without , Please make sure that you do not have a complete disk and that the disk is not used very much .converting HEAP to ondisk
The thread is removing the internal temporary table fromMEMORY
Convert table to disk table .copy to tmp table
The thread is processing a ALTER TABLE sentence . This state occurs after creating a table with a new structure, but before copying rows into it . For threads in this state , have access to Performance Schema To get the progress of the replication operation .Copying to group table
If the statement has differentORDER BY
OfGROUP BY
Conditions , Then the rows will be sorted by group and copied to the temporary table .Copying to tmp table
The server is copying to a temporary table in memory .Copying to tmp table on disk
The server is copying to a temporary table on disk . The temporary result set becomes too large . therefore , The thread changes the temporary table from memory to disk based format to save memory .Creating index
Thread is processingALTER TABLE ... ENABLE KEYS
OneMyISAM
surface .Creating sort index
Thread is processing SELECT Resolved using internal temporary tables a.creating table
This thread is creating a table . This includes creating temporary tables .Creating tmp table
The thread is creating a temporary table in memory or on disk . If the table is created in memory , But later converted to disk table , Then the status during this operation isCopying to tmp table on disk
.committing alter table to storage engine
The server has completed in place ALTER TABLE And is submitting the results .deleting from main table
The server is performing the first part of multi table deletion . It only removes from the first table , And save for use from other ( Reference resources ) Columns and offsets deleted in the table .deleting from reference tables
The server is performing the second part of multi table deletion , And delete matching rows from other tables .discard_or_import_tablespace
Thread is processingALTER TABLE ... DISCARD TABLESPACE
orALTER TABLE ... IMPORT TABLESPACE
sentence .end
This happens at the end , But cleaning up ALTER TABLE, CREATE VIEW, DELETE, INSERT, SELECT, or UPDATE The statement before . aboutend
state , The following actions may occur :Delete the query cache entry after changing the data in the table
Write events to the binary log
Free the memory buffer , Include blob
Execution of init_command
Thread is executinginit_command
Statements in system variable values .freeing items
The thread has executed the command . Some of the item releases completed during this state involve the query cache . This state is usually followed bycleaning up
.FULLTEXT initialization
The server is preparing to perform a natural language full-text search .init
This happened in ALTER TABLE, DELETE, INSERT, SELECT, or UPDATE Before the initialization of the statement . The actions taken by the server in this state include refreshing the binary log 、InnoDB
Logs and some query cache cleanup operations .Killed
someone KILL Sent a statement to the thread , It should be checked next time kill Flag to abort . stay MySQL This flag is checked in each main cycle of , But in some cases , The thread may still take a short time to end . If a thread is locked by another thread , Then terminate immediately after other threads release their locks .logging slow query
The thread is writing a statement to the slow query log .login
The initial state of the connection thread , Until the client is successfully authenticated .manage keys
The server is enabling or disabling table indexing .Opening tables
The thread is trying to open a table . This should be a very fast process , Unless something prevents it from opening . for example , One ALTER TABLEor LOCK TABLE Statement can prevent opening the table , Until the statement is completed . Check your table_open_cache Whether the value is large enough is also worth it .optimizing
The server is performing initial optimization for the query .Purging old relay logs
This thread is deleting unneeded relay log files .query end
This state occurs after processing the query but atfreeing items
Before status .Receiving from client
The server is reading packets from the client . This stateReading from net
stay MySQL 5.7.8 Called before .Removing duplicates
Use of query SELECT DISTINCT In such a way that MySQL Different operations cannot be optimized at an early stage . therefore ,MySQL An additional stage is needed to remove all duplicate rows , Then send the results to the client .SELECT The thread is processing statements Deleting internal temporary tables . If you do not create a temporary table , This state is not used .
rename result table
The thread is processing a ALTER TABLE sentence , A new table has been created , And is renaming it to replace the original table .Reopen tables
The thread obtained the lock of the table , But after obtaining the lock, notice that the underlying table structure has changed . It releases the lock , Table closed , And try to reopen it .preparing for alter table
The server is preparing to perform in place ALTER TABLE.Saving state
aboutMyISAM
Repair or analyze table operations , The thread is saving the new table state to.MYI
The file header . The status includes the number of rows 、AUTO_INCREMENT
Information such as counters and key distribution .Searching rows for update
The thread is executing the first stage to find all matching rows before updating them . If UPDATE Changing the index used to find related rows , You have to do this .Sending data
This thread is reading and processing SELECT Line of statement , And send the data to the client . Because the operations that occur during this state tend to perform a lot of disk access ( Read ), Therefore, it is usually the longest running state in a given query lifecycle .Sending to client
The server is writing packets to the client . This stateWriting to net
stay MySQL 5.7.8 Called before .setup
The thread is starting a ALTER TABLE operation .Sorting for group
The thread is performing sorting to satisfy aGROUP BY
.Sorting for order
The thread is performing sorting to meetORDER BY
.Sorting index MyISAM
This thread is sorting index pages , In order to optimize the operation in the table Make more effective visits during .Sorting result
about SELECT sentence , This is similar toCreating sort index
, But for non temporary tables .statistics
The server is calculating statistics to make a query execution plan . If a thread is in this state for a long time , The server may be doing other work on disk binding .System lock
Thread calledmysql_lock_tables()
And the thread status has not been updated since . This is a very common state , May occur for a variety of reasons . for example , The thread is about to request or waiting for the internal or external system lock of the table . stay InnoDB perform LOCK TABLES. If this state is caused by an external lock request , And you don't use multiple to access the same table mysqld The server , You can use the Options MyISAM Disable external system lock .--skip-external-locking however , External locking is disabled by default , Therefore, this option is likely to have no effect . about SHOW PROFILE, This state means that the thread is requesting a lock ( Don't wait for it ).update
The thread is preparing to start updating the table .Updating
The thread is searching for rows to update and updating them .updating main table
The server is performing the first part of a multi table update . It only updates the first table , And save for updating other ( Reference resources ) Table columns and offsets .updating reference tables
The server is performing the second part of the multi table update , And update the matching rows from other tables .User lock
The thread is about to request or is waiting to call the requested Advisory lock GET_LOCK(). about SHOW PROFILE, This state means that the thread is requesting a lock ( Don't wait for it ).User sleep
The thread called a SLEEP() call .FLUSH TABLES WITH READ LOCK Waiting for lock submission .
FLUSH TABLES WITH READ LOCK Waiting for global read lock or read_only Setting global system variables .
Waiting for tables
The thread receives a notification that the infrastructure of the table has changed , It needs to reopen the table to get the new structure . however , To reopen the table , It must wait until all other threads have closed the table in question .Waiting for table flush
Thread is executing FLUSH TABLES And waiting for all threads to close their tables , Or the thread receives a notification that the underlying structure of the table has changed , It needs to reopen the table to get the new structure . however , To reopen the table , It must wait until all other threads have closed the table in question .Waiting for
The server is waiting to lock the subsystem from metadata obtain Lock or lock , amonglock_type
lock THR_LOCKlock_type
Indicates the type of lock .This status indicates waiting a
THR_LOCK
:Waiting for table level lock
These states indicate waiting for metadata lock :
Waiting for event metadata lock
Waiting for global read lock
Waiting for schema metadata lock
Waiting for stored function metadata lock
Waiting for stored procedure metadata lock
Waiting for table metadata lock
Waiting for trigger metadata lock
You can get that the current thread state is waiting for a lock , Especially execution DDL when , Need to pay attention to , It is easy to cause the link not to be released because the metadata lock cannot be obtained , Database crash .
Waiting on cond
The thread is waiting for the condition to become a true general state . No specific status information is available .Writing to net
The server is writing packets to the network . from MySQL 5.7.8 Start , This state is calledSending to client
.
边栏推荐
- 三面蚂蚁金服成功拿到offer,Android开发社招面试经验
- Dark horse -- redis
- CCNP Part 11 BGP (III) (essence)
- spark基础-scala
- 时钟轮在 RPC 中的应用
- 保证接口数据安全的10种方案
- 【翻译】Linkerd在欧洲和北美的采用率超过了Istio,2021年增长118%。
- 倒计时2天|腾讯云消息队列数据接入平台(Data Import Platform)直播预告
- R language ggplot2 visualization: use the ggstripchart function of ggpubr package to visualize the grouped dot strip plot, and set the add parameter to add box plots for different levels of dot strip
- Carte de réflexion + code source + notes + projet, saut d'octets + jd + 360 + tri des questions d'entrevue Netease
猜你喜欢
PMP每日一练 | 考试不迷路-7.6
Problems encountered in using RT thread component fish
包装行业商业供应链管理平台解决方案:布局智慧供应体系,数字化整合包装行业供应链
三面蚂蚁金服成功拿到offer,Android开发社招面试经验
ACTF 2022圆满落幕,0ops战队二连冠!!
Simple understanding of MySQL database
Zero foundation entry polardb-x: build a highly available system and link the big data screen
Tongyu Xincai rushes to Shenzhen Stock Exchange: the annual revenue is 947million Zhang Chi and Su Shiguo are the actual controllers
学习探索-使用伪元素清除浮动元素造成的高度坍塌
The list of people who passed the fifth phase of personal ability certification assessment was published
随机推荐
接雨水问题解析
Interview assault 63: how to remove duplication in MySQL?
Problems encountered in using RT thread component fish
Leetcode 30. 串联所有单词的子串
MATLAB中deg2rad和rad2deg函数的使用
Php+redis realizes the function of canceling orders over time
Simple understanding of MySQL database
Use of map (the data of the list is assigned to the form, and the JSON comma separated display assignment)
LeetCode_双指针_中等_61. 旋转链表
How to type multiple spaces when editing CSDN articles
Graffiti intelligence is listed on the dual main board in Hong Kong: market value of 11.2 billion Hong Kong, with an annual revenue of 300 million US dollars
史上超级详细,想找工作的你还不看这份资料就晚了
PMP practice once a day | don't get lost in the exam -7.6
ACTF 2022圆满落幕,0ops战队二连冠!!
Mathematical knowledge -- code implementation of Gaussian elimination (elementary line transformation to solve equations)
安装Mysql报错:Could not create or access the registry key needed for the...
Dark horse -- redis
JDBC详解
Characteristic colleges and universities, jointly build Netease Industrial College
通俗的讲解,带你入门协程