如何优化Laravel 5.6.39口才查询

时间:2019-05-20 08:33:28

标签: php mysql laravel laravel-5 eloquent

我有一个关于每次从DB检索数据时减少数据库连接的问题。在地图中,我想优化此查询。而且我不知道该怎么做。请帮忙。提前致谢。这是代码。

$activity_result = Searchy::search('activities')
            ->fields('activity_title', 'activity_description', 'activity_address')
            ->query($request->get('search'))->getQuery()
            ->where('activity_datetime_to', '>=', $currentDatetime)
            ->having('relevance', '>', 50)->limit(10)->get();

        $activities_status= [];
        $activities = [];
        foreach ($activity_result as $activity) {
            $invitedOnActivity = $this->isInvitedOnActivity($activity->id, $activity->user_id, $authUser->id);
            if($invitedOnActivity == true && $activity->activity_privacy == 'invite_only')
            {
               if($activity->activity_privacy_visible == 0)
                {
                    $activity = Activity::where('id', $activity->id)->with('joins')->withCount('joins')->first();
                    // return response()->json(['q'=>$activity],200);
                } else if ($activity->activity_privacy_visible == 1 && in_array($activity->user_id, $userFriendIds))
                {
                    $activity = Activity::where('id', $activity->id)->where('activity_privacy_visible', 1)->with('joins')->withCount('joins')->first();
                    // return response()->json(['w'=>$activity],200);      
                } else {
                    continue;
                } 
            } else if(($invitedOnActivity == false && $activity->activity_privacy != 'invite_only') || $activity->user_id == $authUser->id)
            {
                if($activity->activity_privacy_visible == 0)
                {
                    $activity = Activity::where('id', $activity->id)->with('joins')->withCount('joins')->first();
                    // return response()->json(['q'=>$activity],200);
                } 
                else if ($activity->activity_privacy_visible == 1 && in_array($activity->user_id, $userFriendIds))
                {
                    $activity = Activity::where('id', $activity->id)->where('activity_privacy_visible', 1)->with('joins')->withCount('joins')->first();
                    // return response()->json(['w'=>$activity],200);      
                } 
                else {
                    continue;
                }
            } else {
                continue;
            } 

此查询需要大量时间才能执行。 这是活动表架构:

class CreateActivitiesTable extends Migration
{
   /**
   * Run the migrations.
   *
   * @return void
   */
    public function up()
    {
       Schema::create('activities', function (Blueprint $table) {
           $table->bigInteger('id', true)->unsigned();
           $table->text('activity_type');
           $table->bigInteger('user_id')->unsigned()->index('user_id');
           $table->bigInteger('category_id')->unsigned()->index('category_id');
           $table->bigInteger('subcategory_id')->unsigned()->index('subcategory_id');
           $table->text('activity_privacy');
           $table->integer('activity_privacy_visible')->default(0);
           $table->dateTime('activity_datetime_from');
           $table->dateTime('activity_datetime_to');
           $table->string('activity_address');
           $table->bigInteger('company_id')->unsigned()->nullable()->index('company_id');
           $table->decimal('latitude', 11, 8);
           $table->decimal('longitude', 11, 8);
           $table->integer('age_from');
           $table->integer('age_to');
           $table->string('people_limit')->nullable();
           $table->string('activity_picture')->nullable();
           $table->string('activity_title', 35)->nullable();
           $table->string('activity_description', 100)->nullable();
           $table->timestamps();

           $table->foreign('user_id')
            ->references('id')
            ->on('users')
            ->onDelete('cascade');

           $table->foreign('category_id')
            ->references('id')
            ->on('categories')
            ->onDelete('cascade');

           $table->foreign('subcategory_id')
            ->references('id')
            ->on('subcategories')
            ->onDelete('cascade');

           $table->foreign('company_id')
            ->references('id')
            ->on('companies')
            ->onDelete('cascade');
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('activities');
}

这是活动表架构的代码。

0 个答案:

没有答案