3 回答

TA貢獻(xiàn)1875條經(jīng)驗(yàn) 獲得超5個(gè)贊
這要看你如何用它,畢僅where 1=1這種表達(dá)式真假判斷是要產(chǎn)生系統(tǒng)開(kāi)銷(xiāo)的。用得其所可提高效率減小開(kāi)銷(xiāo),反之亦然。例如:
select * from t1 where 1=1;
等效于
select * from t1;
前者where 1=1必然為真,此判斷為畫(huà)蛇添足,屬于無(wú)用功、徒增系統(tǒng)開(kāi)銷(xiāo)。
如果只需要查看數(shù)據(jù)表字段信息無(wú)需查看具體記錄,那么下列寫(xiě)法將非??扇?br/>select * from t1 where 1<>1;
這樣系統(tǒng)將直接輸出空記錄集,而不會(huì)花費(fèi)資源檢索具體的記錄數(shù)據(jù)到內(nèi)存中,這無(wú)疑會(huì)提高了效率。

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊
SELECT b.id FROM t_address a, t_unit b WHERE b.utj=1 AND b.ufreeze=2 AND a.id=b.uads and a.sid=3
就這個(gè)啦??!
原因很簡(jiǎn)單
b.utj=1不滿足的話,那么系統(tǒng)就直接找下一個(gè)數(shù)據(jù)
用的in的話,如果第一個(gè)不滿足,那么就匹配第二個(gè),第二個(gè)不滿足,在匹配第三個(gè)
,全部不匹配 才找下一條數(shù)據(jù)??!
這個(gè)就可以看到了,可能用IN的話,效率可能會(huì)減慢3倍,打個(gè)比方

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超4個(gè)贊
第一種 exists與not exists
select * from table1 t1 where exists(select columnName from table2 t2 where t1.columnName2 = t2.columnName2)
第二種,in,not in的用法
select * from table1 where columnName in(select columnName from table2 where columnNane = 'condition')
第三種,any,all與比較運(yùn)算的用法
-- 與任意一個(gè)比較返回真
select * from table1 where columnName > any(select columnName from table2 where columnNane = 'condition')
-- 與所有的值比較返回真
select * from table1 where columnName > all(select columnName from table2 where columnNane = 'condition')
添加回答
舉報(bào)