茹志鹃在《妯娌》中说,再看红英自己,那是连半个钟头的工都不肯耽误的,也从没见她吃过一口零食,一看就知道是个会精打细算、会过日子的人。曾有人调侃,已婚身份最是适合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)

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.