PHPackages                             sunsgne/where-has - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. sunsgne/where-has

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

sunsgne/where-has
=================

Webman plugin sunsgne/where-has

20PHP

Since Aug 5Pushed 1y ago1 watchersCompare

[ Source](https://github.com/sunsgneayo/illuminate-wherehas)[ Packagist](https://packagist.org/packages/sunsgne/where-has)[ RSS](/packages/sunsgne-where-has/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

[![sunsgne](https://camo.githubusercontent.com/db2c48fed13718125a662dc35983cf565cd2281664e99d52cb24efcc8f3460a3/687474703a2f2f63646e2e6e696e65313132302e636e2f6c6f676f2d692e706e67)](https://camo.githubusercontent.com/db2c48fed13718125a662dc35983cf565cd2281664e99d52cb24efcc8f3460a3/687474703a2f2f63646e2e6e696e65313132302e636e2f6c6f676f2d692e706e67)

sunsgne/illuminate-wherehas

**🐬 illuminate-wherehas method mysql optimizes query 🐬**

illuminate-wherehas method mysql optimizes query.
=================================================

[](#illuminate-wherehas--method-mysql-optimizes-query)

Wherehas of illuminate/database executes SQL statements, the response time is not ideal

 [ ![Latest Stable Version](https://camo.githubusercontent.com/92f98f9298dccd13320dfb17bd3dd236f40528fafafd9fe6af6a150bf2e06254/687474703a2f2f706f7365722e707567782e6f72672f73756e73676e652f77686572652d6861732f76) ](https://github.com/workbunny/webman-rate-limiter/releases) [ ![PHP Version Require](https://camo.githubusercontent.com/e61302cafaa3914243edffd0b6f256e19e538e8a863d5a5a4e8e0f9b745ddb67/687474703a2f2f706f7365722e707567782e6f72672f73756e73676e652f77686572652d6861732f726571756972652f706870) ](https://github.com/workbunny/webman-rate-limiter/blob/main/composer.json) [ ![GitHub license](https://camo.githubusercontent.com/ca6829ee938b184b8e874f3b1dff7af5d4cfe7991074b041c84ddbf4e485e859/687474703a2f2f706f7365722e707567782e6f72672f73756e73676e652f77686572652d6861732f6c6963656e7365) ](https://github.com/workbunny/webman-rate-limiter/blob/main/LICENSE)

开始
--

[](#开始)

```
    composer require sunsgne/illuminate-wherehas

```

用法
--

[](#用法)

说明
--

[](#说明)

主表`group_member`写入`130002`条数据，关联表`group`写入`1002`条数据

`illuminate/database` model的whereHas执行的SQL如下

```
 select * from `group_member` where exists
     (select * from `group`
               where `group_member`.`group_id` = `group`.`group_id`
                 and `id`  1
                 and `deleted_at` is null
     ) and `deleted_at` is null
```

在业务测试中，上述SQL对耗时： 0.50499701499939 秒

`sunsgne/illuminate-wherehas` model的whereHas执行的SQL如下

```
 select * from `group_member` where in
     (select * from `group`
               where `group_member`.`group_id` = `group`.`group_id`
                 and `id`  1
                 and `deleted_at` is null
     ) and `deleted_at` is null
```

在业务测试中，上述SQL对耗时：0.027166843414307 秒

mysql 中 in 与 exists 的执行计划与效率
----------------------------

[](#mysql-中-in-与-exists-的执行计划与效率)

### IN

[](#in)

- 对于 IN 查询来说，会先执行子查询，如上边的 t2 表，然后把查询得到的结果和外表 t1 做笛卡尔积，再通过条件进行筛选（这里的条件就是指 name 是否相等），把每个符合条件的数据都加入到结果集中。执行SQL如下；

`select * from t1 where name in (select name from t2);`

- 伪代码如下

```
for(x in A){
    for(y in B){
     if(condition is true) {result.add();}
    }
}
```

- in是把外表和内表做hash连接，先查询内表，再把内表结果与外表匹配，对外表使用索引（外表效率高，可用大表），而内表多大都需要查询，不可避免，故外表大的使用in，可加快效率。

### EXISTS

[](#exists)

- 对于 exists 来说，是先查询遍历外表 t1 ，然后每次遍历时，再检查在内表是否符合匹配条件，即检查是否存在 name 相等的数据。执行SQL如下；

`select * from t1 where name exists (select 1 from t2);`

- 伪代码

```
for(x in A){
  if(exists condition is true){result.add();}
}
```

- exists是对外表做loop循环，每次loop循环再对内表（子查询）进行查询，那么因为对内表的查询使用的索引（内表效率高，故可用大表），而外表有多大都需要遍历，不可避免（尽量用小表），故内表大的使用exists，可加快效率；

### 结论

[](#结论)

如果查询的两个表大小相当，那么用in和exists差别不大。如果两个表中一个较小，一个是大表，则子查询表大的用exists，子查询表小的用in。

- in是把外表和内表做hash连接，先查询内表；
- exists是对外表做loop循环，循环后在对内表查询；
- 在外表大的时用in效率更快，内表大用exists更快。

###  Health Score

14

—

LowBetter than 2% of packages

Maintenance28

Infrequent updates — may be unmaintained

Popularity3

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity17

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/65843af70bae47cda80e0386c0640e2e17e1f8c8a12ea8fade3e1297237b26f3?d=identicon)[sunsgneayo](/maintainers/sunsgneayo)

---

Top Contributors

[![sunsgneayo](https://avatars.githubusercontent.com/u/51745500?v=4)](https://github.com/sunsgneayo "sunsgneayo (47 commits)")

### Embed Badge

![Health badge](/badges/sunsgne-where-has/health.svg)

```
[![Health](https://phpackages.com/badges/sunsgne-where-has/health.svg)](https://phpackages.com/packages/sunsgne-where-has)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
