mysql中寫sql的好習慣

2022-06-18 02:54:09 字數 3051 閱讀 9930

1 寫完sql先explain 檢視執行計畫

寫完sql,用explain分析一下,尤其注意走不走索引

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

2操作delete或者update語句,加個limit

delete from euser where age > 30 limit 200;

好處:降低寫錯sql的代價

sql效率很可能更高

避免了長事務

資料量大的話,容易把cpu打滿,系統越來越卡和越刪越慢

3設計表的時候,所有表和字段都新增相應的注釋

設計資料庫表的時候,所有表和字段都加上對應注釋,後面更容易維護

create table  'account'  (

'id'  int(11) not null auto_increment comment  '主鍵',

』name' varchar(255) default null  comment  '賬戶名',

『balance』 int(11)  default null comment   '餘額',

『create_time』  datetime not null comment '建立時間',

『update_time』 datetime not null on update current_timestamp comment '更新時間',

primary key ('id'),

key 'idx_name'  ('name') using btree

)  engine=innodb auto_increment=1570068 default charset=utf8 row_format=redundant comment='賬戶表';

4 sql書寫格式,關鍵字大小保持一致,使用縮排

select stu.name,sum(stu.score)

from student stu

where stu.classno='1班'

group by stu.name;

5 insert 插入標明對應欄位名稱

insert into  student (student_id,name,score)  values ('666','sky','100');

6 變更sql操作先在測試環境執行,寫明詳細的操作步驟以及回滾方案,並在上生產前review

7設計資料庫表的時候,加上3個字段:主鍵,create_time,update_time

create table  'account'  (

'id'  int(11) not null auto_increment comment  '主鍵',

』name' varchar(255) default null  comment  '賬戶名',

『balance』 int(11)  default null comment   '餘額',

『create_time』  datetime not null comment '建立時間',

『update_time』 datetime not null on update current_timestamp comment '更新時間',

primary key ('id'),

key 'idx_name'  ('name') using btree

)  engine=innodb auto_increment=1570068 default charset=utf8 row_format=redundant comment='賬戶表';

主鍵一般要加上,沒有主鍵的表是沒有靈魂的表

建立時間和更新時間,還是建議加上,詳細審計,跟蹤記錄,都是有用的

8 寫完sql,檢查where,order by, groug by 後面的列,多表關聯的列是否已加索引,優先考慮組合索引

新增索引:

alter table user add index idx_address_age 9(address,age)

explain select * from user where address='深圳'  order by age;

9修改或刪除重要資料前,要先備份,先備份,先備份

10 where後面的11欄位,留意其資料型別的隱式轉換

反例:select * from user where userid = 123;

正例:select * from user where userid = '123';

因為不加單引號,是字串跟數字的比較,他們型別不匹配,mysql會做隱式型別轉換,把他們轉換成浮點數再做比較,最後導致索引失效

11  盡量把所有列定義為not null

not null  列更節省空間,null列需要乙個額外位元組作為判斷是否為null的標誌位

null列需要注意空指標問題,null列在計算和比較的時候,需要注意空指標問題

12減少不必要的字段返回,如使用select 《具體字段》代替select *

13 所有表必須使用innodb儲存引擎

14資料庫和表的的字符集盡量統一使用utf8

可以避免亂碼問題

可以避免,不同字符集比較轉換,導致的索引失效問題

15 盡量使用varchar代替char

'deptname' varchar(100) default null comment 』部門名稱『

因為首先變長字段儲存空間小,可以節省儲存空間

16 sql命令列修改資料,養成begin + commit 事務的習慣

begin;

update account set balance = 100000 where name = 'sky';

commit;

17索引命名要規範,主鍵索引名為pk_欄位名,唯一索引名為uk_欄位名,普通索引名為idx_欄位名

說明:pk即primary key,  uk即unique key,   idx即index的簡稱

18 如果修改更新資料過多,考慮批量進行

for each (200次)

delete from account limit 500;

養成寫注釋的好習慣

注釋是很重要的,特別在一些大公司裡,對於注釋的要求特別高,看乙個程式設計師的水平高低,一般看他的注釋就能看出來,所以平常養成寫注釋的好習慣對應程式設計師來說是很重要的。一般注釋和源 的比例是1 1,甚至1 2或者1 3。自己就有這種體會。別人發來 要我修改,一看源 那叫慘不忍睹,密密麻麻的一片,不見...

MySQL 寫SQL語句的幾個習慣

1.設計表時 加注釋及欄位編碼格式 排序規則及主鍵 建立時間 更新時間 create table attribution home id int 11 not null auto increment comment 主鍵id attribution home varchar 255 collate ...

Mysql,你應該掌握的好習慣

1 更新某張表的某條記錄時,應該同時更新它的update time欄位,這個操作可以無需在 中完成 mysql alter table table name modify column update time timestamp not null default current timestamp ...