您好,欢迎访问一九零五行业门户网

MySQL优化之数据库结构:数据对象优化_MySQL

bitscn.com
使用procedure analyse函数优化表的数据类型
表需要使用何种数据类型,是需要根据应用来判断的。在mysql中,可以使用函数procedure analyse()对当前应用的表进行分析,该函数可以对数据库中列的数据类型提出优化建议,用户可以根据应用的实际情况斟酌考虑是否实施优化。
以下是函数procedure analyse()的使用方法:
select * from tbl_name procedure analyse(); select * from tbl_name procedure analyse(16,256); 输出的每一列信息都会对数据表中的列的数据类型提出优化建议。以上第二个语句告诉procedure analyse()不要为那些包含的值多于16个或者256字节的enum类型提出建议。如果没有这样的限制,输出信息可能很长;enum定义通常很难阅读。
根据procedure analyse()函数的输出信息,用户可能会发现,一些表中的字段可以修改为更加高效的数据类型。如果决定改变某个字段的类型,则需要使用alter table语句。
mysql> desc statistics_news_category_history;+---------------+-------------+------+-----+---------+----------------+| field | type | null | key | default | extra |+---------------+-------------+------+-----+---------+----------------+| id | int(10) | no | pri | null | auto_increment | | news_category | varchar(20) | yes | | null | | | news_num | int(10) | yes | | null | | | news_date | varchar(10) | yes | | null | | +---------------+-------------+------+-----+---------+----------------+4 rows in set (0.00 sec)
mysql> select * from statistics_news_category_history procedure analyse(9,256)/g;*************************** 1. row *************************** field_name: datacrawldb.statistics_news_category_history.id min_value: 26 max_value: 30 min_length: 2 max_length: 2 empties_or_zeros: 0 nulls: 0avg_value_or_avg_length: 28.0000 std: 1.4142 optimal_fieldtype: tinyint(2) unsigned not null*************************** 2. row *************************** field_name: datacrawldb.statistics_news_category_history.news_category min_value: bigdata max_value: test min_length: 4 max_length: 10 empties_or_zeros: 0 nulls: 0avg_value_or_avg_length: 6.6000 std: null optimal_fieldtype: enum('bigdata','cloud','datacenter','storage','test') not null*************************** 3. row *************************** field_name: datacrawldb.statistics_news_category_history.news_num min_value: 1 max_value: 33 min_length: 1 max_length: 2 empties_or_zeros: 0 nulls: 0avg_value_or_avg_length: 19.4000 std: 11.9766 optimal_fieldtype: tinyint(2) unsigned not null*************************** 4. row *************************** field_name: datacrawldb.statistics_news_category_history.news_date min_value: 2014-02-17 max_value: 2014-02-17 min_length: 10 max_length: 10 empties_or_zeros: 0 nulls: 0avg_value_or_avg_length: 10.0000 std: null optimal_fieldtype: enum('2014-02-17') not null4 rows in set (0.00 sec)
优化小技巧1) 使用正确合适的类型,不要将数字存储为字符串。
2) 尽可能地使用最有效(最小)的数据类型。mysql有很多节省磁盘空间和内存的专业化类型。
3) 尽可能使用较小的整数类型使表更小。例如,mediumint经常比int好一些,因为mediumint列使用的空间要少25%。
4) 如果可能,声明列为not null。它使任何事情更快而且每列可以节省一位。注意如果在应用程序中确实需要null,应该毫无疑问使用它,只是避免默认地在所有列上有它。
5) 对于myisam表,如果没有任何变长列(varchar、text或blob列),使用固定尺寸的记录格式。这比较快但是不幸地可能会浪费一些空间。即使你已经用create选项让 varchar列row_format=fixed,也可以提示想使用固定长度的行。
6) 使用sample character set,例如latin1。尽量少使用utf-8,因为utf-8占用的空间是latin1的3倍。可以在不需要使用utf-8的字段上面使用latin1,例如mail,url等。
bitscn.com
其它类似信息

推荐信息