简体   繁体   English

根据时间连接到mysql表

[英]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. 如果服务器时间在1-30秒之间连接到一个表,如果超过31-60秒连接到另一个表。

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. 我需要这个的原因是因为我不确定,但如果10,000人正在访问一个mysql表,这会大大减慢它。

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. 不确定我完全理解但是自从Unix Epoch以来,php的time()将返回秒。 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. 如果你需要在不同的表中分配负载,你应该考虑设置分区和负载均衡的mysql集群,这样就可以在数据库级别上进行干净的处理。 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 请参阅: http//php.net/manual/en/mysqli.options.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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