简体   繁体   English

MSSQL使用PHP查询速度较慢

[英]MSSQL slow with PHP Query

I have a PHP page that's slow whenever it goes to query a MSSQL database. 每当查询MSSQL数据库时,我都有一个很慢的PHP页面。 I'm using the latest PHP and the standard SQL odbc drivers to the connect to the database. 我正在使用最新的PHP和标准的SQL odbc驱动程序来连接数据库。 When accessing the page My sql profiler goes crazy.The query is displayed then 访问页面My sql profiler时出现问题。然后显示查询

exec sp_cursorfetch 180150003,2,1,1
go

performs about 240 times before finally 大约执行了240次

exec sp_cursorclose 180150003
go 

occurs and the page is displayed. 发生并显示页面。

跟踪日志截图

This is the query causing the headache: 这是引起头痛的查询:

    <?php
    $con = odbc_connect('Hesk','Trace_user','*******');
    $Assets = odbc_exec($con, "SELECT  AssetName,           AssetID From viewAssets ORDER BY [AssetName];");
?>
<table Border ="0"   width="100%">
<tr>
<td style="text-align:right" width="150"><?php echo $hesklang   ['asset']; ?>: <font class="important"></font>
</td>
<td width = "80%"><select name ="asset">
<option value=""></option>
<?php
    while ($row = odbc_fetch_array($Assets))
    {
    echo '<option value="' . $row['AssetID'] . '"' .            (($_SESSION['c_asset'] == $row['AssetID']) ? '              selected="selected"' : '')
    . '>' . $row['AssetName']. '</option>';
    }
    odbc_close($con);
?>
</select></td>
</tr>
</table>

Any idea how my code is causing this performance holdup? 知道我的代码如何导致这种性能下降吗?

It looks like the odbc_exec functions use cursors under the hood. 似乎odbc_exec函数在后台使用了游标。 I'm not entirely sure who thought that was a good idea, but it's not. 我不完全确定谁认为这是个好主意,但事实并非如此。 Cursors are notoriously bad performers and generally a really bad idea unless you simply have no other way. 众所周知,游标的性能很差,除非您根本没有其他选择,否则通常是一个非常糟糕的主意。

One option is to change your odbc_connect call to have the SQL_CUR_USE_ODBC flag. 一种选择是将odbc_connect调用更改为具有SQL_CUR_USE_ODBC标志。 A better option is to use the native drivers. 更好的选择是使用本机驱动程序。

http://cct.me.ntut.edu.tw/ccteducation/chchting/aiahtm/computer/phphelp/function.odbc-connect.php.htm http://cct.me.ntut.edu.tw/ccteducation/chchting/aiahtm/computer/phphelp/function.odbc-connect.php.htm

Try to measure time where is problem in connection or in query execution If problem is in connection then change MSSQL name to IP 尝试测量连接或查询执行中出现问题的时间如果连接中存在问题,则将MSSQL名称更改为IP

$con = odbc_connect('Hesk','Trace_user','*******');

For some reason my linux box was slow on resolving. 由于某种原因,我的linux盒解析速度很慢。

$con = odbc_connect('192.168.10.10','Trace_user','*******');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM