php Laravel或Where或WhereHas降低了应用程序的速度

92vpleto  于 2023-06-28  发布在  PHP
关注(0)|答案(1)|浏览(159)

下面的代码检查订阅方是否可以访问属性。
如果属性subscriber_id ==当前订户id,则可以访问它。
如果属性subscriber_id位于链接办公室订户ID内,则可以访问它。
如果属性表auction_booked_by或表示表auction_booked_by等于subscriber,则可以访问它。
我在查询构建器中使用了orWhere和orWhereHas。我正在获取数据,但需要7到8秒以上。使用out或WhereHas,只需2到3秒。解决办法是什么?
下面是我获取属性数据的完整代码:$this->model是属性模型。财产属于许多陈述。

$currentSubscriber = Auth::user()->subscriber_id;
$linkedOfficesSubscribers = Auth::user()->subscriber->getLinkedAgencyIdsAttribute();
$subscriberIds = $linkedOfficesSubscribers->merge($currentSubscriber);
$sidewaysSubscriberIds = Auth::user()->subscriber->getSidewaysAgenciesIdsAttribute();
$query = $this->model
    ->join('subscribers', 'properties.subscriber_id', '=','subscribers.id')
    ->join('property_auction_details', 'property_auction_details.property_id', '=','properties.id')
    ->leftjoin('property_listing_details','property_listing_details.property_id', '=','properties.id')
    ->with('subscriber')
    ->whereIn('properties.subscriber_id', $subscriberIds)
    ->orWhere(function ($query) use ($sidewaysSubscriberIds) {
        $query->whereIn('properties.subscriber_id', $sidewaysSubscriberIds)
            ->where('properties.auction_booked_by', Auth::user()->subscriber_id)
            ->orWhereHas('presentations', function ($query) {
                $query->where('presentations.auction_booked_by', Auth::user()->subscriber_id);
            });
    });
rt4zxlrg

rt4zxlrg1#

$currentSubscriber = Auth::user()->subscriber_id;
$linkedOfficesSubscribers = Auth::user()->subscriber->getLinkedAgencyIdsAttribute();
$subscriberIds = $linkedOfficesSubscribers->merge($currentSubscriber);
$sidewaysSubscriberIds = Auth::user()->subscriber->getSidewaysAgenciesIdsAttribute();

$query = $this->model
    ->join('subscribers', 'properties.subscriber_id', '=', 'subscribers.id')
    ->join('property_auction_details', 'property_auction_details.property_id', '=', 'properties.id')
    ->leftJoin('property_listing_details', 'property_listing_details.property_id', '=', 'properties.id')
    ->whereIn('properties.subscriber_id', $subscriberIds)
    ->orWhere(function ($query) use ($sidewaysSubscriberIds, $currentSubscriber) {
        $query->where('properties.auction_booked_by', $currentSubscriber)
            ->whereIn('properties.subscriber_id', $sidewaysSubscriberIds);
    })
    ->orWhereHas('presentations', function ($query) use ($currentSubscriber) {
        $query->where('presentations.auction_booked_by', $currentSubscriber);
    })
    ->with('subscriber') // Eager loading
    ->get();

相关问题