增加 table_definition_cache
作者归档:杨龙
MySQL 索引核心知识点总结
MySQL 索引核心知识点总结
一、索引基本概念
- 定义:索引是帮助 MySQL 高效获取数据的数据结构(如 B+树、Hash),通过减少磁盘 I/O 次数提升查询效率。
- 类比:类似于书籍的目录,通过快速定位数据行实现高效检索。
- 存储引擎差异:
- InnoDB/MyISAM 默认使用 B+树索引。
- Memory 引擎使用 Hash 索引(仅支持等值查询)。
二、索引类型
- 按功能分类:
- 主键索引(PRIMARY KEY):唯一且非空,InnoDB 中为聚簇索引(数据与索引存储在一起)。
- 唯一索引(UNIQUE):列值唯一,允许空值。
- 普通索引(INDEX):无唯一性限制,仅加速查询。
- 复合索引:包含多列,需遵循最左前缀匹配原则。
- 全文索引(FULLTEXT):适用于文本类型字段的模糊匹配(如
LIKE '%keyword%'
)。 - 前缀索引:对文本字段前 N 个字符建立索引,节省空间。
- 按存储结构分类:
- 聚簇索引:数据行与索引存储在一起(如 InnoDB 主键索引)。
- 非聚簇索引:索引与数据行分离(如 MyISAM 索引)17
三、索引的优缺点
- 优点:
- 降低查询的 I/O 成本,提升检索效率。
- 减少排序和分组操作的 CPU 消耗。
- 缺点:
- 占用额外磁盘空间,索引文件可能比数据文件更大。
- 增删改操作需维护索引,降低写性能。
四、索引底层结构(B+树)
- 特点:
- 多路平衡搜索树,树高度低,适合磁盘存储。
- 叶子节点存储实际数据或主键值(非聚簇索引需回表查询)。
- 支持范围查询和排序操作。
五、索引失效场景
- 违反最左前缀法则:复合索引未从第一列开始使用。
- 例:索引
(a, b, c)
,查询条件为b=1
或c=1
时失效。
- 例:索引
- 范围查询右侧列失效:范围查询后的索引列无法使用。
- 例:索引
(a, b)
,条件a>1 AND b=2
中b
可能失效。
- 例:索引
- 对索引列进行运算或函数操作:如
WHERE YEAR(date_column)=2025
。 - 头部模糊匹配:如
LIKE '%abc'
。 - OR 连接非索引列:若 OR 条件中包含未索引字段,全表扫描。
- 隐式类型转换:如字符串字段未加引号(
WHERE id='123'
vsWHERE id=123
)。
六、使用建议
- 适用场景:
- 频繁作为查询条件的字段(WHERE、JOIN)。
- 需要排序或分组的字段(ORDER BY、GROUP BY)。
- 避免滥用:
- 数据量小的表无需索引。
- 频繁更新的字段谨慎建索引。
- 优化策略:
- 优先选择区分度高的字段(如唯一性高的列)。
- 控制复合索引的列数(一般不超过 5 列)。
- 定期分析慢查询,调整索引策略。
七、操作语法示例
sqlCopy Code-- 创建索引
CREATE INDEX idx_name ON table_name(column1, column2);
-- 查看索引
SHOW INDEX FROM table_name;
-- 删除索引
DROP INDEX idx_name ON table_name;
sed
sed
是一种流编辑器,用于对输入流(文件或管道)进行基本的文本转换。它是 Unix/Linux 系统下的一个非常强大的文本处理工具。以下是 sed
的一些常见用法:
- 替换文本:
sed 's/旧字符串/新字符串/' 文件名
这会将文件中的第一个匹配到的“旧字符串”替换为“新字符串”。
- 替换所有匹配项:
sed 's/旧字符串/新字符串/g' 文件名
这会将文件中所有匹配到的“旧字符串”替换为“新字符串”。
- 在特定行替换:
sed '2s/旧字符串/新字符串/' 文件名
这只会替换第 2 行中的第一个匹配项。
- 删除行:
sed '2d' 文件名
这会删除第 2 行。
- 打印特定行:
sed -n '2p' 文件名
这只会打印第 2 行。
- 保存更改到文件:
sed -i 's/旧字符串/新字符串/g' 文件名
这会直接修改原文件内容。
如果你有具体的 sed
使用场景或问题,请提供更多信息,我可以给出更详细的帮助。
Linux 递归删除空目录
find /pathforyoupath/ -type d -empty -delete -print
PHP安全的获取ip
public static function getIpX(): ?string {
[$ip] = self::getIp();
return $ip;
}
public static function getIp(): array {
$ip0 = $ip = $_SERVER['REMOTE_ADDR'] ?? null;
if (in_array($ip, [
'10.29.185.7', '127.0.0.1', '172.17.0.1', '172.31.242.237', # 可信IP列表
])) {
$ip1 = $_SERVER['HTTP_X_FORWARDED_FOR'] ?? null;
if ($ip1) {
$ip0 = $ip1;
$ip = explode(',', $ip1)[0];
}
}
if (!$ip) {
$ip = $_SERVER['REMOTE_ADDR'] ?? null;
}
return [$ip, $ip0];
}
Linux swap使用率为0
Linux 中 Swap 交换分区设置教程,以及 Swap 大小与内存的关系 – 知乎
系统配置:Linux有一个swappiness参数,决定了系统使用swap的频率。默认值通常为60,值越低,越不容易使用swap。可以通过以下命令查看当前值:
cat /proc/sys/vm/swappiness
如果你希望系统更倾向于使用swap,可以将其设置为一个更高的值,例如:
sudo sysctl vm.swappiness=80
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/sunyuhua_keyboard/article/details/14248714
Nginx+PHP服务器上传限制调节
nginx:
client_max_body_size 1G;
php.ini:
upload_max_filesize = 1G
post_max_size = 1G
nginx restart failed:nginx: [alert] OPENSSL_init_ssl() failed (SSL: error:12800067:DSO support routines::could not load the shared library:filename(/snap/certbot/4325/usr/lib/x86_64-linux-gnu/ossl-modules/fips.so): /snap/certbot/4325/usr/lib/x86_64-linux-gnu/ossl-modules/fips.so: cannot open shared object file: No such file or directory error:12800067:DSO support routines::could not load the shared library error:07880025:common libcrypto routines::reason(37):name=fips error:0700006D:configuration file routines::module initialization error:module=providers, value=provider_sect retcode=-1 ) 解决办法
nginx restart failed:
nginx: [alert] OPENSSL_init_ssl() failed (SSL: error:12800067:DSO support routines::could not load the shared library:filename(/snap/certbot/4325/usr/lib/x86_64-linux-gnu/ossl-modules/fips.so): /snap/certbot/4325/usr/lib/x86_64-linux-gnu/ossl-modules/fips.so: cannot open shared object file: No such file or directory error:12800067:DSO support routines::could not load the shared library error:07880025:common libcrypto routines::reason(37):name=fips error:0700006D:configuration file routines::module initialization error:module=providers, value=provider_sect retcode=-1 )
解决办法:
nginx restart failed:
nginx: [alert] OPENSSL_init_ssl() failed (SSL: error:12800067:DSO support routines::could not load the shared library:filename(/snap/certbot/4325/usr/lib/x86_64-linux-gnu/ossl-modules/fips.so): /snap/certbot/4325/usr/lib/x86_64-linux-gnu/ossl-modules/fips.so: cannot open shared object file: No such file or directory error:12800067:DSO support routines::could not load the shared library error:07880025:common libcrypto routines::reason(37):name=fips error:0700006D:configuration file routines::module initialization error:module=providers, value=provider_sect retcode=-1 )
Ask for help or search for solutions at https://community.letsencrypt.org. See the logfile /var/log/letsencrypt/letsencrypt.log or re-run Certbot with -v for more details.
[root@ooops ~]# find / -name fips.so
/usr/lib64/ossl-modules/fips.so
[root@ooops ~]# mount --bind /usr/lib64/ossl-modules/ /snap/certbot/4325/usr/lib/x86_64-linux-gnu/ossl-modules/
4325
注意替换为你的id
Could not find rpcgen / rockylinux 8 / mysql 5.7 编译
powertools 里面有rpcgen:
dnf install --enablerepo=powertools rpcgen
[root@212d1d5f5e2b mysql-5.7.44]# rm CMakeCache.txt && cmake . -DBUILD_CONFIG=mysql_release -DWITH_BOOST=/root/boost_1_59_0 -DCMAKE_INSTALL_PREFIX=/usr/local
阿里云小内存机器频繁死机可能原因
`dnf makecache --timer
` 命令导致内存不足