Mysql中,這21個寫SQL的好習慣,你值得擁有呀

{"type":"doc","content":[{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"前言"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"每一個好習慣都是一筆財富,本文分SQL後悔藥, SQL性能優化,SQL規範優雅三個方向,分享寫SQL的21個好習慣,謝謝閱讀,加油哈~"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"公衆號:"},{"type":"text","marks":[{"type":"strong"}],"text":"撿田螺的小男孩"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"1. 寫完SQL先explain查看執行計劃(SQL性能優化)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"日常開發寫SQL的時候,儘量養成這個好習慣呀:寫完SQL後,用explain分析一下,尤其注意走不走索引。"}]},{"type":"codeblock","attrs":{"lang":""},"content":[{"type":"text","text":"explain select * from user where userid =10086 or age =18;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/16/1687a0e98511faa3d25be6e6d3a87c73.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"2、操作delete或者update語句,加個limit(SQL後悔藥)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在執行刪除或者更新語句,儘量加上limit,以下面的這條 SQL 爲例吧:"}]},{"type":"codeblock","attrs":{"lang":""},"content":[{"type":"text","text":"delete from euser where age > 30 limit 200;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"因爲加了limit 主要有這些好處:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/f1/f1d20d30dc0cc40d5b1944495f1c244f.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"降低寫錯SQL的代價"},{"type":"text","text":", 你在命令行執行這個SQL的時候,如果不加limit,執行的時候一個"},{"type":"text","marks":[{"type":"strong"}],"text":"不小心手抖"},{"type":"text","text":",可能數據全刪掉了,如果"},{"type":"text","marks":[{"type":"strong"}],"text":"刪錯"},{"type":"text","text":"了呢?加了limit 200,就不一樣了。刪錯也只是丟失200條數據,可以通過binlog日誌快速恢復的。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"SQL效率很可能更高"},{"type":"text","text":",你在SQL行中,加了limit 1,如果第一條就命中目標return, 沒有limit的話,還會繼續執行掃描表。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"避免了長事務"},{"type":"text","text":",delete執行時,如果age加了索引,MySQL會將所有相關的行加寫鎖和間隙鎖,所有執行相關行會被鎖住,如果刪除數量大,會直接影響相關業務無法使用。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"數據量大的話,容易把CPU打滿"},{"type":"text","text":" ,如果你刪除數據量很大時,不加 limit限制一下記錄數,容易把cpu打滿,導致越刪越慢的。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"3. 設計表的時候,所有表和字段都添加相應的註釋(SQL規範優雅)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這個好習慣一定要養成啦,設計數據庫表的時候,所有表和字段都添加相應的註釋,後面更容易維護。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"正例:"}]},{"type":"codeblock","attrs":{"lang":""},"content":[{"type":"text","text":"CREATE TABLE `account` (\n `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主鍵Id',\n `name` varchar(255) DEFAULT NULL COMMENT '賬戶名',\n `balance` int(11) DEFAULT NULL COMMENT '餘額',\n `create_time` datetime NOT NULL COMMENT '創建時間',\n `update_time` datetime NOT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '更新時間',\n PRIMARY KEY (`id`),\n KEY `idx_name` (`name`) USING BTREE\n) ENGINE=InnoDB AUTO_INCREMENT=1570068 DEFAULT CHARSET=utf8 ROW_FORMAT=REDUNDANT COMMENT='賬戶表';"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"反例:"}]},{"type":"codeblock","attrs":{"lang":""},"content":[{"type":"text","text":"CREATE TABLE `account` (\n `id` int(11) NOT NULL AUTO_INCREMENT,\n `name` varchar(255) DEFAULT NULL,\n `balance` int(11) DEFAULT NULL,\n `create_time` datetime NOT NULL ,\n `update_time` datetime NOT NULL ON UPDATE CURRENT_TIMESTAMP,\n PRIMARY KEY (`id`),\n KEY `idx_name` (`name`) USING BTREE\n) ENGINE=InnoDB AUTO_INCREMENT=1570068 DEFAULT CHARSET=utf8;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"4. SQL書寫格式,關鍵字大小保持一致,使用縮進。(SQL規範優雅)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"正例:"}]},{"type":"codeblock","attrs":{"lang":""},"content":[{"type":"text","text":"SELECT stu.name, sum(stu.score)\nFROM Student stu\nWHERE stu.classNo = '1班'\nGROUP BY stu.name"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"反例:"}]},{"type":"codeblock","attrs":{"lang":""},"content":[{"type":"text","text":"SELECT stu.name, sum(stu.score) from Student stu WHERE stu.classNo = '1班' group by stu.name."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"顯然,統一關鍵字大小寫一致,使用縮進對齊,會使你的SQL看起來更優雅~"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"5. INSERT語句標明對應的字段名稱(SQL規範優雅)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"反例:"}]},{"type":"codeblock","attrs":{"lang":""},"content":[{"type":"text","text":"insert into Student values ('666','撿田螺的小男孩','100');"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"正例:"}]},{"type":"codeblock","attrs":{"lang":""},"content":[{"type":"text","text":"insert into Student(student_id,name,score) values ('666','撿田螺的小男孩','100');"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"6. 變更SQL操作先在測試環境執行,寫明詳細的操作步驟以及回滾方案,並在上生產前review。(SQL後悔藥)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"變更SQL操作先在測試環境測試,避免有語法錯誤就放到生產上了。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"變更Sql操作需要寫明詳細操作步驟,尤其有依賴關係的時候,如:先修改表結構再補充對應的數據。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"變更Sql操作有回滾方案,並在上生產前,review對應變更SQL。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"7.設計數據庫表的時候,加上三個字段:主鍵,create"},{"type":"text","marks":[{"type":"italic"}],"text":"time,update"},{"type":"text","text":"time。(SQL規範優雅)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"反例:"}]},{"type":"codeblock","attrs":{"lang":""},"content":[{"type":"text","text":"CREATE TABLE `account` (\n `name` varchar(255) DEFAULT NULL COMMENT '賬戶名',\n `balance` int(11) DEFAULT NULL COMMENT '餘額',\n) ENGINE=InnoDB AUTO_INCREMENT=1570068 DEFAULT CHARSET=utf8 ROW_FORMAT=REDUNDANT COMMENT='賬戶表';"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"正例:"}]},{"type":"codeblock","attrs":{"lang":""},"content":[{"type":"text","text":"CREATE TABLE `account` (\n `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主鍵Id',\n `name` varchar(255) DEFAULT NULL COMMENT '賬戶名',\n `balance` int(11) DEFAULT NULL COMMENT '餘額',\n `create_time` datetime NOT NULL COMMENT '創建時間',\n `update_time` datetime NOT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '更新時間',\n PRIMARY KEY (`id`),\n KEY `idx_name` (`name`) USING BTREE\n) ENGINE=InnoDB AUTO_INCREMENT=1570068 DEFAULT CHARSET=utf8 ROW_FORMAT=REDUNDANT COMMENT='賬戶表';"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"理由:"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"主鍵一定要加上的,沒有主鍵的表是沒有靈魂的"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"創建時間和更新時間的話,還是建議加上吧,詳細審計、跟蹤記錄,都是有用的。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"阿里開發手冊也提到這個點,如圖"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/44/44a366ff10071b4f73aa6e07e8c51f1d.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"8. 寫完SQL語句,檢查where,order by,group by後面的列,多表關聯的列是否已加索引,優先考慮組合索引。(SQL性能優化)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"反例:"}]},{"type":"codeblock","attrs":{"lang":""},"content":[{"type":"text","text":"select * from user where address ='深圳' order by age ;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/51/5160b3e48a0fcedfa92fac90c0c1716a.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"正例:"}]},{"type":"codeblock","attrs":{"lang":""},"content":[{"type":"text","text":"添加索引\nalter table user add index idx_address_age (address,age)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/ec/ec9bc739f772a43af59104fdf888b158.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"9.修改或刪除重要數據前,要先備份,先備份,先備份(SQL後悔藥)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果要修改或刪除數據,在執行SQL前一定要先備份要修改的數據,萬一誤操作,還能喫口"},{"type":"text","marks":[{"type":"strong"}],"text":"後悔藥"},{"type":"text","text":"~"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"10. where後面的字段,留意其數據類型的隱式轉換(SQL性能優化)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"反例:"}]},{"type":"codeblock","attrs":{"lang":""},"content":[{"type":"text","text":"//userid 是varchar字符串類型\nselect * from user where userid =123;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/b5/b5c8ee27ad79d4c7b73eb3682e47a298.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"正例:"}]},{"type":"codeblock","attrs":{"lang":""},"content":[{"type":"text","text":"select * from user where userid ='123';"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/27/2756500a2374bb348222ecefc640e935.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"理由:"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"因爲不加單引號時,是字符串跟數字的比較,它們類型不匹配,MySQL會做隱式的類型轉換,把它們轉換爲浮點數再做比較,最後導致索引失效"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":" 11. 儘量把所有列定義爲NOT NULL(SQL規範優雅)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"NOT NULL列更節省空間"},{"type":"text","text":",NULL列需要一個額外字節作爲判斷是否爲 NULL 的標誌位。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"NULL列需要注意空指針問題"},{"type":"text","text":",NULL列在計算和比較的時候,需要注意空指針問題。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"12.修改或者刪除SQL,先寫WHERE查一下,確認後再補充 delete 或 update(SQL後悔藥)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"尤其在操作生產的數據時,遇到修改或者刪除的SQL,先加個where查詢一下,確認OK之後,再執行update或者delete操作"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"13.減少不必要的字段返回,如使用select 代替 select * (SQL性能優化)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"反例:"}]},{"type":"codeblock","attrs":{"lang":""},"content":[{"type":"text","text":"select * from employee;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"正例:"}]},{"type":"codeblock","attrs":{"lang":""},"content":[{"type":"text","text":"select id,name from employee;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"理由:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"節省資源、減少網絡開銷。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可能用到覆蓋索引,減少回表,提高查詢效率。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"14.所有表必須使用Innodb存儲引擎(SQL規範優雅)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Innodb "},{"type":"text","marks":[{"type":"strong"}],"text":"支持事務,支持行級鎖,更好的恢復性"},{"type":"text","text":",高併發下性能更好,所以呢,沒有特殊要求(即Innodb無法滿足的功能如:列存儲,存儲空間數據等)的情況下,所有表必須使用Innodb存儲引擎"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"15.數據庫和表的字符集統一使用UTF8(SQL規範優雅)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"統一使用UTF8編碼"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可以避免亂碼問題"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可以避免,不同字符集比較轉換,導致的索引失效問題"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"16. 儘量使用varchar代替 char。(SQL性能優化)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"反例:"}]},{"type":"codeblock","attrs":{"lang":""},"content":[{"type":"text","text":" `deptName` char(100) DEFAULT NULL COMMENT '部門名稱'"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"正例:"}]},{"type":"codeblock","attrs":{"lang":""},"content":[{"type":"text","text":"`deptName` varchar(100) DEFAULT NULL COMMENT '部門名稱'"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"理由:"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"因爲首先變長字段存儲空間小,可以節省存儲空間。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其次對於查詢來說,在一個相對較小的字段內搜索,效率更高。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"17. 如果修改字段含義或對字段表示的狀態追加時,需要及時更新字段註釋。 (SQL規範優雅)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這個點,是阿里開發手冊中,Mysql的規約。你的字段,尤其是表示枚舉狀態時,如果含義被修改了,或者狀態追加時,爲了後面更好維護,需要即時更新字段的註釋。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"18. SQL修改數據,養成begin + commit 事務的習慣(SQL後悔藥)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"正例:"}]},{"type":"codeblock","attrs":{"lang":""},"content":[{"type":"text","text":"begin;\nupdate account set balance =1000000\nwhere name ='撿田螺的小男孩';\ncommit;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"反例:"}]},{"type":"codeblock","attrs":{"lang":""},"content":[{"type":"text","text":"update account set balance =1000000\nwhere name ='撿田螺的小男孩';"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"19. 索引命名要規範,主鍵索引名爲 pk"},{"type":"text","marks":[{"type":"italic"}],"text":" 字段名;唯一索引名爲 uk "},{"type":"text","text":"字段名 ; 普通索引名則爲 idx _字段名。(SQL規範優雅)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"說明: pk"},{"type":"text","marks":[{"type":"italic"}],"text":" 即 primary key;uk "},{"type":"text","text":" 即 unique key;idx _ 即 index 的簡稱。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"20. WHERE從句中不對列進行函數轉換和表達式計算"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"假設loginTime加了索引"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"反例:"}]},{"type":"codeblock","attrs":{"lang":""},"content":[{"type":"text","text":"select userId,loginTime from loginuser where Date_ADD(loginTime,Interval 7 DAY) >=now();"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"正例:"}]},{"type":"codeblock","attrs":{"lang":""},"content":[{"type":"text","text":"explain select userId,loginTime from loginuser where loginTime >= Date_ADD(NOW(),INTERVAL - 7 DAY);"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"理由:"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"索引列上使用mysql的內置函數,索引失效"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/e2/e2e54aa74b2e9a628184b64e8670098f.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"21.如果修改/更新數據過多,考慮批量進行。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"反例:"}]},{"type":"codeblock","attrs":{"lang":""},"content":[{"type":"text","text":"delete from account limit 100000;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"正例:"}]},{"type":"codeblock","attrs":{"lang":""},"content":[{"type":"text","text":"for each(200次)\n{\n delete from account limit 500;\n}"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"理由:"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"大批量操作會會造成主從延遲。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"大批量操作會產生大事務,阻塞。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"大批量操作,數據量過大,會把cpu打滿。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"參考與感謝"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"[delete後加 limit是個好習慣麼] (https://blog.csdn.net/qq_39390545/article/details/107519747)"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"《阿里開發手冊》"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"公衆號"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/55/55e89982fa8c246805d48fc18054973c.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章