Saturday, September 7, 2013

Codeigniter: Benchmarking record count.

 Menggunakan database SQL Server 2012, dengan jumlah record diatas 1juta. Juga ditambah dengan menggunakan MemoryCache untuk process yang nomor 4.
function get_test() {
$this->benchmark->mark('one');
$this->db->select('*')->from('big_data')->order_by('id', 'asc');
$num_row1 = $this->db->get()->num_rows();
$this->benchmark->mark('two');
$this->benchmark->mark('three');
$this->db->flush_cache();
$this->db->select('COUNT(*) AS rec_count', FALSE)->from('big_data');
$num_row2 = $this->db->get()->row()->rec_count;
$this->benchmark->mark('four');
$this->benchmark->mark('five');
$this->db->flush_cache();
$num_row3 = $this->db->query("select count(*) as rec_count from big_data")->row()->rec_count;
$this->benchmark->mark('six');
$this->benchmark->mark('seven');
$num_row4 = $this->cache->memcached->get('num_row4');
if ( !$num_row4 ) {
$this->db->flush_cache();
$this->db->select('COUNT(*) AS rec_count', FALSE)->from('big_data');
$num_row4 = $this->db->get()->row()->rec_count;
$this->cache->memcached->save('num_row4', $num_row4, 60);
}
$this->benchmark->mark('eight');
$this->benchmark->mark('nine');
$this->db->flush_cache();
$num_row5 = $this->db->get('big_data')->num_rows;
$this->benchmark->mark('ten');
echo "PROCESS #1 : ".$this->benchmark->elapsed_time('one', 'two')."<br />";
echo "REC. COUNT #1 : $num_row1"."<br />";
echo "PROCESS #2 : ".$this->benchmark->elapsed_time('three', 'four')."<br />";
echo "REC. COUNT #2 : $num_row2"."<br />";
echo "PROCESS #3 : ".$this->benchmark->elapsed_time('five', 'six')."<br />";
echo "REC. COUNT #3 : $num_row3"."<br />";
echo "PROCESS #4 : ".$this->benchmark->elapsed_time('seven', 'eight')."<br />";
echo "REC. COUNT #4 : $num_row4"."<br />";
echo "PROCESS #5 : ".$this->benchmark->elapsed_time('nine', 'ten')."<br />";
echo "REC. COUNT #5 : $num_row5"."<br />"; }
RESULT #1
====================
PROCESS #1 : 8.0914
REC. COUNT #1 : 1077960
PROCESS #2 : 0.2156
REC. COUNT #2 : 1077960
PROCESS #3 : 0.1757
REC. COUNT #3 : 1077960
PROCESS #4 : 0.1782
REC. COUNT #4 : 1077960
PROCESS #5 : 7.8618
REC. COUNT #5 : 1077960

RESULT #2
====================
PROCESS #1 : 8.1435
REC. COUNT #1 : 1077960
PROCESS #2 : 0.2062
REC. COUNT #2 : 1077960
PROCESS #3 : 0.1807
REC. COUNT #3 : 1077960
PROCESS #4 : 0.0018
REC. COUNT #4 : 1077960
PROCESS #5 : 8.1332
REC. COUNT #5 : 1077960

Result #1, data di memorycache belum tersimpan (PROCESS #4 : 0.1782).
Result #2, data di memorycache sudah tersimpan (PROCESS #4 : 0.0018).

WOW.....!!!

Codeigniter: Active Record, or_like and brackets

Fix untuk "DB_active_rec.php".

Edit function "protected function _like", tambahkan yang saya beri color merah.
if ( ! is_array($field)) 
{ 
$field = array($field => $match); 
} 
$prefix = '';
Kemudian, masih di function yang sama dengan yang diatas, pindahkan yang saya beri color biru ke bagian bawah, masih didalam foreach. Lihat line yang saya beri color merah.


$prefix = (count($this->ar_like) == 0) ? '' : $type;
$v = $this->escape_like_str($v); 
if ($side == 'none') 
{ 
  $like_statement = $prefix." $k $not LIKE '{$v}'"; 
}

BECOME
$this->ar_like[] = $like_statement; 
if ($this->ar_caching === TRUE) 
{   
  $this->ar_cache_like[] = $like_statement; 
  $this->ar_cache_exists[] = 'like'; 
} 
$prefix = (count($this->ar_like) == 0) ? '' : $type;

Edit file "/system/database/DB_active_rec.php", tambah function dibawah ini :
var $ar_bracket_open = FALSE; 
var $last_bracket_type = 'where'; 
function bracket($type = NULL,$append='where') 
{

        if ( strtolower($type) == 'open' )
        {
            // fetch the key of the last entry added
            $key = key($this->ar_where);
            $this->ar_bracket_open = TRUE;

            // add a bracket close
            $this->ar_where[$key] = '('.$this->ar_where[$key];
        }
        elseif ( strtolower($type) == 'close' )
        {
            // fetch the key of the last entry added
            if ($append == 'like')    {
                end($this->ar_like);
                $key = key($this->ar_like);

                // add a bracket close
                   $this->ar_like[$key] .= ')';

                // update the AR cache clauses as well
                if ($this->ar_caching === TRUE)
                {
                    $this->ar_cache_like[$key] = $this->ar_like[$key];
                }
            } else {
                end($this->ar_where);
                $key = key($this->ar_where);

                // add a bracket close
                   $this->ar_where[$key] .= ')';

                // update the AR cache clauses as well
                if ($this->ar_caching === TRUE)
                {
                    $this->ar_cache_where[$key] = $this->ar_where[$key];
                }
            }
        }
        return $this;
 Dan cara menggunakannya seperti ini…
//bracket started
$this->db->bracket('open','like'); //bracket closed  
$this->db->or_like(array('field1'=>$filter1, 'field2'=>$filter2)); 
$this->db->bracket('close','like'); //bracket closed