Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 50 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
50
Dung lượng
0,92 MB
Nội dung
The MySQL Data Dictionary M ySQL stores information about the data in the databases; this is called metadata Much of this information is stored in the INFORMATION_SCHEMA database, following the SQL 2003 IN THIS CHAPTER standard Examining the Object catalog Tables in the INFORMATION_SCHEMA database are read-only, in-memory, and show data from various sources System information SHOW CREATE TABLE will show the tables as TEMPORARY, because they reside in memory and not persist between mysqld restarts INFORMATION_SCHEMA tables are called system views and they may be of Managing permissions Developing custom metadata different storage engine types At the time of this writing all the system views are either the MEMORY, MyISAM, or Maria storage engine Regular SQL statements can be used to query them, though they have some special properties that other views not have: ■ mysqldump will not export any information (data, schema) from INFORMATION_SCHEMA system views ■ There is no data directory for the INFORMATION_SCHEMA database ■ There is no frm file associated with the INFORMATION_SCHEMA views The definitions are hard-coded into the database The table definitions for the data dictionary are hard-coded into the source code, and loaded when mysqld starts Unlike other databases, there is no directory in the datadir for the INFORMATION_SCHEMA database All users have permission to see the INFORMATION_SCHEMA database; however, they can only see the objects they have permission to see For example, table details in the TABLES system view are limited to the tables that the user has permission to see 667 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark Part IV Extending Your Skills Some of the metadata provided by the INFORMATION_SCHEMA database is also provided by various SHOW commands (see Chapter 4) The INFORMATION_SCHEMA database is a more complete data dictionary than using SHOW commands Also, standard SQL statements can be used to query the system views to retrieve metadata The INFORMATION_SCHEMA database contains more than 40 system views They can be informally categorized as: ■ Object catalog (databases, tables, columns, and so on) ■ System information (variables, statistics, available options) ■ Permissions ■ Storage engine-specific metadata Querying metadata is a powerful tool for a database administrator, answering simple questions such as ‘‘how many tables of each storage engine type exist?’’ and ‘‘which tables have columns using the DECIMAL type?’’ and ‘‘how many foreign key constraints exist?’’ and even ‘‘how much space does a certain group of three tables use?’’ Querying metadata provides a way to retrieve information about the system that can be used to track and tune performance Any tool that performs queries can retrieve metadata by querying the INFORMATION_SCHEMA database, exactly the same way it queries any other database Object Catalog The INFORMATION_SCHEMA database contains system views with metadata about objects such as databases, tables, views, columns, indexes, partitions, stored routines, triggers, and events SCHEMATA ‘‘Schema’’ is another name for a database, and ‘‘schemata’’ is the plural of schema The SCHEMATA system view in the INFORMATION_SCHEMA database provides information about all the databases, including the mysql system database and the INFORMATION_SCHEMA database itself The fields in the SCHEMATA system view are: ■ CATALOG_NAME — Provided for standards compliance However, because MySQL does not have catalogs, this value is always NULL ■ SCHEMA_NAME — The name of the database, such as sakila ■ DEFAULT_CHARACTER_SET_NAME — The default character set of the database If no default character set is assigned by a CREATE DATABASE or ALTER DATABASE command, the default character set for the system is stored Thus, the DEFAULT_CHARACTER_SET_ NAME field always has a non-NULL value, and defaults to the character set of the system at the time of database creation 668 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark The MySQL Data Dictionary ■ DEFAULT_COLLATION_NAME — The default collation of the database If no default collation is assigned by a CREATE DATABASE or ALTER DATABASE command, the default collation for the system is stored Thus, the DEFAULT_CHARACTER_SET_NAME field always has a non-NULL value, and defaults to the collation of the system at the time of database creation ■ SQL_PATH — Provided for standards compliance, this field is usually used to find files related to the database However, MySQL does not support this field, so it is always NULL The SHOW DATABASES command is a shorter way to find the names of existing databases than running SELECT SCHEMA_NAME FROM SCHEMATA To show a subset of all databases, it is easier to use the SCHEMATA system view The SHOW DATABASES command returns a result set where the field name is Database Because Database is a reserved word, in order to use the WHERE extension to SHOW DATABASES, the Database field must be quoted: mysql> SHOW DATABASES WHERE Database NOT IN (’mysql’,’information_ schema’); ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ’NOT IN (’mysql’,’information_schema’)’ at line mysql> SHOW DATABASES WHERE `Database` NOT IN (’mysql’,’information_ schema’); + + | Database | + + | sakila | | test | + + rows in set (0.02 sec) See Chapter for more information about using the backtick (`) to quote identifiers The field name in the SCHEMATA system view is SCHEMA_NAME, which is not a reserved word, and does not need to be escaped: mysql> USE INFORMATION_SCHEMA; Database changed mysql> SELECT SCHEMA_NAME FROM SCHEMATA WHERE SCHEMA_NAME NOT IN (’mysql’,’information_schema’); + -+ | SCHEMA_NAME | + -+ | sakila | | test | + -+ rows in set (0.02 sec) The SHOW DATABASES command also accepts the LIKE extension The SHOW SCHEMAS command behaves the same way as the SHOW DATABASES command, and outputs the same information — SCHEMAS is an alias for DATABASES Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 669 21 Part IV Extending Your Skills The SHOW CREATE DATABASE command returns two fields: Database, which is equivalent to SCHEMA_NAME, and Create Database, which does not have an exact equivalent but contains the value of DEFAULT_CHARACTER_SET_NAME SCHEMA is an alias for DATABASE, so SHOW CREATE SCHEMA returns the same information as SHOW CREATE DATABASE does TABLES Metadata about non-temporary tables is available in the TABLES system view The fields in the TABLES system view are: ■ TABLE_CATALOG — Provided for standards compliance However, because MySQL does not have catalogs, this value is always NULL ■ TABLE_SCHEMA — The name of the database, such as sakila ■ TABLE_NAME — The name of the table ■ TABLE_TYPE — Whether the table is a base table, view, or system view Only the INFORMATION_SCHEMA views are system views ■ ENGINE — The storage engine of the table, such as InnoDB To find out which tables, if any, in the INFORMATION_SCHEMA database not use the MEMORY storage engine: mysql> SELECT TABLE_NAME,ENGINE FROM TABLES WHERE TABLE_ SCHEMA=’INFORMATION_SCHEMA’ AND ENGINE!=’MEMORY’; + -+ + | TABLE_NAME | ENGINE | + -+ + | COLUMNS | MARIA | | EVENTS | MARIA | | PARAMETERS | MARIA | | PARTITIONS | MARIA | | PLUGINS | MARIA | | PROCESSLIST | MARIA | | ROUTINES | MARIA | | TRIGGERS | MARIA | | VIEWS | MARIA | + -+ + rows in set (0.56 sec) ■ From this query we see that most system views in the INFORMATION_SCHEMA database are the MEMORY storage engine, but there are some that use the Maria storage engine ■ VERSION — The version of the frm file, currently 10 The VERSION is NULL for table objects that not have frm files, such as views The exception to this rule is system views, which have a VERSION of 10 — even though there are no frm files, system views have hard-coded definitions, and thus have versions ■ ROW_FORMAT — Different storage engines allow the row storage to vary Fixed-width rows are a fixed size, which minimizes fragmentation Dynamic rows are a variable size, which are good for variable-length data, such as VARCHAR, TEXT, and BLOB InnoDB has 670 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark The MySQL Data Dictionary a compact row format by default, which eliminates some redundant data When an InnoDB table has a redundant format, there is less CPU work needed at the cost of additional storage space MyISAM has a compressed format, obtained by packing the data with the myisampack tool See Chapter 11 for more details on myisampack ■ TABLE_ROWS — The number of rows this table contains This value may be an estimate, depending on the storage engine The value is NULL for views and system views (INFORMATION_SCHEMA tables) ■ AVG_ROW_LENGTH — The average size in bytes of rows this table contains This value may be an estimate, depending on the storage engine The value is NULL for views, but has a value for system views If there are no rows, the value will be ■ DATA_LENGTH — The size in bytes of rows this table contains This value may be an estimate, depending on the storage engine The value is NULL for views and for MEMORY tables System views that are not MEMORY tables have a value for DATA_LENGTH ■ MAX_DATA_LENGTH — The maximum size in bytes that this table may contain The value is NULL for views, because there is no data stored The value is for storage engines that not populate this field, such as Falcon and CSV ■ INDEX_LENGTH — The size in bytes of the indexes for this table This value may be an estimate, depending on the storage engine The value is NULL for views and for MEMORY tables System views that are not MEMORY tables have a value for DATA_LENGTH ■ DATA_FREE — The size in bytes of the free space allocated for this table, and still available This value may be an estimate, depending on the storage engine The value is NULL for views and for system views Many tables have a DATA_FREE value of because there is not space allocated for them, though there may be plenty of free space available to them For example, CSV tables simply use available disk space, without needing MySQL to allocate space for rows In some storage engines such as MyISAM, this might indicate fragmentation and that the table needs to be rebuilt with an OPTIMIZE command See Chapter for more information about OPTIMIZE ■ AUTO_INCREMENT — The next AUTO_INCREMENT value to be used If the maximum AUTO_INCREMENT value for a table is 100, the value of AUTO_INCREMENT is 101 If a table has an AUTO_INCREMENT value and no rows have ever been stored in the table, the value of AUTO_INCREMENT is ■ CREATE_TIME — The DATETIME the table was created The value is NULL for views and MEMORY tables System views that use storage engines other than MEMORY have a proper DATETIME value FEDERATED tables have a value of NULL ■ UPDATE_TIME — The most recent DATETIME that an ALTER TABLE was performed on the table The value is NULL for views, CSV, and MEMORY tables MyISAM, Archive, and Maria tables that have never had ALTER TABLE performed on them have an UPDATE_TIME equivalent to their CREATE_TIME InnoDB and Falcon tables that have never had ALTER TABLE performed on them have a NULL value System views that use storage engines other than MEMORY have a proper DATETIME value FEDERATED tables have a value of NULL Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 671 21 Part IV Extending Your Skills ■ CHECK_TIME — The most recent DATETIME the table was checked with CHECK TABLE The value is NULL for views, system views, and tables that have never been checked or not support the check function ■ TABLE_COLLATION — The character set and collation of the table, for example utf8_bin utf8_general_ci, or latin1_swedish_ci The value is NULL for views If no default character set and collation is assigned by a CREATE TABLE or ALTER TABLE command, the default character set and collation are stored Thus, this field always has a non-NULL value for base tables and system views ■ CHECKSUM — Live checksums can be maintained for MyISAM tables (see Chapter 11) If this table is a MyISAM table with CHECKSUM=1, the live checksum value is displayed For all other tables, the value is NULL ■ CREATE_OPTIONS — CREATE TABLE has many different options The options that are not shown in other fields (such as TABLE_COLLATION) are shown in this field, separated by a space Sample values are partitioned and max_rows=10000 checksum=1 If there are no relevant options to CREATE TABLE, the value is the empty string (’’) The value is NULL for views ■ TABLE_COMMENT — The COMMENT option to CREATE TABLE and ALTER TABLE can be used to provide information about a table If there was no comment specified, the value is the empty string (‘’) The value is VIEW for views Most of the tables in the mysql system database have comments: mysql> SELECT TABLE_NAME, TABLE_COMMENT FROM TABLES WHERE TABLE_SCHEMA=’mysql’\G *************************** row *************************** TABLE_NAME: backup_history TABLE_COMMENT: *************************** row *************************** TABLE_NAME: backup_progress TABLE_COMMENT: *************************** row *************************** TABLE_NAME: columns_priv TABLE_COMMENT: Column privileges *************************** row *************************** TABLE_NAME: db TABLE_COMMENT: Database privileges *************************** row *************************** TABLE_NAME: event TABLE_COMMENT: Events *************************** row *************************** TABLE_NAME: func TABLE_COMMENT: User defined functions *************************** row *************************** TABLE_NAME: general_log TABLE_COMMENT: General log *************************** row *************************** TABLE_NAME: help_category 672 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark The MySQL Data Dictionary TABLE_COMMENT: help categories *************************** row *************************** TABLE_NAME: help_keyword TABLE_COMMENT: help keywords *************************** 10 row *************************** TABLE_NAME: help_relation TABLE_COMMENT: keyword-topic relation *************************** 11 row *************************** TABLE_NAME: help_topic TABLE_COMMENT: help topics *************************** 12 row *************************** TABLE_NAME: host TABLE_COMMENT: Host privileges; Merged with database privileges *************************** 13 row *************************** TABLE_NAME: ndb_binlog_index TABLE_COMMENT: *************************** 14 row *************************** TABLE_NAME: plugin TABLE_COMMENT: MySQL plugins *************************** 15 row *************************** TABLE_NAME: proc TABLE_COMMENT: Stored Procedures *************************** 16 row *************************** TABLE_NAME: procs_priv TABLE_COMMENT: Procedure privileges *************************** 17 row *************************** TABLE_NAME: servers TABLE_COMMENT: MySQL Foreign Servers table *************************** 18 row *************************** TABLE_NAME: slow_log TABLE_COMMENT: Slow log *************************** 19 row *************************** TABLE_NAME: tables_priv TABLE_COMMENT: Table privileges *************************** 20 row *************************** TABLE_NAME: time_zone TABLE_COMMENT: Time zones *************************** 21 row *************************** TABLE_NAME: time_zone_leap_second TABLE_COMMENT: Leap seconds information for time zones *************************** 22 row *************************** TABLE_NAME: time_zone_name TABLE_COMMENT: Time zone names *************************** 23 row *************************** TABLE_NAME: time_zone_transition TABLE_COMMENT: Time zone transitions *************************** 24 row *************************** TABLE_NAME: time_zone_transition_type TABLE_COMMENT: Time zone transition types Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 673 21 Part IV Extending Your Skills *************************** 25 row *************************** TABLE_NAME: user TABLE_COMMENT: Users and global privileges 25 rows in set (0.00 sec) There are a few SHOW commands that show table information SHOW TABLES returns one field, the equivalent of TABLE_NAME SHOW FULL TABLES adds another field, Table_type, which is the equivalent of TABLE_TYPE The SHOW CREATE TABLE command returns two fields: Table, which is the equivalent of TABLE_NAME, and Create Table, which is the full CREATE TABLE statement There is no equivalent in the TABLES system view, though it includes the information in ENGINE, AUTO_INCREMENT, CREATE_OPTIONS, and TABLE_COMMENT SHOW TABLE STATUS returns many fields: ■ Name — Equivalent to TABLE_NAME ■ Engine, Version, and Row_format — Equivalent to ENGINE, VERSION, and ROW_FORMAT ■ Rows — Equivalent to TABLE_ROWS ■ Avg_row_length, Data_length, Max_data_length, Index_length, Data_free, Auto_increment, Create_time, Update_time, and Check_time — Equivalent to AVG_ROW_LENGTH, DATA_LENGTH, MAX_DATA_LENGTH, INDEX_LENGTH, DATA_FREE, AUTO_INCREMENT, CREATE_TIME, UPDATE_TIME, and CHECK_TIME ■ Collation — Equivalent to TABLE_COLLATION ■ Checksum, Create_options — Equivalent to CHECKSUM and CREATE_OPTIONS ■ Comment — Equivalent to TABLE_COMMENT VIEWS The TABLES system view includes rows for views However, many of the fields in TABLES are NULL for views, and some features specific to views are not encompassed in the TABLES system view So MySQL provides the VIEWS system view, with the following fields: ■ TABLE_CATALOG — Provided for standards compliance However, because MySQL does not have catalogs, this value is always NULL ■ TABLE_SCHEMA — The name of the database, such as sakila ■ TABLE_NAME — The name of the view ■ VIEW_DEFINITION — The SELECT statement that defines the view If the current user is not the definer, the value will be blank, even if the current user has permissions to see the view definition: mysql> SELECT DEFINER,VIEW_DEFINITION,CURRENT_USER() -> FROM VIEWS -> WHERE TABLE_NAME=’staff_list’; 674 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark The MySQL Data Dictionary + + -+ + | DEFINER | VIEW_DEFINITION | CURRENT_USER() | + + -+ + | root@localhost | | root@127.0.0.1 | + + -+ + row in set (0.03 sec) mysql> SHOW GRANTS; + -+ | Grants for root@127.0.0.1 | + -+ | GRANT ALL PRIVILEGES ON *.* TO ’root’@’127.0.0.1’ WITH GRANT| | OPTION | + -+ row in set (0.00 sec) mysql> SHOW CREATE VIEW sakila.staff_list\G *************************** row *************************** View: staff_list Create View: CREATE ALGORITHM=UNDEFINED DEFINER=root@ localhost SQL SECURITY DEFINER VIEW sakila.staff_list AS select s.staff_id AS ID,concat(s.first_name,_utf8’ ’,s.last_name) AS name, a.address AS address,a.postal_code AS zip code,a.phone AS phone, sakila.city.city AS city,sakila.country.country AS country,s store_id AS SID from (((sakila.staff s join sakila.address a on((s.address_id = a.address_id))) join sakila.city on((a.city_id = sakila.city.city_id))) join sakila.country on((sakila.city country_id = sakila.country.country_id))) character_set_client: utf8mb3 collation_connection: utf8mb3_general_ci row in set (0.00 sec) ■ In this example, the user root@127.0.0.1 saw a blank view definition for the staff_list view, because the DEFINER is root@localhost ■ CHECK_OPTION — This value is NONE if the view definition has no WITH CHECK OPTION clause; CASCADED if the view definition contains WITH [CASCADED] CHECK OPTION, and LOCAL if the view definition contains WITH LOCAL CHECK OPTION ■ IS_UPDATABLE — YES if the view is updatable, NO if the view is not updatable See Chapter 8, subsection ‘‘Updatable Views,’’ for more information on updatable views ■ DEFINER — The view definer, in the MySQL user@host format ■ SECURITY_TYPE — DEFINER if the view definition was specified with the SQL SECURITY DEFINER option or did not contain an SQL SECURITY option The value is INVOKER if the view definition was specified with the SQL SECURITY INVOKER option ■ CHARACTER_SET_CLIENT — Stores the environmental character set as it was when the view was created Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 675 21 Part IV Extending Your Skills ■ COLLATION_CONNECTION — Stores the environmental collation as it was when the view was created SHOW CREATE VIEW is the SHOW command that shares the most information with the VIEWS system view The fields of SHOW CREATE VIEW are: ■ View — Equivalent to TABLE_NAME ■ Create View — No exact equivalent This is the full CREATE VIEW statement, and has elements from TABLE_NAME, VIEW_DEFINITION, CHECK_OPTION, IS_UPDATABLE, DEFINER, and SECURITY_TYPE ■ character_set_client — Equivalent to CHARACTER_SET_CLIENT ■ collation_connection — Equivalent to COLLATION_CONNECTION COLUMNS The COLUMNS system view contains information about table fields This system view contains information about the fields from every table, view, and system view ■ TABLE_CATALOG — Provided for standards compliance However, because MySQL does not have catalogs, this value is always NULL ■ TABLE_SCHEMA — The name of the database, such as sakila ■ TABLE_NAME — The name of the table ■ COLUMN_NAME — The name of the field ■ ORDINAL_POSITION — The number representing the order of the field The first field has a value of 1, the third field has a value of 3, and so on The value is never NULL ■ COLUMN_DEFAULT — The default value of the field If the default is not specified or specified as NULL, the value is NULL ■ IS_NULLABLE — Whether or not the field is allowed to be null If the field is specified as NOT NULL, the value is NO Otherwise, the value is YES Note that it is possible to have a table where the value of IS_NULLABLE is NO and the COLUMN_DEFAULT is NULL: mysql> USE test; Database changed mysql> CREATE TABLE paradox (numfield INT NOT NULL); Query OK, rows affected (0.11 sec) mysql> SELECT IS_NULLABLE,COLUMN_DEFAULT -> FROM INFORMATION_SCHEMA.COLUMNS -> WHERE TABLE_NAME=’paradox’ AND TABLE_SCHEMA=’test’; + -+ + | IS_NULLABLE | COLUMN_DEFAULT | + -+ + | NO | NULL | + -+ + row in set (0.00 sec) 676 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark Part IV Extending Your Skills TABLE 21-2 (continued ) Value General States (in alphabetical order) Meaning deleting from reference tables Deleting matched rows from the rest of the tables in a multi-table delete discard_or_import_ tablespace Discarding a table space, such as with ALTER TABLE tblname DISCARD TABLESPACE, or importing a tablespace, such as with ALTER TABLE tblname IMPORT TABLESPACE end The end of ALTER TABLE, CREATE VIEW, SELECT, INSERT, UPDATE, and DELETE statements The end state occurs just before the cleaning up state Execution of init_command The init_command system variable allows you to specify a command that gets run whenever a user connects For example, init_command="SET AUTOCOMMIT=0" freeing items Freeing items after a command has finished executing; usually followed by a state of cleaning up Flushing tables Waiting for threads to close tables while executing a FLUSH TABLES FULLTEXT initialization Preparing a FULLTEXT search init Initializing ALTER TABLE, SELECT, INSERT, UPDATE, and DELETE statements Killed Kill flag has been set, waiting for the thread to abort Locked Waiting for another query to finish logging slow query Writing to the slow query log login Waiting for successful authentication NULL The state of a SHOW PROCESSLIST statement Opening table[s] Attempting to open a table, most often seen when a table is locked and cannot be opened preparing Preparing a query during query optimization Purging old relay logs On a slave, purging relay logs after they have been applied query end Query processing completed; followed by the freeing items state Reading from net Reading a packet from the network 702 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark The MySQL Data Dictionary TABLE 21-2 (continued ) Value General States (in alphabetical order) Meaning Removing duplicates Removing duplicate rows, such as during a SELECT DISTINCT This may not appear if the DISTINCT operation was optimized out in an earlier step removing tmp table Removing an internal temporary table after a SELECT statement requiring the internal temporary table has been processed rename Renaming a table rename result table Replacing an old table with a new one via renaming during an ALTER TABLE statement Reopen tables Trying to re-open a table after initially getting a lock, realizing the table structure changed, releasing the lock, and closing the table Repair by sorting Sorting to create indexes during a REPAIR TABLE Repair done Multi-threaded repair for a MyISAM table has completed Repair with keycache Using the key cache to create indexes one row at a time Slower than Repair by sorting Rolling back Rolling back a transaction, as with ROLLBACK Saving state Saving table state information such as row count, AUTO_INCREMENT value, and key distributions to the MYI file (MyISAM tables only) This occurs during statements such as REPAIR TABLE or ANALYZE TABLE Searching rows for update Finding all matching rows This occurs when the rows must be found first, as when an UPDATE is changing an index used to find the rows Note that changing the row data may change the index values for that row, so this can occur when an UPDATE changes data used in an index Sending data Returning data from a SELECT statement to the client program setup Starting an ALTER TABLE Sorting for group Sorting due to a GROUP BY clause Sorting for order Sorting due to an ORDER BY clause Sorting index Sorting index pages to be more efficient during a MyISAM table defragmentation, such as with OPTIMIZE TABLE Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark continued 703 21 Part IV Extending Your Skills TABLE 21-2 (continued ) Value General States (in alphabetical order) Meaning Sorting result Resolving a SELECT statement with an internal table If the internal table is temporary, the state is Creating sort index statistics Calculating statistics to help the query optimizer find a query execution plan System lock Waiting for an external system table lock External system locks are disabled by default and only needed if there is more than one program accessing the table, such as if there are multiple mysqld servers accessing the same tables Table lock Waiting for an internal table lock, after getting an external system table lock Updating Updating rows or searching for rows to update updating main table Updating the first table in a multi-table update, saving data to be used when updating the other tables, called reference tables updating reference tables Updating matched rows from the rest of the tables in a multi-table update User lock Waiting for a lock requested by a GET_LOCK() statement Waiting for table[s] Waiting for other threads to close a table so it can reopen the table after realizing the table structure changed Waiting on cond The generic state when there is no available state information Writing to net Writing a packet to the network States Related to INSERT DELAYED (in order of operation) Creating delayed handler Creating a delayed insert handler waiting for handler open Waiting for the handler initialization and the table to open got old table End of initialization phase after table has been opened waiting for INSERT Delayed insert handler is waiting for a row to be added to the batch 704 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark The MySQL Data Dictionary TABLE 21-2 (continued ) Value General States Meaning waiting for delay_list Waiting to receive the list of delayed insert handler threads waiting for handler lock Waiting for access to the delayed insert handler got handler lock Access to the delayed insert handler has been acquired allocating local table Preparing to send rows to the delayed insert handler storing row into queue Adding a row to the batch that the delayed handler insert will process Upgrading lock Delayed insert handler is waiting for a lock on the table to insert rows insert Delayed insert handler is inserting rows into the table reschedule Delayed insert handler is inserting is sleeping after processing a batch and before processing another, so other threads can access the table waiting for handler insert Delayed insert handler is waiting for new inserts after processing all batches update There is no information on what this status indicates States Related to Replication (in order of operation) Sending binlog event to slave Master is sending a binary log event to a slave Finished reading one binlog; switching to next binlog Master is opening the next binlog to send more events to a slave Has sent all binlog to slave; waiting for binlog to be updated Master is waiting for more updates after all updates have been sent to a slave waiting to finalize termination Master is waiting for the thread on the master that sends events to the slave to stop Waiting for master update Slave I/O is initializing Connecting to master Slave I/O thread is trying to connect to master Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark continued 705 21 Part IV Extending Your Skills TABLE 21-2 (continued ) Value General States Meaning Checking master version Slave I/O thread is checking the mysqld version of the master Registering slave on master Slave I/O thread is registering this slave server with the master Requesting binlog dump Slave I/O thread is requesting the contents of the binary logs Waiting to reconnect after a failed binlog dump request Slave I/O thread is sleeping after a binary log dump request failed The amount of sleep time is determined by the MASTER_CONNECT_RETRY option, which can be set in the CHANGE MASTER TO statement or at startup using the master-connect-retry option Reconnecting after a failed binlog dump request Slave I/O thread is reconnecting to the master after a binary log dump request failed Waiting for master to send event Slave I/O thread is waiting for a binary log event to be sent from the master The slave server has connected successfully to the master Queueing master event to the relay log Slave I/O thread is copying binlog event from master to the relay log Waiting to reconnect after a failed master event read Slave I/O thread is sleeping after an event read from the master failed The amount of sleep time is determined by the MASTER_CONNECT_RETRY option, which can be set in the CHANGE MASTER TO statement or at startup using the master-connect-retry option Reconnecting after a failed master event read Slave I/O thread is reconnecting to the master after an event read from the master failed Waiting for the SQL thread to free enough relay log space Slave I/O thread is waiting for the slave SQL thread to process events from the relay log The relay logs have filled due to exceeding the relay_log_space_limit value The default value is a value of 0, meaning unlimited Waiting for the next event in relay log Slave SQL thread is waiting for the first event to appear in the relay log after the slave SQL thread has been initialized Reading event from the relay log Slave SQL thread is reading an event from the relay log 706 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark The MySQL Data Dictionary TABLE 21-2 (continued ) Value General States Meaning Has read all relay log; waiting for the slave I/O thread to update it Slave SQL thread is waiting for a new event to appear in the relay log Making temp file Slave SQL thread is creating a temporary file to read in rows for a LOAD DATA INFILE statement Waiting for slave mutex on exit Slave I/O thread or SQL thread is stopping Changing master Applying a CHANGE MASTER statement Creating table from master dump Creating a table from a master dump, such as when executing LOAD DATA FROM MASTER or LOAD TABLE FROM MASTER Opening master dump table Opening a table to receive table contents from a master dump, such as when executing LOAD DATA FROM MASTER or LOAD TABLE FROM MASTER Reading master dump table data Receiving table data from a master dump, such as when executing LOAD DATA FROM MASTER or LOAD TABLE FROM MASTER Rebuilding the index on master dump table Rebuilding the index on a table after data from a table dump has been received, such as when executing LOAD DATA FROM MASTER or LOAD TABLE FROM MASTER starting slave Starting the slave I/O and slave SQL threads after a table dump has completed Killing slave Applying a SLAVE STOP or STOP SLAVE statement States Related to MySQL Cluster (in order of operation) Processing events Processing events to write to the binary log Committing events to binlog Writing events to the binary log Syncing ndb table schema operation and binlog Syncing the schema and binary log before replicating the schema Processing events from schema table Replicating the schema Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark continued 707 21 Part IV Extending Your Skills TABLE 21-2 (continued ) Value General States Meaning Shutting down Shutting down Waiting for event from ndbcluster An SQL node connected to a cluster management node is waiting for an event Waiting for first event from ndbcluster An SQL node connected to a cluster management node is waiting for the first event Waiting for ndbcluster binlog update to reach current position A node is waiting for the binary logs to become current Waiting for ndbcluster to start A node is waiting for MySQL Cluster to start Waiting for schema epoch Waiting for a global checkpoint for the schema Opening mysql.ndb_ apply_status A node is waiting for a system table to be opened States Related to the Event Scheduler (in order of operation) Initialized Event scheduler or event execution thread has been initialized Waiting on empty queue Waiting; event queue is empty Waiting for next activation Waiting; event queue has items but the next activation will happen later Waiting for scheduler to stop Waiting for the scheduler to stop; this occurs after a SET GLOBAL event_scheduler=OFF statement has been issued Clearing Event scheduler or event execution thread is ending Being able to query the PROCESSLIST system view is a large advantage over using the SHOW [FULL] PROCESSLIST statement With SHOW [FULL] PROCESSLIST, all of the fields are shown, with no filtering For example, the following query shows information about queries running longer than one minute: SELECT ID, USER, HOST, DB, COMMAND, TIME, STATE, INFO FROM PROCESSLIST WHERE TIME>60; 708 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark The MySQL Data Dictionary You can also find out how many processes a certain username is running, and from what hosts: SELECT ID, USER, HOST, DB, COMMAND, TIME, STATE, INFO FROM PROCESSLIST WHERE USER=some_user ORDER BY HOST; PROFILING As of MySQL version 6.0.5, query profiling can be done on a session-level basis By default, the profiling session variable is set to and the PROFILING system view has no rows If it is set to 1, the PROFILING system view populates the following fields: ■ QUERY_ID — The integer identifier of the query ■ SEQ — The sequence number, showing the display order for rows with the same QUERY_ID ■ STATE — The state the query was in See Table 21-2 for the list of possible values ■ DURATION — How long the query stayed in STATE ■ CPU_USER — Seconds of user CPU use during STATE ■ CPU_SYSTEM — Seconds of system CPU use during STATE ■ CONTEXT_VOLUNTARY — The number of voluntary context switches that occurred during STATE ■ CONTEXT_INVOLUNTARY — The number of involuntary context switches that occurred during STATE ■ BLOCK_OPS_IN — The number of block input operations during STATE ■ BLOCK_OPS_OUT — The number of block output operations during STATE ■ MESSAGES_SENT — The number of communication messages sent during STATE ■ MESSAGES_RECEIVED — The number of communication messages received during STATE ■ PAGE_FAULTS_MAJOR — The number of major page faults that occurred during STATE ■ PAGE_FAULTS_MINOR — The number of minor page faults that occurred during STATE ■ SWAPS — The number of swaps that occurred during STATE ■ SOURCE_FUNCTION — The function in the source code where STATE executed ■ SOURCE_FILE — The source code file where STATE executed ■ SOURCE_LINE — The line number in SOURCE_FILE where STATE executed The SHOW PROFILE statement output information contains some of the same fields as the PROFILING system view Just like the PROFILING system view, the SHOW PROFILE statement Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 709 21 Part IV Extending Your Skills outputs one row per query state However, the SHOW PROFILE statement only shows profiling information for one query The information in the SHOW PROFILE output is: ■ Status — Equivalent to STATE in the PROFILING system view ■ Duration — Equivalent to DURATION in the PROFILING system view See Chapter for more information on using SHOW PROFILE The SHOW PROFILES statement is very different from the SHOW PROFILE statement — it outputs profiling information for the most recent queries There is one query per row, and the maximum number of queries shown is determined by the profiling_history_size session variable This session variable also restricts the number of queries that are saved in the PROFILING system view The information in the SHOW PROFILES output is: ■ Query_ID — Equivalent to QUERY_ID in the PROFILING system view ■ Duration — The total amount of time spent on the query Equivalent to: SELECT SUM(DURATION) FROM PROFILING WHERE QUERY_ID=num; ■ Query — The text of the query that was executed There is no equivalent in the PROFILING system view See Chapter for more information on using SHOW PROFILES GLOBAL_VARIABLES The GLOBAL_VARIABLES system view contains information about the global server variables in mysqld The SHOW GLOBAL VARIABLES command contains the same information as the GLOBAL_VARIABLES system view, using similar field names: ■ VARIABLE_NAME — The name of the global system variable Variable_name in SHOW GLOBAL VARIABLES is equivalent ■ VARIABLE_VALUE — The value of the global system variable Value in SHOW GLOBAL VARIABLES is equivalent SESSION_VARIABLES The SESSION_VARIABLES system view contains information about the session server variables in mysqld The SHOW SESSION VARIABLES command contains the same information as the SESSION_VARIABLES system view, using similar field names: ■ VARIABLE_NAME — The name of the session system variable Variable_name in SHOW SESSION VARIABLES is equivalent ■ VARIABLE_VALUE – –The value of the session system variable Variable_name in SHOW SESSION VARIABLES is equivalent 710 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark The MySQL Data Dictionary GLOBAL_STATUS The GLOBAL_STATUS system view contains information about the global server status in mysqld The SHOW GLOBAL STATUS command contains the same information as the GLOBAL_STATUS system view, using similar field names: ■ VARIABLE_NAME — The name of the global status variable Variable_name in SHOW GLOBAL STATUS is equivalent ■ VARIABLE_VALUE — The value of the global status variable Value in SHOW GLOBAL STATUS is equivalent SESSION_STATUS The SESSION_STATUS system view contains information about the session server status in mysqld The SHOW SESSION STATUS command contains the same information as the SESSION_STATUS system view, using similar field names: ■ VARIABLE_NAME — The name of the session system variable Variable_name in SHOW SESSION STATUS is equivalent ■ VARIABLE_VALUE — The value of the session system variable Variable_name in SHOW SESSION STATUS is equivalent Displaying Permissions The preferred method of changing permissions is by using the GRANT and REVOKE statements However, viewing permissions can be difficult SHOW GRANTS can be used to see the grant statement for the current user, and SHOW GRANTS FOR user@host can be used to see the GRANT statements for user@host In order to run the SHOW GRANTS statement to see permissions for other users, you must know the user@host This may seem obvious, but it is a tricky problem to solve — for example, finding all the users who have permissions to write to a specific table Four INFORMATION_SCHEMA system views make retrieving permissions easier The COLUMN_PRIVILEGES and TABLE_PRIVILEGES are SQL standard system views that show permission information at the column and table levels, respectively The SCHEMA_PRIVILEGES and USER_PRIVILEGES system views are MySQL extensions to the data dictionary, which contain information about permissions at the database and global levels, respectively Unlike the mysql system tables, the INFORMATION_SCHEMA system views show the full user The mysql system tables contain separate fields for User and Host, and the INFORMATION_SCHEMA system views contain one field called GRANTEE, in the user@host format If a user has certain permissions for a database, those permissions will show up only on the database level — the SCHEMA_PRIVILEGES system view and the mysql.db system table Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 711 21 Part IV Extending Your Skills This means that there is no trickle down of permissions, thus to get all the users with permissions for a single column you need to write a query that retrieves: ■ The users with permissions on that column ■ The users with table-level permissions on the table containing the column ■ The users with database-level permissions on the database with the table containing the column ■ The users with global permissions on all databases The SHOW PRIVILEGES statement returns all of the permissions that mysqld supports There are three fields in the result of SHOW PRIVILEGES: ■ Privilege — The name of the permission Possible values are Alter, Alter routine, Create, Create routine, Create temporary tables, Create user, Create view, Delete, Drop, Event, Execute, File, Grant, Index, Insert, Lock tables, Process, References, Reload, Replication client, Replication slave, Select, Show databases, Show view, Shutdown, Super, Trigger, and Update ■ Context — A comma-separated list of contexts of the permission Possible contexts are Databases, File access on server, Functions, Indexes, Procedures, Server Admin, and Tables ■ Comment — A short description of the permission COLUMN_PRIVILEGES The COLUMN_PRIVILEGES system view contains the same information as the columns_priv system table in the mysql database The COLUMN_PRIVILEGES system view contains one row per privilege per user — this means there are separate rows for a SELECT and an INSERT on a column for a user In the columns_priv system table, there is one row per column per user, and a field with a SET data type to store all of the privileges for that user on that column The fields in the COLUMN_PRIVILEGES system view and their equivalences in the columns_priv system table are: ■ GRANTEE — The user for which this permission is granted, in user@host format The User and Host fields in mysql.columns_priv contain the same information ■ TABLE_CATALOG — Provided for standards compliance However, because MySQL does not have catalogs, this value is always NULL There is no equivalent in mysql.columns_priv ■ TABLE_SCHEMA — The name of the database, such as sakila The Db field in mysql.columns_priv is equivalent ■ TABLE_NAME — The name of the table containing the field associated with this permission The Table_name field in mysql.columns_priv is equivalent 712 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark The MySQL Data Dictionary ■ COLUMN_NAME — The name of the field associated with this permission The Column_name field in mysql.columns_priv is equivalent ■ PRIVILEGE_TYPE — The type of privilege Possible values are SELECT, INSERT, UPDATE, and REFERENCES The Column_priv field in mysql.columns_priv contains the same information However, the Column_priv field is a SET data type, and the COLUMN_PRIVILEGES system view has one row per privilege To get the equivalent of the Column_priv field, query the COLUMN_PRIVILEGES system view with: SELECT GROUP_CONCAT(PRIVILEGE_TYPE) FROM COLUMN_PRIVILEGES WHERE GRANTEE="’user’@’host’" AND TABLE_NAME=tblname AND COLUMN_NAME=fldname; ■ IS_GRANTABLE — Whether or not the user can grant this permission to another user If the user can grant this permission to another user, the value is YES; otherwise the value is NO There is no equivalent in mysql.columns_priv TABLE_PRIVILEGES The TABLE_PRIVILEGES system view contains the same information as the tables_priv system table in the mysql database The TABLE_PRIVILEGES system view contains one row per privilege per user — this means there are separate rows for a SELECT and an INSERT on a table for a user In the tables_priv system table, there is one row per table per user, and a field with a SET data type to store all of the privileges for that user on that table The fields in the TABLE_PRIVILEGES system view and their equivalences in the tables_priv system table are: ■ GRANTEE — The user for which this permission is granted, in user@host format The User and Host fields in mysql.tables_priv contain the same information ■ TABLE_CATALOG — Provided for standards compliance However, because MySQL does not have catalogs, this value is always NULL There is no equivalent in mysql.tables_priv ■ TABLE_SCHEMA — The name of the database, such as sakila The Db field in mysql.tables_priv is equivalent ■ TABLE_NAME — The name of the table associated with this permission The Table_name field in mysql.tables_priv is equivalent ■ PRIVILEGE_TYPE — The type of privilege Possible values are SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, GRANT, REFERENCES, INDEX, ALTER, CREATE VIEW, SHOW VIEW, and TRIGGER The Table_priv field in mysql.tables_priv contains the same information However, the Table_priv field is a SET data type, and the TABLE_PRIVILEGES system view has one row per privilege Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 713 21 Part IV Extending Your Skills To get the equivalent of the Table_priv field, query the TABLE_PRIVILEGES system view with: SELECT GROUP_CONCAT(PRIVILEGE_TYPE) FROM TABLE_PRIVILEGES WHERE GRANTEE="’user’@’host’" AND TABLE_NAME=tblname; ■ IS_GRANTABLE — Whether or not the user can grant this permission to another user If the user can grant this permission to another user, the value is YES; otherwise the value is NO There is no equivalent in mysql.tables_priv SCHEMA_PRIVILEGES The SCHEMA_PRIVILEGES system view contains the same information as the db system table in the mysql database The SCHEMA_PRIVILEGES system view contains one row per privilege per user — this means there are separate rows for SELECT and INSERT permissions on a database for a user In the db system table, there is one row per table per user, with many fields storing the permissions The fields in the SCHEMA_PRIVILEGES system view and their equivalences in the db system table are: ■ GRANTEE — The user for which this permission is granted, in user@host format The User and Host fields in mysql.db contain the same information ■ TABLE_CATALOG — Provided for standards compliance However, because MySQL does not have catalogs, this value is always NULL There is no equivalent in mysql.db ■ TABLE_SCHEMA — The name of the database associated with this permission, such as sakila The Db field in mysql.db is equivalent ■ PRIVILEGE_TYPE — The type of privilege Possible values are SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, GRANT, REFERENCES, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, EXECUTE, EVENT, and TRIGGER The mysql.db system table contains the same information However, the db system table contains one row for each table, with separate ENUM fields for each permission The ENUM fields have a value of Y or N, indicating whether or not the user has that permission The SCHEMA_PRIVILEGES system view has one row per privilege ■ To find out which users have SELECT permissions for the sakila database, the mysql.db table can be queried with: SELECT User,Host FROM mysql.db WHERE Select_priv=’Y’ AND Db=’sakila’; 714 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark The MySQL Data Dictionary ■ To get the same result from the SCHEMA_PRIVILEGES system view, the query is: SELECT GRANTEE FROM SCHEMA_PRIVILEGES WHERE PRIVILEGE_TYPE=’SELECT’ AND TABLE_SCHEMA=sakila’; ■ IS_GRANTABLE — Whether or not the user can grant this permission to another user If the user can grant this permission to another user, the value is YES; otherwise the value is NO There is no equivalent in mysql.tables_priv USER_PRIVILEGES The USER_PRIVILEGES system view contains the same information as the user system table in the mysql database The USER_PRIVILEGES system view contains one row per privilege per user — this means there are separate rows for the global SUPER and FILE permissions for a user In the user system table, there is one row per user, with many fields storing the permissions The fields in the USER_PRIVILEGES system view and their equivalences in the user system table are: ■ GRANTEE — The user for which this permission is granted, in user@host format The User and Host fields in mysql.db contain the same information ■ TABLE_CATALOG — Provided for standards compliance However, because MySQL does not have catalogs, this value is always NULL There is no equivalent in mysql.user ■ PRIVILEGE_TYPE — The type of privilege Possible values are all of the Privilege values except Grant from the output of SHOW PRIVILEGES There is no need for a GRANT privilege type because the USER_PRIVILEGES system view stores whether or not a user has grant permission in the IS_GRANTABLE field The mysql.user system table contains the same information However, the user system table contains one row for each user, with separate ENUM fields for each permission The ENUM fields have a value of Y or N, indicating whether or not the user has that permission The USER_PRIVILEGES system view has one row per privilege To find out which users have SUPER permissions, the mysql.user table can be queried with: SELECT User,Host FROM mysql.user WHERE Super_priv=’Y’; To get the same result from the USER_PRIVILEGES system view, the query is: SELECT GRANTEE FROM USER_PRIVILEGES WHERE PRIVILEGE_TYPE=’SUPER’; Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 715 21 Part IV Extending Your Skills ■ IS_GRANTABLE — Whether or not the user can grant this permission to another user If the user can grant this permission to another user, the value is YES; otherwise the value is NO There is no equivalent in mysql.user Storage Engine-Specific Metadata Storage engine authors are increasingly adding system views to the INFORMATION_SCHEMA database At the time of this writing, the only storage engine–specific system views that ship with mysqld are Falcon system views However, because Falcon is not yet complete at the time this book was written, the Falcon system views are being changed regularly Therefore, we will not review any of the storage-engine–specific system views here Custom Metadata The powerful plugin feature in MySQL allows server components to be loaded and unloaded without restarting mysqld Plugins can be thought of as shared libraries that can be dynamically loaded At the time of this writing, there is no support for plugins in MySQL on Windows Plugins are written in any language that can use C calling conventions, such as C or C++ Plugins can only be used on an operating system that supports dynamic loading with a version of mysqld that has been dynamically linked You will need to compile your plugin with a C++ compiler (such as g++, the GNU C/C++ compiler) and some of the MySQL header files To obtain the header files, download the source package from the official website at http://dev.mysql.com/downloads/mysql/6.0.html You can download either the rpm package or the tar archive If you are using the rpm package, install it; if you are using the tar archive, extract the files from it Defining the plugin For this example, we will use Roland Bouman’s sample plugin ‘‘Hello, Information Schema.’’ You can find the original article and code source online at http://www.oreillynet.com/ databases/blog/2008/02/mysql information schema plugi.html The includes you will need are follow; note that the numbers are not part of the code: #include #include #include #include #include #include #include 716 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark ... quoted: mysql> SHOW DATABASES WHERE Database NOT IN (? ?mysql? ??,’information_ schema’); ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server... server version for the right syntax to use near ’NOT IN (? ?mysql? ??,’information_schema’)’ at line mysql> SHOW DATABASES WHERE `Database` NOT IN (? ?mysql? ??,’information_ schema’); + + | Database | +... word, and does not need to be escaped: mysql> USE INFORMATION_SCHEMA; Database changed mysql> SELECT SCHEMA_NAME FROM SCHEMATA WHERE SCHEMA_NAME NOT IN (? ?mysql? ??,’information_schema’); + -+