SQL查詢的where條件優化之一

2021-06-09 06:26:56 字數 504 閱讀 1797

對於mssql中,查詢是非常普遍的事情。對於where後面的條件。系統預設是從左右依次執行的。

所以介於此,我們需要把能夠大範圍過濾掉資料的條件優先排在前面。同理逐級遞減。

舉個簡單的例子。有個表(students)資料有100000條

select id,name,age from students where age=20 and id>1000

上述的語句就比較合理。

如果:select id,name,age from students where  id>1000 and age=20

那麼這條語句就很耗時了。(不相信的同學可以自己手動試試)

我拿自己做了800w的資料測試了下:(在mssql2008上執行)

select * from prices where brandid=833453 and dealerid>3000

select * from prices where  dealerid>3000 and brandid=83453 

SQL查詢優化,注意where條件的順序

1.測試表 employee 雇員id 部門id 薪金 emp id dept id salary 01 01 1050 02 01 2000 ok,我們要查詢部門01下,薪金高於1000的雇員 2.原則及兩個sql的對比 原則,多數資料庫都是從 左到右的順序處理條件,把能過濾更多資料的條件放在前面...

SQL查詢優化,注意where條件的順序

sql查詢優化,注意where條件的順序 1.測試表 employee 雇員id 部門id 薪金 emp id dept id salary 01 01 1050 02 01 2000 ok,我們要查詢部門01下,薪金高於1000的雇員 2.原則及兩個sql的對比 原則,多數資料庫都是從 左到右的順...

SQL查詢優化,注意where條件的順序

size medium 1.測試表 employee 雇員id 部門id 薪金 emp id dept id salary 01 01 1050 02 01 2000 ok,我們要查詢部門01下,薪金高於1000的雇員 2.原則及兩個sql的對比 原則,color red 多數資料庫都是從 左到右的...