Home >Database >Mysql Tutorial >How can I efficiently split and execute large MySQL files within a PHP environment without overloading memory?

How can I efficiently split and execute large MySQL files within a PHP environment without overloading memory?

DDD
DDDOriginal
2024-10-29 10:40:29829browse

How can I efficiently split and execute large MySQL files within a PHP environment without overloading memory?

Importing MySQL Files in PHP: Splitting Queries Effectively

When importing large MySQL files into a PHP-powered website, it's crucial to handle queries efficiently. Without access to the MySQL command line, splitting a file into single queries becomes essential.

Memory-Friendly Solution

The following function splits a large SQL file into individual queries without requiring the entire file to be loaded into memory:

<code class="php">function SplitSQL($file, $delimiter = ';')
{
    set_time_limit(0);

    if (is_file($file) === true)
    {
        $file = fopen($file, 'r');

        if (is_resource($file) === true)
        {
            $query = array();

            while (feof($file) === false)
            {
                $query[] = fgets($file);

                if (preg_match('~' . preg_quote($delimiter, '~') . '\s*$~iS', end($query)) === 1)
                {
                    $query = trim(implode('', $query));

                    if (mysql_query($query) === false)
                    {
                        echo '<h3>ERROR: ' . $query . '</h3>' . "\n";
                    }

                    else
                    {
                        echo '<h3>SUCCESS: ' . $query . '</h3>' . "\n";
                    }

                    while (ob_get_level() > 0)
                    {
                        ob_end_flush();
                    }

                    flush();
                }

                if (is_string($query) === true)
                {
                    $query = array();
                }
            }

            return fclose($file);
        }
    }

    return false;
}</code>

This function addresses common gotchas such as field delimiters and line breaks in memo fields.

Test Data and Output

To demonstrate the function's effectiveness, consider the following test data:

<code class="sql">CREATE TABLE IF NOT EXISTS "test" (
    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
    "name" TEXT,
    "description" TEXT
);

BEGIN;
    INSERT INTO "test" ("name", "description")
    VALUES (";;;", "something for you mind; body; soul");
COMMIT;

UPDATE "test"
    SET "name" = "; "
    WHERE "id" = 1;</code>

Executing the SplitSQL function on this data produces the following output:

SUCCESS: CREATE TABLE IF NOT EXISTS "test" ( "id" INTEGER PRIMARY KEY AUTOINCREMENT, "name" TEXT, "description" TEXT );
SUCCESS: BEGIN;
SUCCESS: INSERT INTO "test" ("name", "description") VALUES (";;;", "something for you mind; body; soul");
SUCCESS: COMMIT;
SUCCESS: UPDATE "test" SET "name" = "; " WHERE "id" = 1;

This demonstrates the reliability of the function in splitting queries accurately.

The above is the detailed content of How can I efficiently split and execute large MySQL files within a PHP environment without overloading memory?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn