简体   繁体   中英

connected to mysql table depending on time

If the server time is between 1-30 second connect to one table and if more than 31-60 seconds connect to another table.

reason why i need this is because im not sure but if 10,000 people are accessing a single mysql table will this slow it down dramatically.

my code is below

 <?php
    header('Content-type: application/json');

    $server = "localhost";
    $username = "root1";
    $password = "root1";
    $database = "test";

    $con = mysql_connect($server, $username, $password) or die ("Could not connect: " .  mysql_error());
    mysql_select_db($database, $con);

    $sql = "SELECT id, name FROM post ORDER BY id";
    $result = mysql_query($sql) or die ("Query error: " . mysql_error());

    $records = array();

    while($row = mysql_fetch_assoc($result)) {
        $records[] = $row;
    }

    mysql_close($con);

    echo $_GET['jsoncallback'] . '(' . json_encode($records) . ');';
    ?>

Not sure I completely understand but php's time() will return seconds since the Unix Epoch. If its an even number, connect it to one table and otherwise another table... this will effectively split traffic ...

  <?php
    $time = time();
    $tableToAccess = ($time % 2 == 0) ? 'table_one' : 'table_two';

    $db = new mysqli('localhost','username','password','database');

    $result = $db->query("SELECT * FROM $tableToAccess");

  ?>

That being said, that database engine being run by multiple users is what will slow you down, not simultaneous table access

I would suggest not to do load balancing at application code level. if you need to distribute load across different tables, you should look into setting up partitioning & a load-balanced mysql cluster, so this can be cleanly handled at the database level. here are few links to guide you further:

You can set the connection timeout as one of the options. See: http://php.net/manual/en/mysqli.options.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM