r/PHPhelp • u/rosen380 • Dec 19 '24
Solved Performance issue using PHP to get data from SQL Server
I have a query that if I run in in SSMS takes about 6 seconds to populate 530k records in the grid. If I export to CSV, it takes another 4s and I have a 37.2MB file.
If I do it in Excel, similar results. About 9 seconds to populate the cells and another 3s if I choose to save it as a CSV (resulting in an identical 37.2MB file).
When I do it with PHP the whole process is ~150s (and I'm not even displaying the raw data in browser, which the other two methods essentially are). The output is another 37.2MB file.
I added in some timers to see where the time is going.
$dt1 = microtime(true);
$objQuery = sqlsrv_query($conn, $query);
$dt2 = microtime(true);
$dt3 = 0;
while ($row = sqlsrv_fetch_array($objQuery, SQLSRV_FETCH_ASSOC))
{
$dt3 = $dt3 - microtime(true);
fputcsv($f, $row, $delimiter);
$dt3 = $dt3 + microtime(true);
}
$dt4 = microtime(true);Between $dt1 and $dt2 is <0.1s, so I imagine the query is executing quickly...?
$dt3 is summing up just the time spent writing the CSV and that was 6.6s, which feels reasonably in line with Excel and SSMS.
The difference between $dt4 and $dt2, less $dt3 would then be the amount of time it spent iterating through the ~500k rows and bringing the data over and that is taking nearly all of the time, 143 seconds in this case.
Same issue is pretty universal for all queries I use, perhaps reasonably proportionate to the amount of rows/data.
And same issue if I have to display the data rather than write to CSV (or have it do both).
I guess my question is -- is there something I can do about that extra 2+ minutes for this particular query (worse for some larger ones)? I'd certainly rather the users get the ~10s experience that I can bypassing PHP than the 2.5 minute experience they are getting with PHP.
One thought I had, while writing this, was maybe server paths?
For SSMS and Excel, I guess it is a "direct" connection between the database server and my local machine. With PHP I suppose there is an extra server in the middle, local to PHP server to database server and back -- is that a likely cause of the extra time?
If so, if my IT team could move the PHP server to be in the same datacenter (or even same box) as SQL Server, would that clear up this performance issue?
4
u/NumberZoo Dec 19 '24
Looks like writing to disk (fputcsv) is slow. There are a couple potential solutions on this page: https://stackoverflow.com/questions/46451299/how-to-write-multiple-rows-arrays-to-csv-file-in-one-attempt-using-php
2
u/colshrapnel Dec 19 '24
And how long it takes if you remove the fputcsv part?
1
u/rosen380 Dec 19 '24
I marked this one with the solved flag.
Thanks everyone for your input -- this has literally been an issue for like 10 years that every once in a while I'd get annoyed and spend the better part of a day trying to figure out what is going on and it's always been fruitless until now :)
1
u/tampacraig Dec 21 '24
Fetch the rows all at once, not one by one, the network overhead is killing you.
https://learn.microsoft.com/en-us/sql/connect/php/pdostatement-fetchall?view=sql-server-ver16
0
u/[deleted] Dec 19 '24
[deleted] — view deleted comment