I have 2 queries. Even though the first one is more complicated and pulls much more data it takes only 154 ms to execute, meanwhile the second one takes 1.76 s to execute.
First (executing fast):
$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();
Second (executing slowly):
$currencies = Currency::select(\DB::raw('DISTINCT currency_code_from AS currency'))
->where('sys_name', 'openexchangerates')
->orderBy('currency')
->get();
first of all you are using 2 queries into one.
This is the first query:
$currencies = Currency::where('sys_name', 'openexchangerates')
->orderBy('currency')
->get();
And this is another:
\DB::raw('DISTINCT currency_code_from AS currency')
In order to use both queries into one, you should use this:
$currencies = Currency::selectRaw('DISTINCT currency_code_from AS currency')
->where('sys_name', 'openexchangerates')
->orderBy('currency')
->get();
I hope this way will decrease the executing time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With