简体   繁体   中英

MYSQL: Making an update query faster?

I have a simple users tables:

userID | credits | name | etc | etc1 | etc2

I do a simple query:

UPDATE users set credits = (credits - 10) where userid = 9

This table has 2000 rows. Why is this query taking 0.16 seconds and how can I make it faster?

userid is the primary key and therefore already has a index on it. So I can't think why this query could possibly be slow. Especially seeing as it's the ONLY query being executed, ONCE, on a server with 16GB of RAM and a killer processor. There is literally nothing else on the server that could be hogging any resources.

Any ideas?

EDIT

This is how I time it:

private function slowQueryEvaluator($startTime, $endTime, $identifier) {

    $MAX_TIME = 0.1;

    $IP = Controller::getRealIpAddress();
    $userID = -1;
    if (isset(YII::app()->user->id)) {
      $userID = YII::app()->user->id;
    }
    $seconds = $endTime - $startTime;
    if ($seconds > $MAX_TIME ) {
      YII::log($IP.' - '.$userID.' - '.$seconds.' seconds - '.$identifier);
    }

  }

credits is an INT.

And here is an export of the create of the users table:

CREATE TABLE IF NOT EXISTS `users` (
  `userid` int(11) NOT NULL AUTO_INCREMENT,
  `cell_nr` varchar(12) NOT NULL,
  `status` varchar(20) NOT NULL DEFAULT 'INACTIVE',
  `full_name` varchar(255) NOT NULL,
  `public_name` varchar(20) NOT NULL,
  `credits` int(11) NOT NULL DEFAULT '0',
  `national_id` varchar(20) NOT NULL,
  `email_address` varchar(255) NOT NULL,
  `OTP` int(5) NOT NULL,
  `OTP_count` int(11) NOT NULL DEFAULT '0',
  `password` char(32) NOT NULL,
  `created` int(11) NOT NULL,
  PRIMARY KEY (`userid`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

您的数据库引擎提供了表锁定或行级别锁定,这可能会改变速度并创建存储过程,并且仅传递参数可能更快,但我没有尝试过,您可以尝试批处理crud。例如20条sql执行是一次提交。尝试在存在的字符串旁边添加另一个字符串索引

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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