Home Database Mysql Tutorial MySQLSchema设计(二)

MySQLSchema设计(二)

Jun 07, 2016 pm 04:18 PM
design

茹志鹃在《妯娌》中说,再看红英自己,那是连半个钟头的工都不肯耽误的,也从没见她吃过一口零食,一看就知道是个会精打细算、会过日子的人。曾有人调侃,已婚身份最是适合DBA,毕竟,不当家不知柴米贵,年底的资源容量订购,那一分钱都是心头肉啊,会吃的吃

       茹志鹃在《妯娌》中说,“再看红英自己,那是连半个钟头的工都不肯耽误的,也从没见她吃过一口零食,一看就知道是个会精打细算、会过日子的人。”曾有人调侃,已婚身份最是适合DBA,毕竟,不当家不知柴米贵,年底的资源容量订购,那一分钱都是心头肉啊,会吃的吃千顿,不会吃的吃一顿。而且,故障诊断以及性能调优时,OS层的APP直接拖垮DB的案例也是家珍如数啊。所以,思前顾后,吃穿常有。谓之,DBA以俭德辟难。

      活在大数据时代下,勤俭节约更是DBA的传统美德。慎重选择数据类型很重要,对类型当持有斤斤计较的心思,理由如下:

● 计算、进而减负CPU负载

㈠ 3种数据类型 1. INT(M) 到底有多M?

M 默认是11,最大有效显示宽度是255。无论M多大,INT一定是4 bytes。M仅表示显示宽度,与存储大小或类型包含的值的范围无关。离了zerofill这个属性,M是毫无意义的,硬说有呢、大概也是为了显示字符的个数、人性化点。对于存储和计算而言,INT(11)和INT(255)是相同的。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

mysql> create table t (id int(2));

Query OK, 0 rows affected (0.08 sec)

 

mysql> insert into t select 10086;

Query OK, 1 row affected (0.01 sec)

Records: 1 Duplicates: 0 Warnings: 0

 

mysql> select * from t;

+-------+

| id |

+-------+

| 10086 |

+-------+

1 row in set (0.01 sec)

 

mysql> alter table t change column id id int(16);

Query OK, 0 rows affected (0.02 sec)

Records: 0 Duplicates: 0 Warnings: 0

 

mysql> select * from t;

+-------+

| id |

+-------+

| 10086 |

+-------+

1 row in set (0.00 sec)

 

mysql> alter table t change column id id int(16) zerofill;

Query OK, 1 row affected (0.19 sec)

Records: 1 Duplicates: 0 Warnings: 0

 

mysql> select * from t;

+------------------+

| id |

+------------------+

| 0000000000010086 |

+------------------+

1 row in set (0.00 sec)

 

mysql> alter table t change column id id int(5) zerofill;

Query OK, 0 rows affected (0.02 sec)

Records: 0 Duplicates: 0 Warnings: 0

 

mysql> select * from t;

+-------+

| id |

+-------+

| 10086 |

+-------+

1 row in set (0.00 sec)

 

mysql> alter table t change column id id int(6) zerofill;

Query OK, 0 rows affected (0.01 sec)

Records: 0 Duplicates: 0 Warnings: 0

 

mysql> select * from t;

+--------+

| id |

+--------+

| 010086 |

+--------+

1 row in set (0.00 sec)

2 计算VARCHAR(N)N的最大值

今有道面试题:若一张表中只有一个字段VARCHAR(N)类型,utf8编码,则N最大值为多少?

我们不急着计算,先来看几个注意事项:

● 最大行长度是65535,不过NDB引擎除外。这个限制了列的数目,比如char(255) utf8,那么列的数目最多有65535/(255*3)=85,列的数目可以从这里得到依据

● 字符集问题

latin1:占用一个字节

gbk:每个字符最多占用2个字节

utf8:每个字符最多占用3个字节

● 长度列表

需要额外地在长度列表上存放实际的字符长度:小于255为1个字节,大于255则要2个字节

● 1byte/row开销

在字符集选用latin1情况下,依据限制3,应该有65533长度可用,然而:

1

2

3

4

mysql> create table max_len_varchar(col varchar(65533) charset latin1);

ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes storage overhead, check the manual. You have to change some columns to TEXT or BLOBs

mysql> create table max_len_varchar(col varchar(65532) charset latin1);

Query OK, 0 rows affected (0.16 sec)

所以,MySQL中,实际存储应该是从第2个字节开始

至此,我们便可以从容得出开头的答案:(65535-1-2)/3。有始有终,再以一道面试题结束本小节:

create table t (col1 int(11), col2 char(50), col3 varchar(N)) charset=utf8;这里的N最大值?有兴趣的朋友可自行算下。

3 timestamp那些事

先看个MySQL datetime的bug提提神:

1

2

3

4

5

6

7

8

9

10

11

12

13

mysql> create table t (start_time datetime,stop_time datetime);

Query OK, 0 rows affected (0.12 sec)

 

mysql> insert into t (start_time, stop_time) values ("2014-01-19 21:46:18", "2014-01-20 00:21:31");

Query OK, 1 row affected (0.02 sec)

 

mysql> select start_time, stop_time, stop_time - start_time from t;

+---------------------+---------------------+------------------------+

| start_time | stop_time | stop_time - start_time |

+---------------------+---------------------+------------------------+

| 2014-01-19 21:46:18 | 2014-01-20 00:21:31 | 787513 |

+---------------------+---------------------+------------------------+

1 row in set (0.00 sec)

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

ZTE 5G portable Wi-Fi U50S goes on sale for NT$899 at first launch: top speed 500Mbps ZTE 5G portable Wi-Fi U50S goes on sale for NT$899 at first launch: top speed 500Mbps Apr 26, 2024 pm 03:46 PM

According to news on April 26, ZTE’s 5G portable Wi-Fi U50S is now officially on sale, starting at 899 yuan. In terms of appearance design, ZTE U50S Portable Wi-Fi is simple and stylish, easy to hold and pack. Its size is 159/73/18mm and is easy to carry, allowing you to enjoy 5G high-speed network anytime and anywhere, achieving an unimpeded mobile office and entertainment experience. ZTE 5G portable Wi-Fi U50S supports the advanced Wi-Fi 6 protocol with a peak rate of up to 1800Mbps. It relies on the Snapdragon X55 high-performance 5G platform to provide users with an extremely fast network experience. Not only does it support the 5G dual-mode SA+NSA network environment and Sub-6GHz frequency band, the measured network speed can even reach an astonishing 500Mbps, which is easily satisfactory.

Starting at 649 yuan, Kubi Cube Xiaoku Tablet 2 Lite is here: 11-inch eye-protecting large screen + 8000mAh large battery Starting at 649 yuan, Kubi Cube Xiaoku Tablet 2 Lite is here: 11-inch eye-protecting large screen + 8000mAh large battery Mar 05, 2024 pm 05:34 PM

According to news on March 4, Kubi Rubik's Cube will launch the "Xiaoku Tablet 2Lite" tablet computer on March 5, with an initial price of 649 yuan. It is reported that the new tablet is equipped with Unisoc’s T606 processor, which uses a 12nm process and consists of two 1.6GHz ArmCortex-A75 CPUs and six ArmCortex-A55 processors. The screen uses a 10.95-inch IPS eye-protection screen with a resolution of 1280x800 and a brightness as high as 350 nits. In terms of imaging, Xiaoku Tablet 2Lite has a 13-megapixel main camera on the rear and a 5-megapixel selfie lens on the front. It also supports 4G Internet access/calls, Bluetooth 5.0, and Wi-Fi5. In addition, the official claimed that this tablet&l

Retro trend! HMD and Heineken jointly launch flip phone: transparent shell design Retro trend! HMD and Heineken jointly launch flip phone: transparent shell design Apr 17, 2024 pm 06:50 PM

According to news on April 17, HMD teamed up with the well-known beer brand Heineken and the creative company Bodega to launch a unique flip phone - The Boring Phone. This phone is not only full of innovation in design, but also returns to nature in terms of functionality, aiming to lead people back to real interpersonal interactions and enjoy the pure time of drinking with friends. Boring mobile phone adopts a unique transparent flip design, showing a simple yet elegant aesthetic. It is equipped with a 2.8-inch QVGA display inside and a 1.77-inch display outside, providing users with a basic visual interaction experience. In terms of photography, although it is only equipped with a 30-megapixel camera, it is enough to handle simple daily tasks.

Honor Magic V3 debuts AI defocus eye protection technology: effectively alleviates the development of myopia Honor Magic V3 debuts AI defocus eye protection technology: effectively alleviates the development of myopia Jul 18, 2024 am 09:27 AM

According to news on July 12, the Honor Magic V3 series was officially released today, equipped with the new Honor Vision Soothing Oasis eye protection screen. While the screen itself has high specifications and high quality, it also pioneered the introduction of AI active eye protection technology. It is reported that the traditional way to alleviate myopia is "myopia glasses". The power of myopia glasses is evenly distributed to ensure that the central area of ​​​​sight is imaged on the retina, but the peripheral area is imaged behind the retina. The retina senses that the image is behind, promoting the eye axis direction. grow later, thereby deepening the degree. At present, one of the main ways to alleviate the development of myopia is the "defocus lens". The central area has a normal power, and the peripheral area is adjusted through optical design partitions, so that the image in the peripheral area falls in front of the retina.

Teclast M50 Mini tablet is here: 8.7-inch IPS screen, 5000mAh battery Teclast M50 Mini tablet is here: 8.7-inch IPS screen, 5000mAh battery Apr 04, 2024 am 08:31 AM

According to news on April 3, Taipower’s upcoming M50 Mini tablet computer is a device with rich functions and powerful performance. This new 8-inch small tablet is equipped with an 8.7-inch IPS screen, providing users with an excellent visual experience. Its metal body design is not only beautiful but also enhances the durability of the device. In terms of performance, the M50Mini is equipped with the Unisoc T606 eight-core processor, which has two A75 cores and six A55 cores, ensuring a smooth and efficient running experience. At the same time, the tablet is also equipped with a 6GB+128GB storage solution and supports 8GB memory expansion, which meets users’ needs for storage and multi-tasking. In terms of battery life, M50Mini is equipped with a 5000mAh battery and supports Ty

How to design the end page of ppt to be attractive enough How to design the end page of ppt to be attractive enough Mar 20, 2024 pm 12:30 PM

At work, ppt is an office software often used by professionals. A complete ppt must have a good ending page. Different professional requirements give different ppt production characteristics. Regarding the production of the end page, how can we design it more attractively? Let’s take a look at how to design the end page of ppt! The design of the ppt end page can be adjusted in terms of text and animation, and you can choose a simple or dazzling style according to your needs. Next, we will focus on how to use innovative expression methods to create a ppt end page that meets the requirements. So let’s start today’s tutorial. 1. For the production of the end page, any text in the picture can be used. The important thing about the end page is that it means that my presentation is over. 2. In addition to these words,

Vivo's phone with the strongest signal! vivo X100s is equipped with a universal signal amplification system: 21 antennas, 360° surround design Vivo's phone with the strongest signal! vivo X100s is equipped with a universal signal amplification system: 21 antennas, 360° surround design Jun 03, 2024 pm 08:41 PM

According to news on May 13, vivoX100s was officially released tonight. In addition to excellent images, the new phone also performs very well in terms of signal. According to vivo’s official introduction, vivoX100s uses an innovative universal signal amplification system, which is equipped with up to 21 antennas. This design has been re-optimized based on the direct screen to balance many signal requirements such as 5G, 4G, Wi-Fi, GPS, and NFC. This makes vivoX100s the mobile phone with the strongest signal reception capability in vivo’s history. The new phone also uses a unique 360° surround design, with antennas distributed around the body. This design not only enhances the signal strength, but also optimizes various daily holding postures to avoid problems caused by improper holding methods.

Honor X60i mobile phone is on sale starting from 1,399 yuan: visual quadrilateral OLED direct screen Honor X60i mobile phone is on sale starting from 1,399 yuan: visual quadrilateral OLED direct screen Jul 29, 2024 pm 08:25 PM

According to news on July 29, the Honor X60i mobile phone is officially on sale today, starting at 1,399 yuan. In terms of design, the Honor X60i mobile phone adopts a straight screen design with a hole in the center and almost unbounded ultra-narrow borders on all four sides, which greatly broadens the field of view. Honor X60i parameters Display: 6.7-inch high-definition display Battery: 5000mAh large-capacity battery Processor: Dimensity 6080 processor (TSMC 6nm, 2x2.4G A76+6×2G A55) System: MagicOS8.0 system Other features: 5G signal enhancement, smart capsule, under-screen fingerprint, dual MIC, noise reduction, knowledge Q&A, photography capabilities: rear dual camera system: 50 million pixels main camera, 2 million pixels auxiliary lens, front selfie lens: 8 million pixels, price: 8GB

See all articles