简体   繁体   English

mysql表很慢

[英]Very slow mysql table

I have a very simple MySQL table that is running extremely slow, and I cannot figure out why. 我有一个非常简单的MySQL表,运行速度非常慢,我不知道为什么。

The structure is: 结构为:

id               int(11)        (Primary Key)        
username         varchar(64)    (Index)              
password         varchar(64)                 
pws              varchar(16)                 
default_password varchar(32)                 
session_id       varchar(64)                 
license_number   int(10)        (Index)          
contact_name     varchar(64)                 
phone            varchar(16)                 
first_login_date datetime                
last_login_date  datetime                
status           tinyint(4)              
test_mode        tinyint(4)              
email            varchar(64)                 
email_verified   tinyint(4)

There are only 80 something rows in the table, yet the following query took 67 seconds !! 表中只有80多个行,但是以下查询花费了67秒

SELECT * FROM `users` ORDER BY `username` ASC

Pretty much any operation on this table is outrageously slow. 该表上的几乎所有操作都极其缓慢。 What am I doing wrong, or where should I start digging? 我在做什么错,还是应该从哪里开始挖掘?

As a side not, I have another, more complicated table, with 20,000 rows, that runs very fast. 顺便说一句,我还有另一个更复杂的表,它具有20,000行,运行速度非常快。

**UPDATE...Repairing the database and optimizing helped with the select. ** UPDATE ...修复数据库并优化选择过程。 It seems updates are extremely slow though, and then slowing down selects... 似乎更新速度非常慢,然后放慢选择速度...

$this->db->update( "UPDATE users SET session_id='' WHERE id=:userid", array( ":userid" => $this->getUserId() ) );

Here is the PHP Database code I am using: 这是我正在使用的PHP数据库代码:

class Database extends PDO
{

    public function __construct($DB_TYPE, $DB_HOST, $DB_NAME, $DB_USER, $DB_PASS)
    {

        $this->handleDB = new PDO($DB_TYPE.':host='.$DB_HOST.';dbname='.$DB_NAME, $DB_USER, $DB_PASS);
        $this->handleDB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    }

public function update($sql, $array = array())
    {
        $sth = $this->handleDB->prepare($sql);
        foreach ($array as $key => $value) {


            if(is_int($value)){
                $sth->bindValue("$key", $value, PDO::PARAM_INT);
            }else{
                $sth->bindValue("$key", $value, PDO::PARAM_STR);
            }



        }

        $sth->execute();

        return $sth->rowCount();
    }
  }
}

Thanks! 谢谢!

You can check two things, if you are running queries from Client machine check your network speed to the server. 您可以检查两件事,如果要从客户端计算机运行查询,请检查到服务器的网络速度。 If query is running slow on Server you can check in Slow query log on Mysql server. 如果查询在服务器上运行缓慢,则可以在Mysql服务器上签入慢查询日志。 Second, you try to recreate index on the table. 其次,您尝试在表上重新创建索引。 Try check table, repair table commands on MySql. 尝试在MySql上检查表,修复表命令。

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

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