常見的SQL錯誤用法,導致效能變差/速度變慢
常見的SQL錯誤用法,導致效能變差/速度變慢
01.LIMIT語句 拖慢速度 [沒有要分頁時,不要使用起始位置]
//--- //X SELECT * FROM operation WHERE type = 'SQLStats' AND name = 'SlowLog' ORDER BY create_time LIMIT 1000, 10; //---X //--- //O SELECT * FROM operation WHERE type = 'SQLStats' AND name = 'SlowLog' AND create_time > '2017-03-16 14:00:00' ORDER BY create_time limit 10; //---O
02.關聯更新,刪除 [更新時把IN 替換成JOIN]
//--- //X UPDATE operation o SET status = 'applying' WHERE o.id IN (SELECT id FROM (SELECT o.id, o.status FROM operation o WHERE o.group = 123 AND o.status NOT IN ( 'done' ) ORDER BY o.parent, o.id LIMIT 1) t); //---X //--- //O UPDATE operation o JOIN (SELECT o.id,o.status FROM operation o WHERE o.group = 123 AND o.status NOT IN ( 'done' ) ORDER BY o.parent, o.id LIMIT 1) t ON o.id = t.id SET status = 'applying' //---O
3 thoughts on “常見的SQL錯誤用法,導致效能變差/速度變慢”
SQL
效能
效率
速度
SQL
查詢
效率
速度
加快
加速
優化
效能
SQL 效率/速度 提升/加快 方法