简体   繁体   English

选择存在与计数

[英]Select exists vs count

I am using Sqlite-net-pcl.我正在使用 Sqlite-net-pcl。 I need to which query is better to use.我需要哪个查询更好用。 I am searching in a table if there is at least one record.如果至少有一条记录,我正在表中搜索。

First query第一次查询

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

Second query第二次查询

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

Both queries are working right.两个查询都正常工作。 But which is the best approach to go for sqlite-net-pcl?但是对于 sqlite-net-pcl 来说,哪个是最好的方法呢?

This query:这个查询:

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

normally must do a full table scan to return the number of rows that satisfy the condition in the WHERE clause.通常必须进行全表扫描以返回满足WHERE子句中条件的行数。

But this:但是这个:

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

will return as soon as it finds the 1st row that satisfies the condition and it will do a full table scan only if there isn't such a row.一旦找到满足条件的第一行,它就会返回,并且只有在没有这样的行时才会进行全表扫描。

So EXISTS should perform better.所以EXISTS应该表现得更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM