Skip to content
This repository has been archived by the owner on Nov 21, 2023. It is now read-only.

Condition for a constraint? #1

Closed
chrismerron opened this issue Aug 14, 2020 · 1 comment
Closed

Condition for a constraint? #1

chrismerron opened this issue Aug 14, 2020 · 1 comment
Labels
good first issue Good for newcomers

Comments

@chrismerron
Copy link

Hi,
Is there a way to provide a condition for the constraint, rather than a callable on the query?

For example, I'd like to do this:
Builder::hasByNonDependentSubquery('relationship', '>=', $value)

This is possible when using
Builder::has('relationship', '>=', $value)

Many thanks!

@mpyw mpyw added the good first issue Good for newcomers label Aug 17, 2020
@mpyw
Copy link
Owner

mpyw commented Aug 17, 2020

image

Unfortunately that is not supported. The feature is useful but its SQL looks hard to be optimized.

has without counting
Standard where exists (select * from sub_table where sub_table.id = main_table.foreign_id)
Patched where main_table.foreign_id in (select sub_table.id from sub_table)

The patched query will be normally optimized to have the same execution plan as for INNER JOIN clause.

has with counting
Standard where (select count(*) from sub_table where sub_table.id = main_table.foreign_id) > ?
Patched ???

In this case, what should we do for that? We can use (select sub_table.id, count(*) from sub_table group by sub_table.id) to avoid dependent subqueries, but we need the HAVING clause to use the result which may degrade the performance.

The current best solution for you is still:

Builder::has('relationship', '>=', $value)

@mpyw mpyw closed this as completed Aug 21, 2020
@mpyw mpyw pinned this issue Aug 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants