最近公司的一个项目使用了oracle数据库,使用‘think-oracle-master’扩展驱动 ,用PDO的方式连接,会使用oracle的共享池,导致数据加载慢,刷新5-6次后才能正常访问。
后来改用原生oci_connect 方式,不经过oracle的共享池,加载快了很多。
求助:
thinkphp5.0 如果我全局想使用原生oci_connect 方式,需要怎么配置?或者做些什么样的修改?
单例代码如下:
<?php
namespace app\index\controller;
use think\Db;
class Test extends \think\Controller {
public function test(){
$account = '45010218北仑大道'; // 测试数据 SELECT * FROM users where ACCOUNT='45010218北仑大道'
// 1). 使用 pdo方式调用数据
$t1 = microtime(1);
$user = Db::table('users')->where("ACCOUNT='$account'")->find();
echo '<pre><span style="color:red;">pdo方式查询结果:</span> ';print_r( $user );echo '</pre>';
dump( microtime(1) - $t1 );
// 2)。直接使用 oci 方式 读取oracle数据:
$t1 = microtime(1);
$res = $this->oci_query( "SELECT * FROM users where ACCOUNT='{$account}' " );
echo '<pre><span style="color:red;">原生oci_execute方式:</span> ';print_r( $res );echo '</pre>';
dump( microtime(1) - $t1 );
}
public function oci_query( $sql )
{
// 2)。直接使用 oci 方式 读取oracle数据:
$dbstr ="(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST =192.168.1.109)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
(INSTANCE_NAME = orcl)))";
$conn = oci_connect('yzxt','yzxt',$dbstr,'utf8');
if (!$conn) {
$e = oci_error();
print htmlentities($e['message']);
exit;
}
$statement = oci_parse( $conn, $sql );
oci_execute($statement);
$res = [];
while ($row = oci_fetch_array($statement, OCI_ASSOC+OCI_RETURN_NULLS)) {
$res[] = $row;
}
oci_free_statement($statement);
oci_close($conn);
return $res;
}
}性能对比图:pdo方式查询结果: Array
(
[ID] => 200664
[ACCOUNT] => 45010218北仑大道
[NAME] => 北仑大道
[PASSWORD] => 855e48299f6cbbbbba821871175dab7b
[SALT] => FIOcAq
[IID] => 53819901
[SEX] => 1
[PHONE] => 东兴市北仑大道营业所
[CREATE_TIME] => 1526353912
[LOGIN_TIME] => 0
[STATUS] => 0
[PID] => 53816100
[IS_INITIAL] => 0
[NUMROW] => 1
)
D:\phpStudy\WWW\yzxt\thinkphp\library\think\Debug.php:193:float 1.2720000743866
原生oci_execute方式: Array
(
[0] => Array
(
[ID] => 200664
[ACCOUNT] => 45010218北仑大道
[NAME] => 北仑大道
[PASSWORD] => 855e48299f6cbbbbba821871175dab7b
[SALT] => FIOcAq
[IID] => 53819901
[SEX] => 1
[PHONE] => 东兴市北仑大道营业所
[CREATE_TIME] => 1526353912
[LOGIN_TIME] => 0
[STATUS] => 0
[PID] => 53816100
[IS_INITIAL] => 0
)
)
D:\phpStudy\WWW\yzxt\thinkphp\library\think\Debug.php:193:float 0.043999910354614 最佳答案