MySQL 5.7 SYS System schema

Source: Internet
Author: User
Tags mysql in prepare

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Before explaining the system database, let's look at the evolution of MySQL in data dictionaries:
MySQL4.1 provides a information_schema data dictionary. It is easy to use SQL statements to retrieve the required system metadata from this point.
MYSQL5.5 provides a performance_schema performance dictionary. But the dictionary is more professional, the average person may also look at the dead.
MySQL5.7 provides the SYS system database. SYS database contains a series of stored procedures, custom functions, and views to help us quickly understand the system's meta-data information.


The SYS system database combines INFORMATION_SCHEMA and performance_schema data to make it easier to retrieve metadata. Now, I'll show you how to use it quickly in the next few scenarios.


First,
For example, before you want to know whether a table exists or not, there are two ways to do this:

A, pessimistic method, write SQL from INFORMATION_SCHEMA to take information:

[SQL]View PlainCopy
  1. Mysql> SELECT IF (COUNT (*) = 0,' not exists! ',' exists! ') as ' result ' from information_schema.tables WHERE table_schema = ' new_feature ' and table_name = ' T1 ';
  2. +-------------+  
  3. | Result |
  4. +-------------+  
  5. |  Not exists! |
  6. +-------------+  
  7. 1 row in Set (0.00 sec)



B, the optimistic approach, assuming that the table exists, write a stored procedure:
[SQL]View PlainCopy
  1. DELIMITER $$
  2. Use ' New_feature ' $$
  3. DROP PROCEDURE IF EXISTS ' sp_table_exists ' $$
  4. CREATE definer= ' root ' @ ' localhost ' PROCEDURE ' sp_table_exists ' (
  5. in Db_name VARCHAR (+),
  6. in Tb_name VARCHAR (+),
  7. out is_exists VARCHAR
  8. )
  9. BEGIN
  10. DECLARE no_such_table CONDITION for 1146;
  11. DECLARE EXIT HANDLER for no_such_table
  12. BEGIN
  13. SET is_exists = ' not exists! ';
  14. END;
  15. SET @stmt = CONCAT (' Select 1 from ', db_name,'. ', tb_name);
  16. PREPARE s1 from @stmt;
  17. EXECUTE S1;
  18. deallocate PREPARE s1;
  19. SET is_exists = ' exists! ';
  20. end$$
  21. DELIMITER;




Now to call:
[SQL]View PlainCopy
    1. Mysql> Call Sp_table_exists (' new_feature ',' t1 ', @result);
    2. Query OK, 0 rows Affected (0.00 sec)
    3. mysql> Select @result;
    4. +-------------+  
    5. | @result |
    6. +-------------+  
    7. |  Not exists! |
    8. +-------------+  
    9. 1 row in Set (0.00 sec)




Now we call directly with the existing stored procedure in the SYS database,
[SQL]View PlainCopy
  1. Mysql> Call Table_exists (' new_feature ',' t1 ', @v_is_exists);
  2. Query OK, 0 rows Affected (0.00 sec)
  3. Mysql> SELECT IF (@v_is_exists = ',' not exists! ', @v_is_exists) as ' result ';
  4. +-------------+  
  5. | Result |
  6. +-------------+  
  7. |  Not exists! |
  8. +-------------+  
  9. 1 row in Set (0.00 sec)




second, get an index that has not been used.


[SQL]View PlainCopy
  1. Mysql> SELECT * from schema_unused_indexes;
  2. +---------------+-------------+--------------+  
  3. | Object_schema | object_name | index_name |
  4. +---------------+-------------+--------------+  
  5. | New_feature | T1 | Idx_log_time |
  6. | New_feature | T1 | Idx_rank2 |
  7. +---------------+-------------+--------------+  
  8. 2 rows in Set (0.00 sec)




third, retrieve the table scan information below the specified database and filter out queries that are greater than 10 execution times.
[SQL]View PlainCopy
  1. Mysql> SELECT * from statement_analysis WHERE db=' new_feature ' and full_scan = ' * ' and exe C_count > 10\g
  2. 1. Row ***************************
  3. Query:show STATUS
  4. Db:new_feature
  5. Full_scan: *
  6. Exec_count:26
  7. err_count:0
  8. warn_count:0
  9. total_latency:74.68 ms
  10. max_latency:3.86 ms
  11. avg_latency:2.87 ms
  12. lock_latency:4.50 ms
  13. rows_sent:9594
  14. rows_sent_avg:369
  15. rows_examined:9594
  16. rows_examined_avg:369
  17. rows_affected:0
  18. rows_affected_avg:0
  19. tmp_tables:0
  20. tmp_disk_tables:0
  21. rows_sorted:0
  22. sort_merge_passes:0
  23. digest:475fa3ad9d4a846cfa96441050fc9787
  24. First_seen:2015-11-16 10:51:17
  25. Last_seen:2015-11-16 11:28:13
  26. 2. Row ***************************
  27. Query: SELECT ' state ', ' round ' ( SUM ... uration (summed) in sec ' DESC
  28. Db:new_feature
  29. Full_scan: *
  30. Exec_count:12
  31. err_count:0
  32. Warn_count:12
  33. total_latency:16.43 ms
  34. max_latency:2.39 ms
  35. avg_latency:1.37 ms
  36. lock_latency:3.54 ms
  37. rows_sent:140
  38. Rows_sent_avg:12
  39. rows_examined:852
  40. rows_examined_avg:71
  41. rows_affected:0
  42. rows_affected_avg:0
  43. Tmp_tables:24
  44. tmp_disk_tables:0
  45. rows_sorted:140
  46. sort_merge_passes:0
  47. digest:538e506ee0075e040b076f810ccb5f5c
  48. First_seen:2015-11-16 10:51:17
  49. Last_seen:2015-11-16 11:28:13
  50. 2 rows in Set (0.01 sec)






Finally, the same continues above, filtering out the query with the temporary table,


[SQL]View PlainCopy
  1. Mysql> SELECT * from statement_analysis WHERE db=' new_feature ' and tmp_tables > 0 ORDER by tmp_tables DESC LIMIT 1\g
  2. 1. Row ***************************
  3. Query: SELECT ' Performance_schema '. ... name '. ' sum_timer_wait ' DESC
  4. Db:new_feature
  5. Full_scan: *
  6. Exec_count:2
  7. err_count:0
  8. warn_count:0
  9. total_latency:87.96 ms
  10. max_latency:59.50 ms
  11. avg_latency:43.98 ms
  12. lock_latency:548.00 US
  13. Rows_sent:101
  14. Rows_sent_avg:51
  15. rows_examined:201
  16. Rows_examined_avg:101
  17. rows_affected:0
  18. rows_affected_avg:0
  19. tmp_tables:332
  20. Tmp_disk_tables:15
  21. rows_sorted:0
  22. sort_merge_passes:0
  23. digest:ff9bdfb7cf3f44b2da4c52dcde7a7352
  24. First_seen:2015-11-16 10:24:42
  25. Last_seen:2015-11-16 10:24:42
  26. 1 row in Set (0.01 sec)




Can see the above query detailed detailed, no longer do not perform the show status manually to filter.




Five, retrieve the top five executions of the statement,
[SQL]View PlainCopy
  1. Mysql> SELECT statement,total from user_summary_by_statement_type WHERE 'user ' =' root '   ORDER by Total DESC LIMIT 5;
  2. +-------------------+-------+  
  3. | Statement | Total |
  4. +-------------------+-------+  
  5. | Jump_if_not | 17635 |
  6. |  Freturn | 3120 |
  7. |   show_create_table | 289 |
  8. |   Field List | 202 |
  9. |   set_option | 190 |
  10. +-------------------+-------+  
  11. 5 rows in Set (0.01 sec)






Example I wrote so many, detailed to see the user's manual and find out for yourself. http://blog.csdn.net/yueliangdao0608/article/details/50032851

MySQL 5.7 SYS System schema

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.