I am working in a legacy system that uses row() plus limit() to get one result. I didn't understand why, because row() already give me one result, but a coworker said that improves performance. Example:
$this->db
->select()
->select('extract(epoch from cadevolucao.dt_sistema) as data_sistema')
->select('extract(epoch from cadevolucao.dt_previsao_alta) as data_previsao')
->select('cadevolucao.cd_evolucao, cadevolucao.dt_sistema')
->join('contatnd', 'cadevolucao.num_atend = contatnd.num_atend')
->join('cadplanejamento', 'cadevolucao.cd_evolucao = cadplanejamento.cd_evolucao')
->where('contatnd.cd_pessoa', $cd_pessoa)
->where('tp_evolucao', -1)
->where('tipo', 1)
->order_by('cadevolucao.cd_evolucao','desc')
->limit(3)
->get('cadevolucao')
->row();
I looked for in the CI Documentation and Google, not founding anything useful about that.
Can someone explain if it's needed the limit() when using row() in Active Record's CI and why?
According to what i know row
method returns a single result row. If your query has more than one row, it returns only the first row.But internally its still fetching all the rows fetched by the query and storing it in an array. Yes i think i must agree with your co-worker indeed limit will have a performance impact.
this is what row
method does internally
/**
* Returns a single result row - object version
*
* @param int $n
* @return object
*/
public function row_object($n = 0)
{
$result = $this->result_object();
if (count($result) === 0)
{
return NULL;
}
if ($n !== $this->current_row && isset($result[$n]))
{
$this->current_row = $n;
}
return $result[$this->current_row];
}
as you its either returning the first element or the argument supplied i.e the row index.
row
is actually an alias to this row_object