簡體   English   中英

選擇存在與計數

[英]Select exists vs count

我正在使用 Sqlite-net-pcl。 我需要哪個查詢更好用。 如果至少有一條記錄,我正在表中搜索。

第一次查詢

Select exists(Select 1 from invnentory where itemname='box2')

第二次查詢

Select count(*) from inventory where itemname='box2'

兩個查詢都正常工作。 但是對於 sqlite-net-pcl 來說,哪個是最好的方法呢?

這個查詢:

Select count(*) from inventory where itemname = 'box2'

通常必須進行全表掃描以返回滿足WHERE子句中條件的行數。

但是這個:

Select exists(Select 1 from invnentory where itemname='box2')

一旦找到滿足條件的第一行,它就會返回,並且只有在沒有這樣的行時才會進行全表掃描。

所以EXISTS應該表現得更好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM