given the following SQL in Postgres, I want to see if any row matches the criteria and not interested in what rows match. eg in this case, (if i have any perishable stock)
The only way i have achieved this is use the limit clause but its very slow (there is >100,000,000 records)
select is_perishable
from stock
where is_perishable = true
limit 1;
Is there a quicker, more eloquent method in achieving this?
You could try EXISTS:
SELECT EXISTS(
select null from stock where is_perishable = true
)
How much faster it'll be, if at all, I've no idea, but it ought to be optimized to quit early upon assessing the presence of a record.. Though so would a limit query... If it's no different, post the query plan
this might be a little bit faster:
select 1
from stock
where is_perishable = true
limit 1;
nevertheless it can be slow if there is no row with is_perishable = true. here a proper index can be helpful.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.