Showing posts with label SQL Server 2012. Show all posts
Showing posts with label SQL Server 2012. Show all posts

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.....!!!

Monday, May 13, 2013

Codeigniter sqlsrv_driver.php (function _limit) (fix)

Fix update untuk "function _limit", agar compatible dengan SQL Server All Version.
Edit file "/system/database/drivers/sqlsrv/sqlsrv_driver.php", Line 575:
 function _limit($sql, $limit, $offset) { if($offset === FALSE || version_compare($this->version(), "9.0.00", "<=")) { // do simple limit if no offset, or version is earlier than 2005 $i = $limit + $offset; return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$i.' ', $sql); } else { if (version_compare($this->version(), "11.0.00", ">=")) { $orderBy = "ORDER BY "; $orderBy .= implode(', ', $this->ar_orderby); if ($this->ar_order !== FALSE) { $orderBy .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC'; } else { $orderBy .= '1 ASC '; } // for Denali's new feature if ($this->ar_orderby !== FALSE) return $sql . "OFFSET " . $offset . " ROWS FETCH NEXT " . $limit . " ROWS ONLY"; else return $sql . $orderBy . "OFFSET " . $offset . " ROWS FETCH NEXT " . $limit . " ROWS ONLY"; // for Denali's new feature // return $sql . "OFFSET " . $offset . " ROWS FETCH NEXT " . $limit . " ROWS ONLY"; } else { $orderBy = "ORDER BY "; $orderBy .= implode(', ', $this->ar_orderby); if ($this->ar_order !== FALSE) { $orderBy .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC'; } $sql = preg_replace('/(\\'. $orderBy .'\n?)/i','', $sql); $sql = preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 row_number() OVER ('.$orderBy.') AS CI_offset_row_number, ', $sql); $newSQL = "SELECT * \nFROM (\n" . $sql . ") AS A \nWHERE A.CI_offset_row_number BETWEEN (" .($offset + 1) . ") AND (".($offset + $limit).")"; return $newSQL; } } }

Sunday, May 12, 2013

Connect Codeigniter with SQL Server 2012

[Steps 1]. Download File2 yang diperlukan for x64 :
1. xampp-win32-1.8.1-VC9-installer.exe ( Install Apache v2.4 & PHP v5.4 )
2. vcredist_x64.exe ( Visual C++ Redistributed for x64 )
3. SQLSRV30_PHP_CONNECTION.EXE ( Microsoft Drivers for PHP for SQL Server )
4. sqlncli_2012_x64.msi ( SQLSERVER NATIVE CLIENT 2012 )

[Steps 2]. Setelah proses installasi [Steps 1] :
1. Copy-kan file "php_pdo_sqlsrv_54_ts.dll" & "php_sqlsrv_54_ts.dll" ke folder "c:\xampp\php\ext\"
2. Edit file php.ini, tambahkan baris ini :
extension=php_sqlsrv_54_ts.dll
extension=php_pdo_sqlsrv_54_ts.dll
3. Restart Apache Service.

[Steps 3]. Setting Database Config di Codeigniter : (Contoh Settingan)
1. Edit file "application/config/database" :
$active_group = 'sqlsvr12';
$active_record = TRUE;
$db['sqlsvr12']['hostname'] = 'AXIOO-PC\SQLEXPRESS';
$db['sqlsvr12']['username'] = 'sa';
$db['sqlsvr12']['password'] = 'password';
$db['sqlsvr12']['database'] = 'db_test';
$db['sqlsvr12']['dbdriver'] = 'sqlsrv';
$db['sqlsvr12']['dbprefix'] = '';
$db['sqlsvr12']['pconnect'] = TRUE;
$db['sqlsvr12']['db_debug'] = TRUE;
$db['sqlsvr12']['cache_on'] = FALSE;
$db['sqlsvr12']['cachedir'] = '';
$db['sqlsvr12']['char_set'] = 'utf8';
$db['sqlsvr12']['dbcollat'] = 'utf8_general_ci';
$db['sqlsvr12']['swap_pre'] = '';
$db['sqlsvr12']['autoinit'] = TRUE;
$db['sqlsvr12']['stricton'] = FALSE;


2. Edit file "/system/database/drivers/sqlsrv/sqlsrv_driver.php" liat ini.
3. Done !