gpt4 book ai didi

php - 在 PHP 中使用 odbc_fetch_array 来自 SQL Azure 数据库的响应缓慢

转载 作者:行者123 更新时间:2023-12-03 00:33:10 27 4
gpt4 key购买 nike

我在 Azure 中有一个数据库。当我运行一个通过 SMSS 返回 300 条记录的查询时,我很快就得到了结果。但是,当我尝试在 php 中使用 odbc_fetch_array 获取结果时,响应时间很慢(15 - 20 秒)。如果我将结果集限制为几条记录(在 php 中),则响应速度很快。如何减少延迟?

这是我的代码:

    //CONNECTIONS  *************************************************
try {
$connect = odbc_connect(odbcname,'user','password');
$result = odbc_exec($connect,$sql);
$applicants = array();
//slow response in while loop below
while( $row = odbc_fetch_array($result) ) {
array_push($applicants,$row);
}
$err = (odbc_error() ? odbc_errormsg($connect) : '');
odbc_close($connect);
} catch (Exception $e) {
die();
}

Web 服务器是 Windows Server 2008 R2 Datacenter。odbc 驱动程序是“ODBC Driver 11 for SQL Server”。

最佳答案

您可以尝试设置连接以使用 ODBC_CURSOR

//CONNECTIONS  *************************************************
try {
$connect = odbc_connect(odbcname,'user','password', SQL_CUR_USE_ODBC); //<--------
$result = odbc_exec($connect,$sql);
$applicants = array();
//slow response in while loop below
while( $row = odbc_fetch_array($result) ) {
array_push($applicants,$row);
}
$err = (odbc_error() ? odbc_errormsg($connect) : '');
odbc_close($connect);
} catch (Exception $e) {
die();
}

但是,Microsoft 建议从 PHP 访问 SQL 数据库的方法是使用 Microsoft Drivers for PHP for SQL Server here

这里有一些由 Microsoft 提供的连接到 Azure SQL Server 的示例(未经测试的示例)。

$server = "tcp:<value of SERVER from section above>";
$user = "<value of USERNAME from section above>"@SERVER_ID;
$pwd = "password";
$db = "testdb";

$conn = sqlsrv_connect($server, array("UID"=>$user, "PWD"=>$pwd, "Database"=>$db));

if($conn === false){
die(print_r(sqlsrv_errors()));
}

$tsql = "SELECT * FROM PRODUCTS WHERE ID > ?";

$cursorType = array("Scrollable" => SQLSRV_CURSOR_KEYSET);

$params = array(1)

$getProducts = sqlsrv_query($conn, $tsql, $params, $cursorType);
if ( $getProducts === false)
die( FormatErrors( sqlsrv_errors() ) );


if(sqlsrv_has_rows($getProducts))
{

while( $row = sqlsrv_fetch_array( $getProducts, SQLSRV_FETCH_ASSOC))
{
//ADD CODE HERE
}

}

/* Free the statement and connection resources. */
sqlsrv_free_stmt( $getProducts );
sqlsrv_close( $conn );

在 Azure 网站上,您可以找到有关如何使用 php 连接到 Sql Server 的分步教程,see here

在这里您可以找到有关 Microsoft Drivers for PHP for Sql Server Link 的文档

关于php - 在 PHP 中使用 odbc_fetch_array 来自 SQL Azure 数据库的响应缓慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26476428/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com