MYSQL锁表问题

时间:2013-03-18 03:55:57

标签: php mysql locking

我有一个问题,所以在大约3秒内(不连续)向我的数据库发送大约100个查询,他们读取一个表,然后根据他们读取的内容插入另一个表。

如果我没有锁定表,那么他们最终会读取错误的数据,因为它们在发送查询时已经过时了。所以我在读取和插入数据之前锁定了表,然后在完成时将其解锁。但是我的问题是,PHP页面最终会拖延一段时间甚至更长时间,但有时它无论如何都无法解锁。有谁知道为什么?没有理由为什么每个查询不应该花费超过几毫秒。

 if (!$this->MYSQL->Query("SELECT * FROM videos WHERE active = 1 LIMIT 1")) {
            $this->MYSQL->Close();
            return false;
        }
        if ($this->MYSQL->Records == 0) {
            $this->MYSQL->Close();
            return false;
        }


        $this->VIDEO = $this->MYSQL->ArrayedResults();
        $this->MYSQL->Query("LOCK TABLES sessions WRITE, accounts WRITE, videos WRITE");
        if (!$this->MYSQL->Query("SELECT * FROM accounts
WHERE id NOT IN (
SELECT account
FROM sessions WHERE video = '" . $this->VIDEO['id'] . "') LIMIT 1")) {
            $this->MYSQL->Query("UNLOCK TABLES");
            $this->MYSQL->Close();
            return false;
        }


        if ($this->MYSQL->Records == 0) {
            $this->MYSQL->Query("UNLOCK TABLES");
            $this->MYSQL->Close();

            return false;
        }

        $this->ACCOUNT = $this->MYSQL->ArrayedResults();

        if (!$this->MYSQL->Query("INSERT INTO sessions (account,address,action,video) VALUES ('" . $this->ACCOUNT['id'] . "', '" . $_SERVER["REMOTE_ADDR"] . "', '" . $this->ACTION . "', '" . $this->VIDEO['id'] . "'    )")) {
            $this->MYSQL->Query("UNLOCK TABLES");
            $this->MYSQL->Close();
            return false;
        }


        if (!$this->MYSQL->Query("SELECT COUNT(*) FROM sessions WHERE video = '" . $this->VIDEO['id'] . "'")) {
            $this->MYSQL->Query("UNLOCK TABLES");
            $this->MYSQL->Close();
            return false;
        }


        if ($this->MYSQL->Result >= $this->VIDEO['amount']) {
            $this->MYSQL->Query("UPDATE videos SET active = 0 WHERE id = '" . $this->VIDEO['id'] . "'");
        }
        $this->MYSQL->Query("UNLOCK TABLES");
        $this->MYSQL->Close();

0 个答案:

没有答案