简体   繁体   English

SQL Laravel 5.7 unique() 收集真的很慢

[英]SQL Laravel 5.7 unique() on collection really slow

I'm trying to calculate the number of unique records based on a mobile column that has an index via the Laravel collect and unique method.我正在尝试根据具有索引的mobile列通过 Laravel collectunique方法计算唯一记录的数量。 I have 200,000 rows and have a column called optout_csv_schedule_id that has an index on it along with the mobile.我有 200,000 行,并且有一个名为optout_csv_schedule_id的列,它与移动设备一起有一个索引。 Right now, it's been running over 15 minutes for the query to execute, how can I improve the performance of this as I need to calculate the number of unique numbers out of the 200,000, my current query is:现在,执行查询已经运行了超过 15 分钟,我该如何提高它的性能,因为我需要计算 200,000 中唯一数字的数量,我当前的查询是:

/**
 * Get valid lead count
 */
protected function getValidLeadCount($schedule_id)
{
    $optoutConnectionLogs = OptoutConnectionLog::where('optout_csv_schedule_id', $schedule_id)
                                               ->get();

    // no leads
    if (!$optoutConnectionLogs) {
        return 0;
    }

    // count total unique leads
    $uniqueLeads = collect($optoutConnectionLogs)->unique('mobile')->count();
    return $uniqueLeads;
}

You are not using the database to do the unique, you already got the records with ->get() , and are using PHP/Laravel to do it.您没有使用数据库来执行唯一操作,您已经使用->get()获得了记录,并且正在使用 PHP/Laravel 来执行此操作。 That will be much slower than using the database.这将比使用数据库慢得多

Use distinct() to get unique records, eg:使用distinct()获取唯一记录,例如:

$optoutConnectionLogs = OptoutConnectionLog::where('optout_csv_schedule_id', $schedule_id)
    ->select('mobile')
    ->distinct()
    ->get();

It seems to be difficult to calculate the number of unique numbers out of the 200,000 in Laravel .似乎很难计算Laravel中 200,000 个唯一数字的数量。 Try to change as follows:尝试改变如下:

protected function getValidLeadCount($schedule_id)
{
    $uniqueLeads = OptoutConnectionLog::where('optout_csv_schedule_id', $schedule_id)
                                                 ->distinct('mobile')
                                                 ->count('mobile');
    return $uniqueLeads;
}

You read all the data into memory, convert it into PHP objects, and then iterate to count the numbers.你把所有的数据读入memory,转换成PHP个对象,然后迭代统计数字。 The database index you created is not used at all.您创建的数据库索引根本没有被使用。

Your needs should be simplified into the following code您的需求应该简化为以下代码

return OptoutConnectionLog::where('optout_csv_schedule_id', $schedule_id)
                           ->distinct('mobile')
                           ->count();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM