为什么此查询中的内存消耗会随着内部指针循环进行而继续上升?如何提高效率和精益?
$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 */
答案 0 :(得分:2)
AFAIK一些mysqli实现确实保留了与列的最大大小一样多的内存。因此,即使文本字段中有1Kb,它也会保留64k,而不是1。
我不是很擅长,但你可以尝试mysqli_unbuffered_query(如果有的话)
或者,作为最佳选择 - 仅选择特定页面上所需的行和列