如何解决共享主机的MySQL高使用率问题。我的网站的托管由于MYSQL高使用率而被暂停

时间:2019-02-16 15:00:02

标签: mysql hosting long-polling

今天,我的网站托管突然被我的服务提供商暂停了。我需要帮助来查找实际导致问题的原因。他们向我发送了以下共享的日志。

暂停原因:MYSQL使用率高 这是您的MYSQL高使用率日志;

ncgtjnoe_begreen
localhost
ncgtjnoe_crmbegreen
Sleep
4
NULL

1663045
ncgtjnoe_begreen
localhost
ncgtjnoe_crmbegreen
Sleep
4
NULL

1663674
ncgtjnoe_begreen
localhost
ncgtjnoe_crmbegreen
Sleep
4
NULL

1664163
ncgtjnoe_begreen
localhost
ncgtjnoe_crmbegreen
Sleep
4
NULL

1664518
ncgtjnoe_begreen
localhost
ncgtjnoe_crmbegreen
Sleep
4
NULL

1665183
ncgtjnoe_begreen
localhost
ncgtjnoe_crmbegreen
Sleep
4
NULL

1675598
ncgtjnoe_begreen
localhost
ncgtjnoe_crmbegreen
Sleep
4
NULL

1676464
ncgtjnoe_begreen
localhost
ncgtjnoe_crmbegreen
Sleep
4
NULL

1676932
ncgtjnoe_begreen
localhost
ncgtjnoe_crmbegreen
Sleep
4
NULL

1677146
ncgtjnoe_begreen
localhost
ncgtjnoe_crmbegreen
Sleep
4
NULL

1708438
ncgtjnoe_begreen
localhost
ncgtjnoe_crmbegreen
Sleep
4
NULL

1789011
ncgtjnoe_begreen
localhost
ncgtjnoe_crmbegreen
Sleep
4
NULL

1800685
ncgtjnoe_begreen
localhost
ncgtjnoe_crmbegreen
Sleep
4
NULL

1816150
ncgtjnoe_begreen
localhost
ncgtjnoe_crmbegreen
Sleep
4
NULL

1824685
ijnxqsbq_boltlgu
localhost
ijnxqsbq_boltlgu
Sleep
95
NULL

1825121
ijnxqsbq_boltlgu
localhost
ijnxqsbq_boltlgu
Sleep
18
NULL

1825190
ijnxqsbq_boltlgu
localhost
ijnxqsbq_boltlgu
Sleep
4
NULL

1825191
ijnxqsbq_boltlgu
localhost
ijnxqsbq_boltlgu
Sleep
3
NULL

是什么导致高使用率,我怀疑我在使用长池进行推送通知,这导致了问题。

这是网站每一页上使用的通知的代码。

      <script type="text/javascript">

    function poll() {
        var ajax = new XMLHttpRequest();
        ajax.onreadystatechange = function() {
            if (this.readyState === 4 && this.status === 200) {
                if (this.status === 200) {
                    try {
                        var json = JSON.parse(this.responseText);                       //alert('changed');
                    } catch {
                        setTimeout(poll,55000);return;
                    }


                    if (json.status !== true) {
                        alert(json.error);return;
                    }

                    var data = json.data;
                    for (var i = 0, len = data.length; i < len; i++) {
                        var x = data[i];
                        var body = 'Task: ' + x.content + '\nDate: ' + x.time + '\nID: ' + x.id;
                        notifyMe(body,x.id);
                    }




                    setTimeout(poll,55000);
                } else {
                    setTimeout(poll,55000);
                }

            }
        }
        ajax.open('GET', 'long-polling.php', true);
        ajax.send();


    }

    setTimeout(poll,60000);

//var body1 = "This is the body of the notification asargument";

function notifyMe(body1,id) {
  // Let's check if the browser supports notifications
  if (!("Notification" in window)) {
    alert("This browser does not support desktop notification");
  }

  // Let's check if the user is okay to get some notification
  else if (Notification.permission === "granted") {
    // If it's okay let's create a notification
  var options = {
        body: body1,
        icon: "icon.png",
        dir : "ltr"
    };
  var notification = new Notification("Task Alert",options);
    notification.addEventListener("click", function() {
    window.open('opentask.php?id='+id, '_blank');
    window.focus();
    this.close();
    // Do something cool

}, {once : true});

  }

  // Otherwise, we need to ask the user for permission
  // Note, Chrome does not implement the permission static property
  // So we have to check for NOT 'denied' instead of 'default'
  else if (Notification.permission !== 'denied') {
    Notification.requestPermission(function (permission) {
      // Whatever the user answers, we make sure we store the information
      if (!('permission' in Notification)) {
        Notification.permission = permission;
      }

      // If the user is okay, let's create a notification
      if (permission === "granted") {
        var options = {
              body: "Notifications enabled",
              icon: "icon.png",
              dir : "ltr"
          };
        var notification = new Notification("Notification",options);
      }
    });
  }

  // At last, if the user already denied any notification, and you
  // want to be respectful there is no need to bother them any more.
}
</script> 

这是long-polling.php文件的代码

<?php

//session_start();
session_write_close();
ignore_user_abort(false);
set_time_limit(40);

try {

    include_once '../config.php';

    if ($_COOKIE['notifyEnabled']==0) {
//      echo "<script>alert('Helow');</script>";
        $user = $_COOKIE['loggedinid'];

        // add user to the database
        $mysqli -> query("INSERT INTO tasks_update_track VALUES ($user, 0)");
        // send to the browser
        setcookie('notifyEnabled', 1);
        $_COOKIE['notifyEnabled']=1;
        // first request does not do anything than creating the cookie
        exit();
    }

    // get the user value 
    $user = $_COOKIE['loggedinid'];

    while (true) {

        // select new rows
        $result = $mysqli -> query("SELECT t.id, t.title, t.description , t.reminder FROM tasks t INNER JOIN tasks_update_track ud ON ud.last_sent_id < t.id WHERE ud.user_id = $user AND t.responsibleperson=$user ORDER BY t.id");

        // check whether there were new rows in above query
        if ($result && $result -> num_rows) {
            //if yes, makes the output
            $output = [];

            // this is used to update the db_user_data table at last. As rows are ordered by t.id in ascending order in above query, last row has the last Id
            $lastId = 0;
            foreach ($result as $row) {
                $output[] = [
                    'content' => $row['title'],
                    'id' => $row['id'], 
                    'time' => $row['reminder']];

                $lastId = $row['id'];
            }

            // update the table and set last_sent_id to the last sent row id of other table.
            $mysqli -> query("UPDATE tasks_update_track SET last_sent_id = $lastId WHERE user_id = $user");


            echo json_encode([
                'status' => true,
                'data' => $output
            ]);
            exit;
        }



        // db queries are heavy. So 2 seconds
        sleep(5);
    }

} catch (Exception $e) {

    exit(
        json_encode(
            array (
                'status' => false,
                'error' => $e -> getMessage()
            )
        )
    );

}

0 个答案:

没有答案