Reputation: 377
I have a very simple MySQL table that is running extremely slow, and I cannot figure out why.
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!!
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.
**UPDATE...Repairing the database and optimizing helped with the select. 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:
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!
Upvotes: 2
Views: 196
Reputation: 425
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. Second, you try to recreate index on the table. Try check table, repair table commands on MySql.
Upvotes: 1