Welcome Guest, Not a member yet? Register   Sign In
Which way is better and faster?
#1

(This post was last modified: 04-11-2022, 01:12 AM by lazyme114.)

I need top 5 notifications. My previous code was:- 

PHP Code:
$result $this->notification_model->where(array(
 
'deleted' => '0',
 
"company_id" => $this->session->userdata("company_id"),
 
"date(created_on) >= " => $range['fiscal']['start'],
 
"date(created_on) <= " => $range['fiscal']['end']
))
 ->
order_by("created_on""desc")
 ->
find_all();

$top_notifications = array();
if (
count($result) > 5) {
 
$top_notifications array_slice($result05);
} else {
 
$top_notifications $result;


and I am thinking of changing it to :-

PHP Code:
$top_notifications $this->notification_model->where(array(
 
'deleted' => '0',
 
"company_id" => $this->session->userdata("company_id"),
 
"date(created_on) >= " => $range['fiscal']['start'],
 
"date(created_on) <= " => $range['fiscal']['end']
))
 ->
order_by("created_on""desc")
        ->limit(5)
 ->
find_all(); 

Which is faster and better. First method or second method?

PS:- Its in CI Bonfire.
Reply
#2

The second is probably faster.

If you have 1,000,000 records, the first code gets all of them from the database, and get the 5 records in PHP.
$result is an array with 1,000,000 items.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB