[High.Performance.MySQL(3rd,2012.3)].Baron.Schwartz.之数据类型优化

本文介绍了MySQL中数据类型的优化选择,强调了选择最小且最简单数据类型的重要性,并建议避免使用NULL。讨论了不同类型的整数、浮点数、字符串、日期时间及位数据类型的特点,以及如何根据需求进行合理选择。还提到了数据表设计中的注意事项,如避免过多列和JOIN操作,以及快速ALTER TABLE的方法。
摘要由CSDN通过智能技术生成

选择合适的数据类型

越小越好

选择能够存储和表示数据的最小的数据类型。

越简单越好

处理简单的数据类型耗费越少的CPU cycles。
在MySQL中应该使用内置的数据类型保存datestimes.
应该使用整数保存IP地址。

如果可能,避免使用NULL

对于MySQL来说,可NULL的列很难优化。因为nullable column占用更多的存储空间,在MySQL中需要特殊处理。

Whole Numbers

整数分为TINYINTSAMLLINTMEDIUMINTINTBIGINT
对应8,16,24,32,64位的存储空间。
整形可以带UNSIGNED属性。

Real Numbers

FLOATDOUBLE类型支持近似计算,计算速度快。
DECIMAL支持精确计算。
存储同样类型的值浮点类型耗费空间比DECIMAL类型少。
一个FLOAT占用4个字节,DOUBLE占用8个字节。

DECIMAL(18, 9) will store nine digits from each side of the decimal point, using nine bytes in total: four for the digits before the decimal point, one for the decimal point itself, and four for the digits after the decimal point.

String Types

VARCHAR and CHAR Types

VARCHAR 是可变长度字符串,仅占用所需的空间。
VARCHAR 使用1或2个字节来保存字符串长度:1个字节最大长度胃255,更长的长度
使用2个字节保存。
VARCHAR在保存和查询字符串时会保存空格。

CHAR

CHAR是固定长度:MySQL为特定数量的字符串分配固定长度空间。
CHAR在保存和查询是会去掉尾部的空格。

mysql> CREATE TABLE char_test( char_col CHAR(10));
mysql> INSERT INTO char_test(char_col) VALUES
-> ('string1'), (' string2'), ('string3 ');

检索时:

mysql> SELECT CONCAT("'", char_col, "'") FROM char_test;
+----------------------------+
| CONCAT("'", char_col, "'") |
+----------------------------+
| 'string1'                  |
| ' string2'                 |
| 'string3'                  |
+----------------------------+

如果用VARCHAR保存

mysql> SELECT CONCAT("'", varchar_col, "'") FROM varchar_test;

+-------------------------------+
| CONCAT("'", varchar_col, "'") |
+-------------------------------+
| 'string1'                     |
| ' string2'                    |
| 'string3 '                    |
+-------------------------------+

BLOB and TEXT Types

the character types are TINYTEXT, SMALLTEXT, TEXT, MEDIUMTEXT, and LONGTEXT, and the binary types are TINYBLOB, SMALLBLOB,
BLOB, MEDIUMBLOB, and LONGBLOB. BLOB is a synonym for SMALLBLOB, and TEXT is a synonym
for SMALLTEXT.

MySQL sorts BLOB and TEXT columns differently from other types: instead of sorting the full length of the string, it sorts only the first max_sort_length bytes of such columns. If you need to sort by only the first few characters, you can either decrease the max_sort_length server variable or use ORDER BY SUBSTRING(column, length).
MySQL在排序BLOB and TEXT列时,根据该列的前max_sort_length字节的字符进行排序。自定义排序时可以使用ORDER BY SUBSTRING(column, length)方法。

Date and Time Types

DATETIME

This type can hold a large range of values, from the year 1001 to the year 9999,
with a precision of one second. It stores the date and time packed into an integer in YYYYMMDDHHMMSS format, independent of time zone. This uses eight bytes of storage space.
By default, MySQL displays DATETIME values in a sortable, unambiguous format, such as 2008-01-16 22:37:08. This is the ANSI standard way to represent dates and times.
DATETIME保存的时间格式为 2008-01-16 22:37:08

TIMESTAMP

the TIMESTAMP type stores the number of seconds elapsed since
midnight, January 1, 1970, Greenwich Mean Time (GMT)—the same as a Unix timestamp. TIMESTAMP uses only four bytes of storage, so it has a much smaller range than DATETIME
TIMESTAMP保存的是从midnight, January 1, 1970, Greenwich Mean Time (GMT)时刻开始到现在的秒数。

IIMESTAMP also has special properties that DATETIME doesn’t have. By default, MySQL will set the first TIMESTAMP column to the current time when you insert a row without specifying a value for the column.
TIMESTAMP在插入时如果没有制定值,第一个TIMESTAMP会设置为当前时间。

Bit-Packed Data Types

BIT

在MySQL5.0之前,BITTINYINT是一样的,但是在MySQL5.0和往后的版本中,这是一个完全不同的数据类型,具有特殊的字符。

你可以使用一个BIT来在一个列中存储多个true/falseBIT(1)定义一个域包含单个位,BIT(2)存储2个位,一个BIT最多可以存储64位。

BIT在InnoDB引擎中,存储每一个BIT列时按照能够包含这些位的最小整数来存储。
MySQL把BIT当做一个string类型而不是数字类型。当你检索BIT值的时候,得到的是一个string,内容是二进制的0或1的,不是ASCII的0或1。
举例:当你把值b’00111001’(57的二进制形式)存入BIT(8),然后检索,你会得到字符码57刚好是9的ASCII码值。但是在数学上下文中,你会得到数值57.

mysql> CREATE TABLE bittest(a bit(8));
mysql> INSERT INTO bittest VALUES(b'00111001');
mysql> SELECT a, a + 0 FROM bittest;

+------+-------+
| a    | a + 0 |
+------+-------+
| 9    |   57  |
+------+-------+

Schema Design Gotchas in MySQL

1 太多列
2 太多join
. 能使用TINYINT的地方使用ENUM
使用ENUM的地方使用SET

You can configure MySQL’s
SQL_MODE to disallow nonsense dates, which is an especially good practice for a new
application that hasn’t yet created a database full of bad data.

Pros and Cons of a Denormalized Schema

当使用join但是数据不能完全装进内存的时候,不适用join比使用join要更快。
单个表可以使用更加高效的index策略。

Cache and Summary Tables

查找前24小时内用户提交的评论数量。

mysql>SELECT SUM(cnt) FROM msg_per_hr
->WHERE hr BETWEEN
->    CONCAT(LEFT(NOW(), 14), '00:00') - INTERVAL 23 HOUR
->    AND CONCAT(LEFT(NOW(), 14), '00:00') - INTERVAL 1 HOUR;

mysql>SELECT COUNT(*) FROM message
->WHERE posted >= NOW() - INTERVAL 24 HOUR
->    AND posted < CONCAT(LEFT(NOW(), 14), '00:00') - INTERVAL 23 HOUR;

mysql>SELECT COUNT(*) FROM message
->    WHERE posted >= CONCAT(LEFT(NOW(), 14), '00:00');

替换数据库方法

mysql> DROP TABLE IF EXISTS my_summary_new, my_summary_old;
mysql> CREATE TABLE my_summary_new LIKE my_summary;
-- populate my_summary_new as desired
mysql> RENAME TABLE my_summary TO my_summary_old, my_summary_new TO my_summary;

快速ALTER TABLE

快速修改列的默认值

普通修改表列的默认值的命令是:

mysql> ALTER TABLE sakila.film
-> MODIFY COLUMN rental_duration TINYINT(3) NOT NULL DEFAULT 5;

这个命令会创建一个新的表格然后将数据拷贝过去。其实列的默认值保存在表格的.frm文件中,
所以你可以在不改变表的情况下改变默认值:

mysql> ALTER TABLE sakila.film
-> ALTER COLUMN rental_duration SET DEFAULT 5;

Building MyISAM Indexes Quickly

mysql> ALTER TABLE test.load_data DISABLE KEYS;
-- load the data
mysql> ALTER TABLE test.load_data ENABLE KEYS;

只针对nonunique Indexes有效。

In modern versions of InnoDB, you can use an analogous technique that relies onInnoDB’s fast online index creation capabilities. This calls for dropping all of the non-unique indexes, adding the new column, and then adding back the indexes you drop-ped.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
We had several goals in mind for this book. Many of them were derived from think- ing about that mythical perfect MySQL book that none of us had read but that we kept looking for on bookstore shelves. Others came from a lot of experience helping other users put MySQL to work in their environments. We wanted a book that wasn’t just a SQL primer. We wanted a book with a title that didn’t start or end in some arbitrary time frame (“...in Thirty Days,” “Seven Days To a Better...”) and didn’t talk down to the reader. Most of all, we wanted a book that would help you take your skills to the next level and build fast, reliable systems with MySQL—one that would answer questions like “How can I set up a cluster of MySQL servers capable of handling millions upon millions of queries and ensure that things keep running even if a couple of the servers die?” We decided to write a book that focused not just on the needs of the MySQL appli- cation developer but also on the rigorous demands of the MySQL administrator, who needs to keep the system up and running no matter what the programmers or users may throw at the server. Having said that, we assume that you are already rela- tively experienced with MySQL and, ideally, have read an introductory book on it. We also assume some experience with general system administration, networking, and Unix-like operating systems. This revised and expanded second edition includes deeper coverage of all the topics in the first edition and many new topics as well. This is partly a response to the changes that have taken place since the book was first published: MySQL is a much larger and more complex piece of software now. Just as importantly, its popularity has exploded. The MySQL community has grown much larger, and big corporations are now adopting MySQL for their mission-critical applications. Since the first edi- tion, MySQL has become recognized as ready for the enterprise.* People are also * We think this phrase is mostly marketing fluff, but it seems to convey a sense of importance to a lot of people. using it more and more in applications that are exposed to the Internet, where down- time and other problems cannot be concealed or tolerated. As a result, this second edition has a slightly different focus than the first edition. We emphasize reliability and correctness just as much as performance, in part because we have used MySQL ourselves for applications where significant amounts of money are riding on the database server. We also have deep experience in web applications, where MySQL has become very popular. The second edition speaks to the expanded world of MySQL, which didn’t exist in the same way when the first edition was written.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值