SQL查詢的幾點注意事項


    1. 不使用select * ,而是列出需要查詢的列名。

    2. 對於多個表查詢,用相應的別名+列名查詢,減少解析時間。如:

    select a.p_personId,a.p_personName,b.p_sexName

    from p_person a

    left join p_sex b on a.p_personId= b.p_personId;

  而不要寫成

    select a.p_personId,p_personName,p_sexName

    from p_person a

    left join p_sex b on a.p_personId= b.p_personId;


  二樓提出此句可改為

    select a.p_personId,a.p_personName,b.p_sexName
    from p_person a
    left join p_sex b using(personId);

  學習了

 3. 關於create table select的問題:create table select會比create table +insert into快很多,但會帶來鎖表問題

   4. 對於相關子查詢用盡量用join代替:not in、not exists可用left join代替;in、exists可用inner join代替

   5. 對於判斷記錄是否存在,不要用count(*),而選擇用left join或exists.

   6. 索引:盡量使用索引,但不要對索引列進行運算,如:

    select p_personId,p_personName

    from p_person

    where p_personId+2=100;

  而應改為

    select p_personId,p_personName

    from p_person

    where p_personId=100-2;

  不要在索引列中使用函數、格式轉換、多字段連接(p_personId||’A’),盡量使用聚集索引

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM