Skip to content

Add EXISTS query #1069

@limick

Description

@limick

Some backends allow queries to check if there is a record fulfilling certain criteria.

In SQL, for example, you can write SELECT EXISTS(SELECT 1 FROM tbl WHERE criterion). The query will return True if and only if there is at least one row such that criterion is fulfilled.

The query is logically equivalent to doing SELECT COUNT(1) FROM tbl WHERE criterion and then checking if the result is non-zero, but the existence check can in some circumstances be much faster, especially on large amounts of data.

I'd like to add this type of queries. The type signature is the same as for the count function, except it returns Bool:

    -- | Check if there is at least one record fulfilling the given criterion.
    --
    -- @since 2.11
    exists :: (MonadIO m, PersistRecordBackend record backend)
           => [Filter record] -> ReaderT backend m Bool

For backends not providing optimized existence queries we can use a default implementation using count.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions