I'm using sqlsrv (php_pdo_sqlsrv_55_ts.dll and php_sqlsrv_55_ts.dll) in PHP (5.5.12, CLI version on Windows 8.1) to connect to a SQL Server 2012 over a VPN tunnel. But the transfer rate for large result sets is very slow.
Testing SSMS on the same PC over the same VPN tunnel for this query (query is just for testing purposes, I also tried other queries with large data sets and all showed the same performance issues):
SELECT *
FROM [Data].[dbo].[Logins]
WHERE date >= '2014-01-27 00:00:00.000' AND date < '2014-01-29 00:00:00.000'
returns about 100,000 rows in 4 seconds. Checking the transfer rate on my firewall/VPN shows 2,500 KB/s (on a 100mbit network) while the query is running.
Using PHP:
sqlsrv_configure('ClientBufferMaxKBSize', 1024*1024);
$dbconnect = "SERVER\\HERE";
$dbconinfo = array("UID" => "user", "PWD" => "pass", "Database" => "Data")
$conn = sqlsrv_connect( $dbconnect, $dbconinfo);
$sql = "
SELECT *
FROM [dbo].[Logins]
WHERE date >= '2014-01-27 00:00:00.000' AND date < '2014-01-29 00:00:00.000'
";
$options = array();
$options["Scrollable"] = SQLSRV_CURSOR_CLIENT_BUFFERED;
$options["QueryTimeout"] = 30000;
$stmt = sqlsrv_query( $conn, $sql, array(), $options);
runs 40 seconds and the firewall/VPN shows less than 150 KB/s while the query is running. Task-Manager shows around 5% CPU load for the script.
I used SQLSRV_CURSOR_CLIENT_BUFFERED just for testing, because it reads the result in its own buffer without any further PHP code. Fetching each set of data without buffering is just a little bit slower (around 45 seconds).
I also tried a PDO version which lead to the same result.
ConnectionPooling 0 or 1 didn't make any difference either.
Changing server name to DNS vs. IP adress also made no difference.
LogSubsystems -1 and LogSeverity -1 did not show any issues or anything helpful at all.
I even used Wireshark to watch the network traffic but couldn't find any big differences between PHP and SSMS versions. But I don't know too much about networking layers.
So the CPU load is low, there is no HDD use at all, the network transfer rate is extremely slow and the SQL server would be capable to deliver the results very much faster. And it actually is a lot faster with SSMS on the same PC and network connection. Why is sqlsrv so very slow?
Any ideas what might be the issue or what I could try to speed up PHP/sqlsrv would be greatly appreciated.