views:

27

answers:

1

Why is the memory consumption in this query continuing to rise as the internal pointer progresses through loop? How to make this more efficient and lean?

$link = mysqli_connect(...);
$result = mysqli_query($link,$query); // 403,268 rows in result set
while ($row = mysqli_fetch_row($result)) 
{
    // print time, (get memory usage), -- row number
}
mysqli_free_result();
mysqli_close($link);
/*
06:55:25 (1240336) -- Run query
06:55:26 (39958736) -- Query finished
06:55:26 (39958784) -- Begin loop
06:55:26 (39960688) -- Row 0
06:55:26 (45240712) -- Row 10000
06:55:26 (50520712) -- Row 20000
06:55:26 (55800712) -- Row 30000
06:55:26 (61080712) -- Row 40000
06:55:26 (66360712) -- Row 50000
06:55:26 (71640712) -- Row 60000
06:55:26 (76920712) -- Row 70000
06:55:26 (82200712) -- Row 80000
06:55:26 (87480712) -- Row 90000
06:55:26 (92760712) -- Row 100000
06:55:26 (98040712) -- Row 110000
06:55:26 (103320712) -- Row 120000
06:55:26 (108600712) -- Row 130000
06:55:26 (113880712) -- Row 140000
06:55:26 (119160712) -- Row 150000
06:55:26 (124440712) -- Row 160000
06:55:26 (129720712) -- Row 170000
06:55:27 (135000712) -- Row 180000
06:55:27 (140280712) -- Row 190000
06:55:27 (145560712) -- Row 200000
06:55:27 (150840712) -- Row 210000
06:55:27 (156120712) -- Row 220000
06:55:27 (161400712) -- Row 230000
06:55:27 (166680712) -- Row 240000
06:55:27 (171960712) -- Row 250000
06:55:27 (177240712) -- Row 260000
06:55:27 (182520712) -- Row 270000
06:55:27 (187800712) -- Row 280000
06:55:27 (193080712) -- Row 290000
06:55:27 (198360712) -- Row 300000
06:55:27 (203640712) -- Row 310000
06:55:27 (208920712) -- Row 320000
06:55:27 (214200712) -- Row 330000
06:55:27 (219480712) -- Row 340000
06:55:27 (224760712) -- Row 350000
06:55:27 (230040712) -- Row 360000
06:55:27 (235320712) -- Row 370000
06:55:27 (240600712) -- Row 380000
06:55:27 (245880712) -- Row 390000
06:55:27 (251160712) -- Row 400000
06:55:27 (252884360) -- End loop
06:55:27 (1241264) -- Free */
A: 

AFAIK some of mysqli implementations do reserve as much memory as column's max size. So, even if you have 1Kb in the text field, it will reserve 64k, not 1.

I'm not so good at it, but you can try mysqli_unbuffered_query (if there is any)

Or, as the best choice - select only rows and columns that you need on the particular page

Col. Shrapnel
<pre>I replaced:$result = mysqli_query($link,$query);with these two:mysqli_real_query($link,$query);$result = mysql_use_result($link);and this fixed my memory issue. Thanks for the tip.</pre>
Poe
@Poe glad you solve it :) They use a ``backticks`` to format the code here
Col. Shrapnel