【问题标题】:can we avoid eloquent in large no of records我们可以避免在大量记录中雄辩吗
【发布时间】:2021-06-23 04:03:04
【问题描述】:

我正在使用 laravel eloquent,并且在数据库中有数千条记录的情况下,因此当使用 eloquent 关系时,我的查询执行速度很慢。我应该避免在这些情况下使用 eloquent 还是其他方式?

这里是mysql查询

$leads=Lead::select($col)
                ->join("gc_od_leads_detail as ld", "gc_od_leads.leads_id", "=", "ld.ld_leads_id")
                ->join("gc_od_chat as c", "gc_od_leads.leads_chat_id", "=", "c.chat_id")
                ->join("gc_od_group as g", "c.chat_group_id", "=", "g.group_octachat_id")
                ->where('c.chat_tags','sales')
                ->whereIn('c.chat_group_id',$filter['groups']);
            if(!empty($filter['keyword'])) {
                $leads=$leads->where(function ($q) use ($filter) {
                           $q->where('ld_name','like', "%".$filter['keyword']."%")
                           ->orWhere('ld_email','like', "%".$filter['keyword']."%")
                           ->orWhere('ld_phoneno','like', "%".$filter['keyword']."%");
                   });
            }
            if(!empty($filter['startDate']) && !empty($filter['endDate'])){
                $leads=$leads->whereBetween('leads_created_date', [$filter['startDate']." 00:00:00",$filter['endDate']." 23:59:59"]);
            }
            $leads=$leads->orderBy('leads_created_date','desc');
            return $leads;
        }

我在边消息和聊天表中有超过 500 000 条录音。我在 eloquent 中更改了查询并对其进行了调试

查询:

    Lead::select('leads_id','leads_chat_id')->with(["detail"=>function($q){
               $q->select("ld_leads_id");
           }])->with(["chat"=>function($q){
               $q->select("chat_id")->where(['chat_status'=>1]);
           }])->where("leads_status",1)->get();

Debuging Ouput
    array:3 [▼
      0 => array:3 [▼
        "query" => "select `leads_id`, `leads_chat_id` from `gc_od_leads` where `leads_status` = ?"
        "bindings" => array:1 [▼
          0 => 1
        ]
        "time" => 14.85
      ]
      1 => array:3 [▼
        "query" => "select `ld_leads_id` from `gc_od_leads_detail` where `gc_od_leads_detail`.`ld_leads_id` in (2278918, 2278919, 2278920, 2278921, 2278922, 2278923, 2278924, 22789 ▶"
        "bindings" => []
        "time" => 0.59
      ]
      2 => array:3 [▼
        "query" => "select `chat_id` from `gc_od_chat` where `gc_od_chat`.`chat_id` in (3496457, 3496458, 3496459, 3496460, 3496461, 3496462, 3496463, 3496464, 3496465, 3496466, 34 ▶"
        "bindings" => array:1 [▶]
        "time" => 4.21
      ]
    ]

i 在上面的输出中,您可以看到它首先获取所有潜在客户记录,然后转到潜在客户详细信息和聊天表,如果我只想找出聊天状态 =1 的潜在客户,它仍然会查询所有潜在客户,这是减慢我的速度的原因查询

在我们使用 join 的地方,它不会以这种方式工作,我认为这将节省时间和空间,这是我的我发布这个问题,我认为很多人都有同样的问题,没有人讨论这一点

【问题讨论】:

  • “我应该避免在这些情况下使用 eloquent” - 您是否尝试过不使用 Eloquent 来查看是否有区别?在决定最终解决方案之前,您需要调试/分析您的代码/查询以找出减速的​​原因。您是否为表添加了适当的索引?究竟有多少条记录是“千”?
  • 是的,我调试查询并发现由于 where 子句导致执行速度变慢而存在内部查询
  • 请在您的问题中添加所有信息。如果您已经完成调试/分析等,我们需要知道这一点。您基本上是在问我们应该做什么,同时拥有比我们更多的信息。
  • 好的,我会的,但是告诉我一件事,你能以雄辩的方式转换上面定义的查询吗?如果没有大量记录,你不认为我会花更多时间吗??
  • Eloquent 如何将您提供的代码转换为 SQL 查询并没有本质上的低效。如果您愿意,您当然可以切换到使用本机 SQL。对于帮助您提高性能的人来说,它更容易阅读。

标签: php mysql laravel eloquent query-optimization


【解决方案1】:

让我们看一下其中的一部分。

if(!empty($filter['keyword'])) {
   $leads=$leads->where(function ($q) use ($filter) {
          $q->where('ld_name','like', "%".$filter['keyword']."%")
            ->orWhere('ld_email','like', "%".$filter['keyword']."%")
            ->orWhere('ld_phoneno','like', "%".$filter['keyword']."%");
       });
 }

这种关键字匹配方案本质上是缓慢的,而且是灾难性的。它在 Eloquent 和原生 SQL 中都很慢。如果不使用full table scan,它就无法在 MySQL 中工作。也就是说,它必须检查表的每一行以查找匹配项,并且不能在 MySQL 中利用任何索引查找方案。为什么?

column LIKE 'constant%'

可以查看column 上的索引并快速找到以'constant' 开头的任何值。但是

column LIKE '%constant%'

必须查看表中的每个值。前导的% 使索引查找无用。

在 MySQL 中,您最好调查 MySQL's FULLTEXT searching 作为处理关键字查找的一种方式。 (最新版本的 postgreSQL 可以直接使用不同类型的索引处理此类查询,但 MySQL 不行。)

【讨论】:

  • 我编辑了这个问题,请检查一下,谢谢
猜你喜欢
  • 2017-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-01
  • 2023-02-04
  • 2019-02-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多