PHPackages                             sllhsmile/wherehasin - 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. [Database &amp; ORM](/categories/database)
4. /
5. sllhsmile/wherehasin

ActiveLibrary[Database &amp; ORM](/categories/database)

sllhsmile/wherehasin
====================

Hyperf ORM whereHasIn

v0.0.1(7mo ago)5158MITPHPPHP &gt;=7.0

Since Jan 26Pushed 7mo ago1 watchersCompare

[ Source](https://github.com/sllhSmile/wherehasin)[ Packagist](https://packagist.org/packages/sllhsmile/wherehasin)[ Docs](https://github.com/sllhSmile/wherehasin)[ RSS](/packages/sllhsmile-wherehasin/feed)WikiDiscussions main Synced today

READMEChangelog (3)Dependencies (1)Versions (4)Used By (0)

Hyperf WHERE HAS IN
===================

[](#hyperf-where-has-in)

 [![](https://camo.githubusercontent.com/ffe7adf17b1d38c79e0e81f80884e0e21f43129b2b96c7a123ea3bd032f09cb2/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d3733383944382e7376673f7374796c653d666c6174)](https://github.com/sllhSmile/wherehasin/blob/main/LICENSE) [ ![Build Status](https://github.com/sllhSmile/wherehasin/workflows/Phpunit/badge.svg) ](https://github.com/sllhSmile/wherehasin/actions) [![](https://camo.githubusercontent.com/33c3f0cee757253c95c3e5e1a5e3b53e4f26e44ab3f2df8ac27d436075f28607/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f736c6c68536d696c652f7768657265686173696e2e7376673f636f6c6f723d343039394445)](https://github.com/sllhSmile/wherehasin/releases) [![](https://camo.githubusercontent.com/358ed2b9e63f317cf494e209d647bb2c21985178bfe1e9b082cc624cd25528d5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f736c6c68536d696c652f7768657265686173696e2e7376673f636f6c6f723d)](https://packagist.org/packages/sllhsmile/wherehasin) [![](https://camo.githubusercontent.com/58ee6668ad0f0c53e9a258560f4eb7483b281fe1b8520bf8bdc2cb248bff4a46/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d372b2d3539613966382e7376673f7374796c653d666c6174)](https://camo.githubusercontent.com/58ee6668ad0f0c53e9a258560f4eb7483b281fe1b8520bf8bdc2cb248bff4a46/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d372b2d3539613966382e7376673f7374796c653d666c6174)

`Hyperf wherehasin`是一个可以提升`Hyperf ORM`关联关系查询性能的扩展包，可以替代`Hyperf ORM`中的`whereHas`以及`whereHasMorphIn`查询方法。

环境
--

[](#环境)

- PHP &gt;= 7
- Hyperf &gt;= 2.0

安装
--

[](#安装)

```
  composer require sllhsmile/wherehasin
```

### 简介

[](#简介)

`Hyperf`的关联关系查询`whereHas`在日常开发中给我们带来了极大的便利，但是在**主表**数据量比较多的时候会有比较严重的性能问题，主要是因为`whereHas`用了`where exists (select * ...)`这种方式去查询关联数据。

通过这个扩展包提供的`whereHasIn`方法，可以把语句转化为`where id in (select xxx.id ...)`的形式，从而提高查询性能，下面我们来做一个简单的对比：

> 当主表数据量较多的情况下，`where id in`会有明显的性能提升；当主表数据量较少的时候，两者性能相差无几。

主表`test_users`写入`130002`条数据，关联表`test_user_profiles`写入`1002`条数据，查询代码如下

```
