MySQL Schema设计(四)一个MySQL里的JQuery:common_schema (先存着)

来源:http://blog.itpub.net/26515977/viewspace-1208257/

我们总要在一定的框架中活着,框架的构成有来自法律,有来自道德的,还有来自潜规则的。大部分人只求安生的活着,玩命的人毕竟是少数,有人打破框架平度青云,也有人打破框却架坠落深渊。每每跟做开发的同学沟通,就会听到一大滩框架名称,觉得很是高上大的样子。但他山之石可以攻玉,在MySQL当中也是有框架,这便是我们要介绍的common_schema。高性能MySQL一书作者 Baron Schwartz曾如是说:The common_schema is to MySQL as JQuery is to JavaScript。本节仅仅简单介绍Schema相关部分,毕竟common_schema实在太强悍太广博。

软件主页:code.google.com/p/common-schema

软件安装

[[email protected] ~]$ mysql -uroot -p < common_schema-2.2.sql
Enter password:
complete
- Base components: installed
- InnoDB Plugin components: installed
- Percona Server components: not installed
- TokuDB components: partial install: 1/2

Installation complete. Thank you for using common_schema!

软件信息

mysql> select attribute_name,substr(attribute_value,1,50) from metadata;
+-------------------------------------+----------------------------------------------------+
| attribute_name                      | substr(attribute_value,1,50)                       |
+-------------------------------------+----------------------------------------------------+
| author                              | Shlomi Noach                                       |
| author_url                          | http://code.openark.org/blog/shlomi-noach          |
| base_components_installed           | 1                                                  |
| innodb_plugin_components_installed  | 1                                                  |
| install_mysql_version               | 5.6.12-log                                         |
| install_sql_mode                    | NO_AUTO_VALUE_ON_ZERO,STRICT_TRANS_TABLES,NO_ENGIN |
| install_success                     | 1                                                  |
| install_time                        | 2014-02-05 21:53:55                                |
| license                             |

common_schema - DBA\‘s Framework for MySQL
Copyri |
| license_type                        | GPL                                                |
| percona_server_components_installed | 0                                                  |
| project_home                        | http://code.google.com/p/common-schema/            |
| project_name                        | common_schema                                      |
| project_repository                  | https://common-schema.googlecode.com/svn/trunk/    |
| project_repository_type             | svn                                                |
| revision                            | 523                                                |
| version                             | 2.2                                                |
+-------------------------------------+----------------------------------------------------+
17 rows in set (0.00 sec)

内建帮助系统

mysql> desc help_content;
+--------------+-------------+------+-----+---------+-------+
| Field        | Type        | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| topic        | varchar(32) | NO   | PRI | NULL    |       |
| help_message | text        | NO   |     | NULL    |       |
+--------------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> select topic from help_content;
+--------------------------------+
| topic                          |
+--------------------------------+
| auto_increment_columns         |
| candidate_keys                 |
| candidate_keys_recommended     |

mysql> select help_message from help_content where topic=\‘innodb_index_stats\‘\G;
*************************** 1. row ***************************
help_message:
NAME

innodb_index_stats: Estimated InnoDB depth & split factor of key\‘s B+ Tree

TYPE

View

DESCRIPTION

innodb_index_stats extends the INNODB_INDEX_STATS patch in Percona Server, and
presents with estimated depth & split factor of InnoDB keys.
Estimations are optimistic, in that they assume condensed trees. It is
possible that the depth is larger than estimated, and that split factor is
lower than estimated.
Estimated values are presented as floating point values, although in reality
these are integer types.
This view is experimental and in BETA stage.
This view depends upon the INNODB_INDEX_STATS patch in Percona Server.
Note that Percona Server 5.5.8-20.0 version introduced changes to the
INNODB_INDEX_STATS schema. This view is compatible with the new schema, and is
incompatible with older releases.
...............<此处省略输出>.............

那么数据源来自哪里?我们以redundant_keys为例追踪其源码:

  FROM
    _flattened_keys AS redundant_keys
    INNER JOIN _flattened_keys AS dominant_keys
    USING (TABLE_SCHEMA, TABLE_NAME)

再以 _flattened_keys 为基表查看:

  FROM INFORMATION_SCHEMA.STATISTICS

作者Shlomi Noach便是认为"INFORMATION_SCHEMAprovides with complete info, it is ofter difficult to aggregate. It is sometimes too normalized, and at other times too de-normalized",他的诞生和Perl有些类似,系统管理员沃尔曾想用awk来完成,但其并不能满足他的需求,结果就是一门新的编程语言要诞生了。

我把common_schema涉及到schema的归档一类,如下图所示:

接着我们对这5个分类展开大致介绍

1 Data Size per Schema

● data_size_per_schema

mysql> select * from data_size_per_schema where table_schema=\‘sakila\‘\G;
*************************** 1. row ***************************
      TABLE_SCHEMA: sakila
      count_tables: 16
       count_views: 7
  distinct_engines: 2
         data_size: 4297536
        index_size: 2581504
        total_size: 6879040
     largest_table: rental
largest_table_size: 2785280
1 row in set (0.16 sec)

2 Schema Object Analysis: Tables

DDL scripts
   ● sql_alter_table
   ● sql_foreign_keys

mysql> select table_name,sql_add_keys from sql_alter_table where table_schema=\‘sakila\‘\G;
*************************** 1. row ***************************
  table_name: actor
sql_add_keys: ADD KEY `idx_actor_last_name`(`last_name`), ADD KEY `idx_actor_last_name_duplicate`(`last_name`), ADD PRIMARY KEY (`actor_id`)
*************************** 2. row ***************************
  table_name: address
sql_add_keys: ADD KEY `idx_fk_city_id`(`city_id`), ADD PRIMARY KEY (`address_id`)
.................<此处省略输出>.................

mysql> select * from sql_foreign_keys where table_schema=\‘sakila\‘\G;
*************************** 1. row ***************************
    TABLE_SCHEMA: sakila
      TABLE_NAME: address
CONSTRAINT_NAME: fk_address_city
  drop_statement: ALTER TABLE `sakila`.`address` DROP FOREIGN KEY `fk_address_city`
create_statement: ALTER TABLE `sakila`.`address` ADD CONSTRAINT `fk_address_city` FOREIGN KEY (`city_id`) REFERENCES `sakila`.`city` (`city_id`) ON DELETE RESTRICT ON UPDATE CASCADE
........................<此处省略输出>.........................

3 Schema Object Analysis: Columns

Column overviews

● auto_increment_columns
   ● text_columns

mysql> select table_name,column_name,data_type,max_value,auto_increment value,auto_increment_ratio ratio
    ->   from auto_increment_columns
    ->  where table_schema=\‘sakila\‘;
+------------+--------------+-----------+------------+-------+--------+
| TABLE_NAME | COLUMN_NAME  | DATA_TYPE | max_value  | value | ratio  |
+------------+--------------+-----------+------------+-------+--------+
| actor      | actor_id     | smallint  |      65535 |   201 | 0.0031 |
| address    | address_id   | smallint  |      65535 |   606 | 0.0092 |
| category   | category_id  | tinyint   |        255 |    17 | 0.0667 |
| city       | city_id      | smallint  |      65535 |   601 | 0.0092 |
| country    | country_id   | smallint  |      65535 |   110 | 0.0017 |
| customer   | customer_id  | smallint  |      65535 |   600 | 0.0092 |
| film       | film_id      | smallint  |      65535 |  1001 | 0.0153 |
| inventory  | inventory_id | mediumint |   16777215 |  4582 | 0.0003 |
| language   | language_id  | tinyint   |        255 |     7 | 0.0275 |
| payment    | payment_id   | smallint  |      65535 | 16050 | 0.2449 |
| rental     | rental_id    | int       | 2147483647 | 16050 | 0.0000 |
| staff      | staff_id     | tinyint   |        255 |     3 | 0.0118 |
| store      | store_id     | tinyint   |        255 |     3 | 0.0118 |
+------------+--------------+-----------+------------+-------+--------+
13 rows in set (0.90 sec)

4 Schema Object Analysis: Indexes

Keys and Indexes
   ● candidate_keys
   ● candidate_keys_recommended
   ● no_pk_innodb_tables
   ● rendundant_keys

mysql> select * from candidate_keys_recommended where table_schema=\‘sakila\‘;
+--------------+---------------+------------------------+--------------+------------+-----------------------+---------------------+
| table_schema | table_name    | recommended_index_name | has_nullable | is_primary | count_column_in_index | column_names        |
+--------------+---------------+------------------------+--------------+------------+-----------------------+---------------------+
| sakila       | language      | PRIMARY                |            0 |          1 |                     1 | language_id         |
| sakila       | customer      | PRIMARY                |            0 |          1 |                     1 | customer_id         |
| sakila       | film_category | PRIMARY                |            0 |          1 |                     2 | film_id,category_id |
| sakila       | category      | PRIMARY                |            0 |          1 |                     1 | category_id         |
| sakila       | rental        | PRIMARY                |            0 |          1 |                     1 | rental_id           |
| sakila       | film_actor    | PRIMARY                |            0 |          1 |                     2 | actor_id,film_id    |
| sakila       | inventory     | PRIMARY                |            0 |          1 |                     1 | inventory_id        |
| sakila       | country       | PRIMARY                |            0 |          1 |                     1 | country_id          |
| sakila       | store         | PRIMARY                |            0 |          1 |                     1 | store_id            |
| sakila       | address       | PRIMARY                |            0 |          1 |                     1 | address_id          |
| sakila       | payment       | PRIMARY                |            0 |          1 |                     1 | payment_id          |
| sakila       | film          | PRIMARY                |            0 |          1 |                     1 | film_id             |
| sakila       | film_text     | PRIMARY                |            0 |          1 |                     1 | film_id             |
| sakila       | city          | PRIMARY                |            0 |          1 |                     1 | city_id             |
| sakila       | staff         | PRIMARY                |            0 |          1 |                     1 | staff_id            |
| sakila       | actor         | PRIMARY                |            0 |          1 |                     1 | actor_id            |
+--------------+---------------+------------------------+--------------+------------+-----------------------+---------------------+
16 rows in set (0.39 sec)

5 Schema Object Analysis: Dependencies

Dependency Routines
   ● get_event_dependencies(schema, name)
   ● get_routine_dependencies(schema, name)
   ● get_view_dependencies(schema, name)
   ● get_sql_dependencies(sql, schema)

mysql> call get_view_dependencies(\‘sakila\‘,\‘actor_info\‘);
+-------------+---------------+-------------+--------+
| schema_name | object_name   | object_type | action |
+-------------+---------------+-------------+--------+
| sakila      | actor         | table       | select |
| sakila      | category      | table       | select |
| sakila      | film          | table       | select |
| sakila      | film_actor    | table       | select |
| sakila      | film_category | table       | select |
+-------------+---------------+-------------+--------+
5 rows in set (0.32 sec)

Query OK, 0 rows affected (0.32 sec)

以上都是common_schema分内之事,以下再介绍2种创建Schema的方法,这对common_schema而言,也是小菜一碟。

1 eval()

具体用法,可查看帮助:

mysql> call help(\‘eval\‘);
+--------------------------------------------------------------------------------+
| help                                                                           |
+--------------------------------------------------------------------------------+
|                                                                                |
| NAME                                                                           |
|                                                                                |
| eval(): Evaluates the queries generated by a given query.                      |
|                                                                                |
| TYPE                                                                           |
..............<此处省略输出>...............

方法演示:

mysql> call eval(\‘select concat(\\‘create table test.\\‘, table_name,\\‘ as select * from sakila.\\‘, table_name)
    \‘>   from information_schema.tables
    \‘>   where table_schema = \\‘sakila\\‘\‘);
Query OK, 0 rows affected (11.30 sec)

mysql> show tables in test;
+----------------------------+
| Tables_in_test             |
+----------------------------+
| actor                      |
| actor_info                 |
| address                    |
......  <此处省略输出>.......
| staff_list                 |
| store                      |
+----------------------------+
23 rows in set (0.00 sec)

mysql> call eval(\‘select concat(\\‘drop table test.\\‘, table_name) from information_schema.tables
    \‘> where table_schema = \\‘test\\‘\‘);
Query OK, 0 rows affected (0.92 sec)

mysql> show tables in test;
Empty set (0.00 sec)

2 foreach

同理,查看foreach帮助:

mysql> call help(\‘foreach\‘);
+--------------------------------------------------------------------------------+
| help                                                                           |
+--------------------------------------------------------------------------------+
|                                                                                |
| NAME                                                                           |
|                                                                                |
| foreach(): Invoke a script on each element of given collection. $() is a       |
| synonym of this routine.                                                       |
|                                                                                |
| TYPE                                                                           |
|                                                                                |
| Procedure                                                                      |
|                                                                                |
| DESCRIPTION                                                                    |
|                                                                                |
| This procedure accepts collections of varying types, including result sets,    |
| and invokes a QueryScript code per element.                                    |
...............<此处省略N个输出>.................

具体演示过程:

mysql> call $(\‘1:3\‘, \‘create table test.${1}(id int,name varchar(20))\‘);
Query OK, 0 rows affected, 1 warning (0.59 sec)

mysql> show tables in test;
+----------------+
| Tables_in_test |
+----------------+
| 1              |
| 2              |
| 3              |
+----------------+
3 rows in set (0.00 sec)

mysql> call $(\‘1:3\‘, \‘drop table test.`${1}`\‘);
Query OK, 0 rows affected, 1 warning (0.40 sec)

mysql> show tables in test;
Empty set (0.00 sec)

By DataHacker

2014-2-7

Good Luck!

时间: 2024-10-13 01:24:19

MySQL Schema设计(四)一个MySQL里的JQuery:common_schema (先存着)的相关文章

mysql schema设计中应避免的陷阱

谨记红字: 1. 表中谨防太多列: MySQL 的存储引擎API 工作时需要在服务器层和存储引擎层之间通过行缓冲格式拷贝数据,然后在服务器层将缓冲内容解码成各个列.从行缓冲中将编码过的列转换成行数据结构的操作代价是非常高的.MyISAM 的定长行结构实际上与服务器层的行结构正好匹配,所以不需要转换.然而,MyISAM 的变长行结构和InnoDB 的行结构则总是需要转换.转换的代价依赖于列的数量.当我们研究一个CPU 占用非常高的案例时,发现客户使用了非常宽的表(数千个字段),然而只有一小部分列会

MySQL Schema设计(三)利用Python操作Schema

来源:http://blog.itpub.net/26515977/viewspace-1208256/ 本文中 SQLAlchemy部分可参考http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0014320114981139589ac5f02944601ae22834e9c521415000 弓在箭要射出之前,低声对箭说道,“你的自由是我的”.Schema如箭,弓似Python,

《深入浅出Mysql》——第四章 Mysql中的运算符

“<=>”安全的等于运算符,和“=”类似,在操作数相等时值为 1,不同之处在于即使 操作的值为 NULL 也可以正确比较. 原文地址:https://www.cnblogs.com/JasonPeng1/p/12234403.html

MySQL之schema设计优化

良好的逻辑设计和物理设计是高性能的基石,应该根据系统要执行的查询语句来设计 schema.这往往需要权衡各种因素. 例如:反范式的设计可以加快某些类型的查询,但同时可能使另一些类型的查询变慢.比如添加计数表和汇总表是一种很好的优化查询的方式, 但是这些表的维护成本会很高.MySQL独有的特性和实现细节对性能影响也很大. 选择优化的数据类型的简单原则: 1.更小的通常更好 一般情况下,应该尽量使用可以正确存储数据的最小数据类型. 2.简单就好 简单数据类型的操作通常需要更少的cpu周期. 3.尽量

MySQL性能调优与架构设计——第9章 MySQL数据库Schema设计的性能优化

MySQL性能调优与架构设计——第9章 MySQL数据库Schema设计的性能优化 前言: 很多人都认为性能是在通过编写代码(程序代码或者是数据库代码)的过程中优化出来的,其实这是一个非常大的误区.真正影响性能最大的部分是在设计中就已经产生了的,后期的优化很多时候所能够带来的改善都只是在解决前妻设计所遗留下来的一些问题而已,而且能够解决的问题通常也比较有限.本章将就如何在 MySQL 数据库 Schema 设计的时候保证尽可能的高效,尽可能减少后期的烦恼. 9.1 高效的模型设计 最规范的就一定

一步一步跟我学模型设计之入门级:设计一个mysql模型

欢迎试用Linker Networks(www.linkernetworks.com)的领科云,一个基于Mesos Docker的混合云平台. http://marketplace.linkernetworks.com/ 在本章节,我们在设计模型的时候,并不考虑任何的应用关系,从一个原子的应用开始. 我们就设计一个最基本的mysql模型. 为了简单期间,我们暂且不考虑从dockerfile编译成Docker镜像的过程,而是从一个已经存在的镜像开始. 第一步:在Docker Hub上查找一个Doc

MySQL性能调优与架构设计——第10章 MySQL数据库Schema设计的性能优化

第10章 MySQL Server性能优化 前言: 本章主要通过针对MySQL Server(mysqld)相关实现机制的分析,得到一些相应的优化建议.主要涉及MySQL的安装以及相关参数设置的优化,但不包括mysqld之外的比如存储引擎相关的参数优化,存储引擎的相关参数设置建议将主要在下一章“常用存储引擎的优化”中进行说明. 10.1 MySQL 安装优化 选择合适的发行版本 1. 二进制发行版(包括RPM等包装好的特定二进制版本) 由于MySQL开源的特性,不仅仅MySQL AB提供了多个平

说一个MySQL里可能90%的程序员都会遇到的坑

说一个MySQL里可能90%的程序员都会遇到的坑最近我遇到了一个bug,我试着通过Rails在以"utf8"编码的MariaDB中保存一个UTF-8字符串,然后出现了一个离奇的错误:Incorrect string value: '\xF0\x9F\x98\x83 <-' for column 'summary' at row 1 我用的是UTF-8编码的客户端,服务器也是UTF-8编码的,数据库也是,就连要保存的这个字符串" 原文地址:https://blog.51c

mysql数据库设计开发规范

1.设计 1. 一般都使用INNODB存储引擎,除非读写比率<1%,才考虑使用MYISAM存储引擎:其他存储引擎请在DBA的建议下使用. 2. Stored procedure (包括存储过程,函数,触发器)对于MYSQL来说还不是很成熟,没有完善的出错记录处理,不建议使用. 3. UUID(),USER()这样的MYSQL INSIDE函数对于复制来说是很危险的,会导致主备数据.不一致.所以请不要使用.如果一定要使用UUID作为主键,让应用程序来产生. 4. 请不要使用外键约束,如果数据存在外