当前位置:网站首页>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 always def.

  • 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 ( FixedDynamicCompressedRedundantCompact).

  • TABLE_ROWS  Row number . Some storage engines , for example  MyISAM, Store the exact count . For other storage engines , for example InnoDB, This value is an approximation , It may be different from the actual value 40% To 50%. under these circumstances , Please use SELECT COUNT(*) To get an accurate count .

  • AVG_ROW_LENGTH  The average President .

  • DATA_LENGTH  about MyISAM,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 ) multiply InnoDB Page size .

    Information about other storage engines , Please refer to the notes at the end of this section .

  • MAX_DATA_LENGTH  about MyISAMMAX_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  about MyISAM,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 ) multiply  InnoDB Page size

  • AUTO_INCREMENT  next AUTO_INCREMENT value

  • CREATE_TIME  When the table was created

  • TABLE_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 always def.

  • 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 a  NULL If the column has an explicit default NULL, Or the column definition does not contain DEFAULT Clause .

  • IS_NULLABLE  Column nullability . The value is YES whether  NULL 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 yes  PRI, Then the column is PRIMARY KEY or Is one of many columns PRIMARY KEY.

    • If COLUMN_KEY yes  UNI, Then the column is UNIQUE The first column of the index .( UNIQUE Multiple indexes are allowed  NULL value , But you can NULL clear through  Null Column to determine whether the column allows .)

    • If COLUMN_KEY yes  MUL, 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  value RANGELISTHASHLINEAR HASHKEY, or  LINEAR 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 statement

  • DB  Thread's default database , perhaps  NULL If there is no choice .

  • COMMAND  The thread represents the type of command executed by the client , perhaps Sleep 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 , perhaps  NULL 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 , If CALL Statement execution is executing  SELECT Statement stored procedure , Then  INFO 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 .

  • Change user  The thread is performing a change user action .

  • Close stmt  The thread is closing the prepared statement .

  • Connect  Replica connected to its source .

  • Connect Out  The replica is connecting to its source .

  • 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 .

  • Debug  The thread is generating debug information .

  • Delayed insert  This thread is a delay insertion handler .

  • Drop DB  Thread is performing delete database operation .

  • Error

  • Execute  The thread is executing the prepared statement .

  • Fetch  The thread is getting results from executing the prepared statement .

  • Field List  This thread is retrieving information about table columns .

  • Init DB  The thread is selecting a default database .

  • Kill  This thread is killing another thread .

  • Long Data  The thread retrieves long data in the result of executing the prepared statement .

  • Ping  This thread is processing the server ping request .

  • Prepare  The thread is preparing a prepared statement .

  • Processlist  This thread is generating information about the server thread .

  • Query  The thread is executing a statement .

  • Quit  Thread terminating .

  • 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 .

  • Reset stmt  The thread is resetting the prepared statement .

  • Set option  The thread is setting or resetting the client statement execution options .

  • Shutdown  The thread is shutting down the server .

  • 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 from  MEMORY 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 different ORDER BY Of  GROUP 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 processing ALTER TABLE ... ENABLE KEYS One MyISAM 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 is Copying 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 processing ALTER TABLE ... DISCARD TABLESPACEorALTER TABLE ... IMPORT TABLESPACE sentence .

  • end  This happens at the end , But cleaning up  ALTER TABLECREATE VIEWDELETEINSERTSELECT, or UPDATE The statement before . about end 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

  • executing  The thread has started executing the statement .

  • Execution of init_command  Thread is executing  init_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 by cleaning up.

  • FULLTEXT initialization  The server is preparing to perform a natural language full-text search .

  • init  This happened in  ALTER TABLEDELETEINSERTSELECT, 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 .

  • preparing  This state occurs during query optimization .

  • Purging old relay logs  This thread is deleting unneeded relay log files .

  • query end  This state occurs after processing the query but at  freeing items Before status .

  • Receiving from client  The server is reading packets from the client . This state Reading 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 .

  • removing tmp table

    SELECT  The thread is processing statements   Deleting internal temporary tables . If you do not create a temporary table , This state is not used .

  • rename  The thread is renaming a table .

  • 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 .

  • Repair by sorting  Fix code using sort to create index .

  • preparing for alter table  The server is preparing to perform in place  ALTER TABLE.

  • Rolling back  Thread is rolling back transaction .

  • Saving state  about MyISAM 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 state Writing 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 a GROUP BY.

  • Sorting for order  The thread is performing sorting to meet ORDER 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 to Creating sort index, But for non temporary tables .

  • starting  The first stage of statement execution .

  • 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 called mysql_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 .

  • Waiting for commit lock

    FLUSH TABLES WITH READ LOCK  Waiting for lock submission .

  • Waiting for global read lock

    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 lock_type lock THR_LOCK The server is waiting to lock the subsystem from metadata   obtain Lock or lock , among lock_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 called Sending to client.

原网站

版权声明
本文为[Night hunter - Demon King]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207061131155097.html