微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

mysql-简单的雄辩查询耗时过长

我有2个查询.即使第一个比较复杂并且提取更多的数据,它也只需要154毫秒即可执行,而第二个则需要1.76 s来执行.

首先(快速执行):

$offers = Offer::select(\DB::raw('tbl_offer.offer_id as sys_id, 
                                  tbl_offer.offer_name, 
                                  tbl_offer.preview_url, 
                                  COALESCE(tbl_offer.is_allow_website_links, 
                                  false) as is_allow_website_links, 
                                  tbl_offer.is_require_approval, 
                                 tbl_relationship.fk_relationship_status_id, 
                                  tbl_offer.is_private,
                                  tbl_offer.currency'))
                        ->leftJoin('tbl_relationship', function ($q) use ($affiliateId) {

                        $q->on('tbl_offer.offer_id', '=', 'tbl_relationship.fk_offer_id')
                          ->where('tbl_relationship.fk_affiliate_id', '=', $affiliateId);})
                          ->whereIn('fk_offer_status_id', [ 18, 19 ])
                          ->where('is_display', 1)
                          ->where('tbl_offer.is_trd_deleted', 0)
                          ->orderBy('offer_name')
                          ->get();

第二(执行缓慢):

$currencies = Currency::select(\DB::raw('disTINCT currency_code_from AS currency'))
                 ->where('sys_name', 'openexchangerates')
                 ->orderBy('currency')
                 ->get();   

>可能是什么问题?
>您对减少加载时间有任何想法吗?

解决方法:

首先,您将两个查询合二为一.

这是第一个查询

$currencies = Currency::where('sys_name', 'openexchangerates')
            ->orderBy('currency')
            ->get();  

这是另一个

\DB::raw('disTINCT currency_code_from AS currency')

为了将两个查询合而为一,您应该使用以下命令:

$currencies = Currency::selectRaw('disTINCT currency_code_from AS currency')
            ->where('sys_name', 'openexchangerates')
            ->orderBy('currency')
            ->get();   

我希望这样可以减少执行时间.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。