博客
关于我
mysql批量update优化_Mysql中,21个写SQL的好习惯,你值得拥有呀
阅读量:789 次
发布时间:2023-02-12

本文共 4103 字,大约阅读时间需要 13 分钟。

SQL 编写规范与优化建议

在数据库开发过程中,良好的SQL编写习惯和优化策略能够显著提升效率并减少潜在风险。以下是一些实用的建议,帮助开发人员避免常见问题并提升代码质量。

1. 编写完成的SQL后,使用explain工具分析执行计划

在编写复杂的SQL查询时,养成习惯:完成后立即使用explain工具分析执行计划。这不仅有助于了解查询的性能表现,还能帮助检查索引的使用情况。例如:

explain select * from user where userid = 10086 or age = 18;

通过分析explain结果,可以了解以下内容:

  • 查询的执行方式(如是否使用了索引)
  • 每个步骤的执行时间
  • 是否存在性能瓶颈

2. 在删除或更新操作中添加limit限制

为了防止误操作带来的数据丢失,开发人员应当在更新或删除操作前加上limit限制。例如:

delete from user where age = 18 limit 200;

这种做法具有以下优势:

  • 降低误删风险:如果手误删除,仅会影响部分数据,通过binlog日志可以快速恢复。
  • 提升效率:在命中目标数据时,查询会立即停止,避免不必要的全表扫描。
  • 避免长事务:减少锁竞争和资源占用,提升系统的稳定性。

3. 设计数据库表时添加注释

在数据库设计阶段,所有表和字段都应添加详细注释。这不仅有助于后续维护,还能让团队成员对数据结构有更清晰的理解。例如:

-- 用户表create table user (    -- 用户ID    id int primary key auto_increment,    -- 用户名    name varchar(50) not null,    -- 年龄    age int,    -- 创建时间    create_time datetime default current_timestamp(),    -- 更新时间    update_time timestamp default current_timestamp()) comment '用户信息表';

4. 规范SQL书写格式

为了提高可读性和一致性,SQL语句应遵循统一的格式要求。关键字应保持一致,使用适当的缩进。例如:

select     id,    namefrom     userwhere     age = 18    and userid = 10086;

5. 在INSERT语句中明确字段名称

为了避免歧义和错误,INSERT语句中应明确指定需要插入的字段名称。例如:

insert into user (id, name, age) values ('123', '张三', 18);

6. 在变更操作前执行测试,确保回滚方案

在生产环境中执行重大数据库变更之前,应当先在测试环境中验证。变更操作应包含详细的步骤说明和回滚方案。例如:

-- 测试环境执行脚本@execute_script.sql

7. 添加主键和时间字段

在设计数据库表时,应始终添加主键、创建时间和更新时间字段。主键为表的灵魂,时间字段有助于审计和追踪。例如:

create table user (    id int primary key auto_increment,    name varchar(50) not null,    age int,    create_time datetime default current_timestamp(),    update_time timestamp default current_timestamp()) comment '用户信息表';

8. 检查WHERE、ORDER BY和GROUP BY后的列是否已加索引

在编写复杂查询时,应确保WHERE、ORDER BY和GROUP BY后面的列已添加索引。尤其是多表关联时,优先考虑使用组合索引。例如:

select     u.userid,    u.name,    u.agefrom     user ujoin     order o on u.userid = o.useridwhere     u.age > 18    and o.orderdate >= '2024-01-01'group by     u.useridorder by     u.name asc;

9. 修改或删除前备份数据

在进行重要数据修改或删除操作前,应当先备份相关数据,以防误操作。例如:

-- 备份用户数据backup table user to '/data/user.backup.sql';

10. 留意WHERE字段的数据类型转换

在编写WHERE子句时,应注意字段的数据类型转换可能带来的性能影响。避免不必要的类型转换,例如:

where charindex(username, '张三') > 0

这类转换可能导致索引失效,影响查询性能。

11. 尽量使用NOT NULL列

为了减少索引大小和存储空间,开发人员应当尽量使用NOT NULL列。NULL列不仅占用额外空间,还可能引发空指针问题。例如:

create table user (    id int primary key auto_increment not null,    name varchar(50) not null,    age int not null,    create_time datetime default current_timestamp(),    update_time timestamp default current_timestamp()) comment '用户信息表';

12. 在修改或删除操作前,先执行确认查询

在生产环境中执行修改或删除操作前,开发人员应当先执行确认查询,避免误操作。例如:

select id from user where age = 18;

13. 使用具体字段代替SELECT *

为了减少数据库负载,开发人员应当避免使用SELECT *,而是明确指定需要的字段。例如:

select     id,    name,    agefrom     userwhere     age > 18;

14. 使用Innodb存储引擎

在设计数据库时,所有表都应使用Innodb存储引擎。Innodb支持事务、行级锁和高并发,适合大多数业务场景。例如:

create table user (    id int primary key auto_increment,    name varchar(50) not null,    age int,    create_time datetime default current_timestamp(),    update_time timestamp default current_timestamp()) engine=Innodb;

15.统一字符集使用UTF8

为了避免乱码问题和索引失效,数据库和表的字符集应当统一使用UTF8编码。对于存储表情符号的字段,可以使用UTF8mb4。例如:

create table user (    id int primary key auto_increment,    name varchar(50) utf8 not null,    age int,    create_time datetime default current_timestamp(),    update_time timestamp default current_timestamp()) charset=utf8;

16. 使用varchar代替char

由于varchar能够存储可变长度数据,开发人员应当优先使用varchar代替char类型。例如:

create table user (    id int primary key auto_increment,    username varchar(50) not null,    age int,    create_time datetime default current_timestamp(),    update_time timestamp default current_timestamp()) comment '用户信息表';

17. 及时更新字段注释

在字段含义或表示状态发生变化时,开发人员应当及时更新字段注释,以便后续维护更加方便。例如:

-- 用户表create table user (    -- 用户ID    id int primary key auto_increment,    -- 用户名    name varchar(50) not null comment '用户登录名',    -- 年龄    age int comment '用户年龄',    -- 创建时间    create_time datetime default current_timestamp() comment '记录用户注册时间',    -- 更新时间    update_time timestamp default current_timestamp() comment '记录用户最后登录时间') comment '用户信息表';

18. 使用begin + commit事务

在进行重大数据修改或删除操作前,开发人员应当使用begin + commit事务模式,以确保操作的原子性和一致性。例如:

begin;delete from user where age = 18;commit;

这种做法可以有效防止数据不一致的问题,提升系统的稳定性。

转载地址:http://nbdfk.baihongyu.com/

你可能感兴趣的文章
MySQL基础系列—SQL分类之一
查看>>
MySQL处理千万级数据分页查询的优化方案
查看>>
mysql备份
查看>>
mysql备份与恢复
查看>>
mysql备份工具xtrabackup
查看>>
mysql备份恢复出错_尝试备份/恢复mysql数据库时出错
查看>>
mysql复制内容到一张新表
查看>>
mysql复制表结构和数据
查看>>
mysql复杂查询,优质题目
查看>>
MySQL外键约束
查看>>
MySQL多表关联on和where速度对比实测谁更快
查看>>
MySQL多表左右连接查询
查看>>
mysql大批量删除(修改)The total number of locks exceeds the lock table size 错误的解决办法
查看>>
mysql如何做到存在就更新不存就插入_MySQL 索引及优化实战(二)
查看>>
mysql如何删除数据表,被关联的数据表如何删除呢
查看>>
MySQL如何实现ACID ?
查看>>
mysql如何记录数据库响应时间
查看>>
MySQL子查询
查看>>
Mysql字段、索引操作
查看>>
mysql字段的细节(查询自定义的字段[意义-行列转置];UNION ALL;case-when)
查看>>