`
k1280000
  • 浏览: 196728 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

sql 里判断表中是否有满足条件的记录时一个省时省力的方法

 
阅读更多

question :

query from table peron ,validate are there any records which name = C 

 

table person like below :

 

id  | name

1     A

2     B

3     C

4     C

5     C

 

SQL 1 : (more common)  SELECT COUNT(*) FROM PERSON  WHERE NAME = 'C';

 

SQL 2 : SELECT 1 FROM DUAL WHERE EXISTS (SELECT * FROM PERSON WHERE NAME = 'C');

 

 

 

COUNT(*) will scan all rows in the person table ,if the record in person is more than 1 million one query may cost many time ; but if we use the exists  ,the scan will return immediately when the record been scanned .

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics