SQL Server 数据库同步到 MySQL

问题描述 投票:0回答:0

我有一个 SQL Server 数据库,它有一个包含大约 25,000 行的视图,这些行是按发票的行项目排列的,所以它看起来像这样。

Invoice_number  Total_Amount  Line_Item    Record
111                2000         book        23
111                2000         case        24
111                2000         binder      25
222                 500         book        26
222                 500         book        27

因此发票编号将重复该发票中的每行项目数等等。

这是托管在 SQL Server 上,我想将它复制到 xaamp 数据库,然后让它们保持同步。

这是我用来保持同步的代码,但速度非常慢。它持续 15 分钟以上。有什么我想念的吗?最有效的方法是什么?非常感谢帮助

function syncData()
{

    $serverName = "onlinehost";
    $connectionOptions = array(
        "Database" => "onlinedb",
        "Uid" => "user",
        "PWD" => "password"
    );
    $onlineTable = "onlineview";
    // Establishes the connection
    $onlineConn = sqlsrv_connect($serverName, $connectionOptions);
    if ($onlineConn === false) {
        die(print_r(sqlsrv_errors(), true));
    }



    // Local XAMPP SQL Server connection details
    $localServer = "localhost";
    $localUsername = "root";
    $localPassword = "";
    $localDatabase = "local";
    $localTable = "invoices";



    // Connect to the local XAMPP MySQL
    $localConn = mysqli_connect($localServer, $localUsername, $localPassword, $localDatabase);
    if (!$localConn) {
        die("Local XAMPP database connection failed: " . mysqli_connect_error());
    }

    // Fetch records from the online server
    $onlineQuery = "SELECT * FROM $onlineTable";
    $onlineResult = sqlsrv_query($onlineConn, $onlineQuery);
    if ($onlineResult === false) {
        die(print_r(sqlsrv_errors(), true));
    }

    // Insert new records into the local database
    $insertedRecords = 0;
    while ($row = sqlsrv_fetch_array($onlineResult, SQLSRV_FETCH_ASSOC)) {
        $record = $row['Record'];

        // Check if the record already exists in the local database
        $localQuery = "SELECT * FROM $localTable WHERE Record = '$record'";
        $localResult = mysqli_query($localConn, $localQuery);
        if (!$localResult) {
            die("Error checking existing record in local database: " . mysqli_error($localConn));
        }

        if (mysqli_num_rows($localResult) == 0) {
            // Record doesn't exist, insert it into the local database
            $columns = implode(", ", array_keys($row));
            $values = array_map(function ($value) {
                if ($value instanceof DateTime) {
                    return "'" . $value->format('Y-m-d H:i:s') . "'";
                }
                return "'" . $value . "'";
            }, $row);
            $values = implode(", ", $values);
            $insertQuery = "INSERT INTO $localTable ($columns) VALUES ($values)";
            $insertResult = mysqli_query($localConn, $insertQuery);

            if (!$insertResult) {
                die("Error inserting record into local database: " . mysqli_error($localConn));
            }

            $insertedRecords++;
        }
    }

    // Close connections
    sqlsrv_close($onlineConn);
    mysqli_close($localConn);

    // Return the number of inserted records
    return $insertedRecords;
}

// Usage:
syncData();


?>

php sql sql-server mysqli relational-database
© www.soinside.com 2019 - 2024. All rights reserved.