php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79163 SQL SELECT EXISTS fills up memory
Submitted: 2020-01-24 09:06 UTC Modified: 2020-12-16 13:49 UTC
From: kinimodmeyer at gmail dot com Assigned:
Status: Verified Package: MySQLi related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: kinimodmeyer at gmail dot com
New email:
PHP Version: OS:

 

 [2020-01-24 09:06 UTC] kinimodmeyer at gmail dot com
Description:
------------
SELECT EXISTS in the query fills up the memory. see test-script below.

Test script:
---------------
<?php
$start = microtime(true);
var_dump(memory_get_usage(true));

for ($i = 0;$i<100000;$i++) {
    $sql = 'SELECT 1
            FROM `article`
            WHERE a_nr = "'.$mysqli->real_escape_string(bin2hex(random_bytes(10))).'"
            LIMIT 1';
    $mysqli->query($sql);
}

var_dump(microtime(true) - $start, memory_get_usage(true));
$start = microtime(true);

for ($i = 0;$i<100000;$i++) {
    $sql = 'SELECT EXISTS
            (
                SELECT 1
                FROM `article`
                WHERE a_nr = "'.$mysqli->real_escape_string(bin2hex(random_bytes(10))).'"
                LIMIT 1
            )';
    $mysqli->query($sql);
}

var_dump(microtime(true) - $start, memory_get_usage(true));

Expected result:
----------------
memory1: int(4194304)
memory2: int(4194304)
momory3: int(28311552)

Actual result:
--------------
php memory fills up in the second query

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-01-24 09:47 UTC] cmb@php.net
I cannot reproduce with PHP 7.4.3-dev using mysqlnd.
 [2020-12-13 21:43 UTC] dharman@php.net
-Status: Open +Status: Verified
 [2020-12-13 21:43 UTC] dharman@php.net
I am able to reproduce also on PHP 8.0. I created simpler reproducible test case available at https://phpize.online/?phpses=20833fbdffb444d3d8b79c6b292ce7cb&sqlses=null&php_version=php7&sql_version=mysql57

This code should report 0Mb difference or almost zero. However, the memory used is increasing with each iteration. The problem seems to be caused by column names in the result set, but so far the root cause has eluded me. 
-----------------------
<?php
$mem = memory_get_usage(0);

for ($i = 0; $i < 2000; $i++) {
    $sql = 'SELECT 42 AS `'.$i.'`';
	$res = $mysqli->query($sql);
}
unset($i, $sql, $res);

var_dump(memory_get_usage(0)-$mem);
-----------------------
I can reproduce it on Windows CLI, but I do not know why I can't reproduce it using Apache.
 [2020-12-16 13:49 UTC] nikic@php.net
The problem is that mysqlnd uses interned strings for column names, so they will never get freed.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 09 11:01:28 2024 UTC