-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Q10Viking
committed
Mar 23, 2024
1 parent
61351e9
commit 2af6192
Showing
8 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
sidebarDepth: 3 | ||
sidebar: auto | ||
prev: | ||
text: Back To 目录 | ||
link: /MySQL/ | ||
typora-root-url: ..\.vuepress\public | ||
--- | ||
|
||
|
||
|
||
|
||
|
||
首先,我们要明白数据库连接池是一种管理和维护数据库连接的技术。它在应用程序和数据库之间建立了一个连接的缓冲池,用于存储和复用已经建立好的数据库连接。 | ||
|
||
那为什么需要数据库连接池呢? | ||
|
||
1. **提高性能:**数据库连接的建立和断开是比较耗时的操作,频繁地创建和销毁连接会增加系统的负担。通过使用连接池,可以避免频繁地创建和关闭连接,减少了连接的开销,提高了系统的性能。 | ||
2. **资源管理:**数据库连接是有限的资源,如果每个请求都创建一个新的连接,可能导致连接过多而耗尽资源。连接池通过对连接的管理和复用,能够更有效地管理数据库连接,避免资源的浪费。 | ||
3. **并发处理:**在高并发的场景下,如果每个请求都去单独连接数据库,可能会导致数据库连接数量过多,从而限制了系统的扩展性。连接池允许多个请求共享连接,减少了数据库连接的数量,提高了并发处理能力。 | ||
4. **连接可靠性:**数据库连接可能会因为网络问题或服务器故障而中断,当发生这种情况时,连接池能够检测到连接的失效,并重新创建一个可用的连接,确保应用程序的可靠运行。 | ||
|
||
**总结来说:** 数据库连接池是为了提高性能、更有效地管理资源、提高并发处理能力和增加连接的可靠性而存在的。它通过缓存已经建立好的连接,复用连接以及管理连接的生命周期,从而减少了连接的开销,提高了系统的性能和稳定性。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--- | ||
sidebarDepth: 3 | ||
sidebar: auto | ||
prev: | ||
text: Back To 目录 | ||
link: /MySQL/ | ||
typora-root-url: ..\.vuepress\public | ||
--- | ||
|
||
|
||
|
||
|
||
|
||
1. **丢失修改(Lost Update):**两个或多个事务同时修改同一数据,并且最终只有一个事务的修改被保留,其他事务的修改被覆盖或丢失。这种情况可能会导致数据的部分更新丢失,造成数据的不一致性。 | ||
2. **脏读(Dirty Read):**一个事务读取了另一个事务未提交的数据。假设事务A修改了一条数据但未提交,事务B却读取了这个未提交的数据,导致事务B基于不准确的数据做出了错误的决策。 | ||
3. **不可重复读(Non-repeatable Read):**一个事务在多次读取同一数据时,得到了不同的结果。假设事务A读取了一条数据,事务B修改或删除了该数据并提交,然后事务A再次读取同一数据,发现与之前的读取结果不一致,造成数据的不一致性。 | ||
4. **幻读(Phantom Read):**一个事务在多次查询同一范围的数据时,得到了不同数量的结果。假设事务A根据某个条件查询了一组数据,事务B插入了符合该条件的新数据并提交,然后事务A再次查询同一条件下的数据,发现结果集发生了变化,产生了幻觉般的新增数据。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
--- | ||
sidebarDepth: 3 | ||
sidebar: auto | ||
prev: | ||
text: Back To 目录 | ||
link: /MySQL/ | ||
typora-root-url: ..\.vuepress\public | ||
--- | ||
|
||
|
||
|
||
|
||
|
||
DECIMAL字段类型用于存储精确的定点数值,可以指定总共的位数和小数点后的位数。这使得它非常适合用于存储货币金额,因为货币金额通常需要精确到小数点后几位。 | ||
|
||
```sql | ||
CREATE TABLE my_table ( | ||
amount DECIMAL(18, 2) | ||
); | ||
``` | ||
|
||
上述语句创建了一个名为amount的DECIMAL字段,总共有18位,其中小数点后有2位。 | ||
|
||
使用DECIMAL字段类型的好处包括: | ||
|
||
1. **精确性:**DECIMAL字段类型可以确保货币金额的精确性,避免由于浮点数运算带来的精度问题。 | ||
2. **可控性:**通过指定总位数和小数位数,可以精确控制存储的金额范围和精度。 | ||
3. **计算准确性:**DECIMAL字段类型支持数值计算,如加法、减法和乘法等,保证计算结果的准确性。 | ||
|
||
需要注意的是,DECIMAL字段类型占用的存储空间相对较大,因此在设计表结构时需要考虑存储和性能需求,合理选择DECIMAL字段的位数。另外,应根据具体业务需求和国际化要求,考虑货币符号和货币转换等问题。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--- | ||
sidebarDepth: 3 | ||
sidebar: auto | ||
prev: | ||
text: Back To 目录 | ||
link: /MySQL/ | ||
typora-root-url: ..\.vuepress\public | ||
--- | ||
|
||
CHAR和VARCHAR的区别可以总结如下: | ||
|
||
1. **存储方式:**CHAR是固定长度的字符类型,而VARCHAR是可变长度的字符类型。 | ||
2. **占用空间:**CHAR会以固定的长度存储数据,不论实际存储的字符数目,而VARCHAR则根据实际需要的空间动态分配存储。 | ||
3. **尾随空格:**CHAR在存储时会用空格填充到指定长度,并在检索时需要删除尾随空格,而VARCHAR没有这个问题。 | ||
4. **长度限制:**CHAR的长度范围为1到255个字符,而VARCHAR的长度范围也是255个字符,但可以根据需求设定更长的长度。 | ||
5. **访问效率:**由于CHAR是固定长度的,它在某些情况下可能会比VARCHAR稍微快一些。 | ||
|
||
综上所述,CHAR适合存储长度固定且固定大小的数据,而VARCHAR适用于长度可变的数据 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--- | ||
sidebarDepth: 3 | ||
sidebar: auto | ||
prev: | ||
text: Back To 目录 | ||
link: /MySQL/ | ||
typora-root-url: ..\.vuepress\public | ||
--- | ||
|
||
|
||
|
||
1. **UNION:**UNION用于合并两个或多个查询结果集,并去除重复的行。它将多个查询的结果**合并为一个结果集,并自动去除重复的行。**在执行UNION操作时,数据库会进行额外的去重操作,这可能会带来一定的性能开销。 | ||
|
||
```sql | ||
-- 使用UNION | ||
SELECT * FROM test_user u | ||
UNION | ||
SELECT * FROM test_user u; | ||
``` | ||
|
||
使用 UNION ALL,可以看到查询结果有 5条数据 | ||
|
||
![image-20240323171628979](/images/MySQL/image-20240323171628979.png) | ||
|
||
2. **UNION ALL:**UNION ALL同样**用于合并查询结果集,但不去除重复的行。**它将多个查询的结果简单地合并在一起,包括重复的行。相比于UNION,UNION ALL不进行去重操作,因此执行效率更高。 | ||
|
||
```sql | ||
-- 使用UNION ALL | ||
SELECT * FROM test_user u | ||
UNION ALL | ||
SELECT * FROM test_user u; | ||
``` | ||
|
||
使用 UNION ALL,可以看到查询结果有 10 条数据 | ||
|
||
![image-20240323171653778](/images/MySQL/image-20240323171653778.png) |