gorm: The performance for update is bad!

I test a update as follow:

    start := time.Now()
    if val, ok := value.(*data); ok {
        ps.Model(val.Model).Updates(val.Data)
    }
    fmt.Printf("111111111111111111The time save take:%v\n", time.Since(start))

The sql take just 1.83 ms.

[2015-12-28 21:48:58]  [1.83ms]  UPDATE `profile` SET `nick_name` = 'xxx', `exchanged_times` = '0', `exchanged_for_other_currency` = '0', `gem` = '0', `currency` = '100', `contact` = '', `purchased_for_othertimes` = '0', `max_gem` = '0', `signature` = '', `is_active` = 'true', `charged_times` = '0', `purchased_gem` = '0', `exchanged_for_othertimes` = '0', `purchased_times` = '0', `gender` = '0', `charged_for_othertimes` = '0', `charged_for_other_money` = '0', `charged_money` = '0', `exchanged_currency` = '0', `purchased_for_other_gem` = '0', `max_currency` = '100', `privilege_end_time` = '1984-08-07T07:45:00+08:00', `taked_currency` = '0', `avatar_url` = '5', `privilege_level` = '0', `bank_currency` = '0', `created_at` = '2015-12-28T21:48:57+08:00'  WHERE (`player_id` = '1024')

but the ps.Model(val.Model).Updates(val.Data) take 41ms.

111111111111111111The time save take:41.247981ms

why? and how to refine? @jinzhu

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 19 (6 by maintainers)

Most upvoted comments

@jinzhu I test on ubuntu again. there is no changes! and i update the test script above! And i found that

i: performance for insert is not good at the same.
ii:every insert or update operation will generate two sql command, is it okay?
(/home/xxx/test.go:103) 
[2016-02-24 09:40:35]  [0.70ms]  INSERT INTO `test_table` (`signature`,`created_at`,`avatar_url`,`nick_name`,`player_id`,`max_currency`,`currency`,`contact`) VALUES ('','2016-02-24T09:40:35+08:00','5','jian','1024','100','100','') 

(/home/xxx/test.go:103) 
[2016-02-24 09:40:35]  [1.01ms]  SELECT  max_gem, bank_currency, taked_currency, gender, charged_times, purchased_gem, exchanged_times, is_active, charged_money, purchased_times, charged_for_othertimes, charged_for_other_money, purchased_for_othertimes, purchased_for_other_gem, exchanged_for_othertimes, exchanged_for_other_currency, gem, exchanged_currency, privilege_level, privilege_end_time FROM `test_table`  WHERE (`player_id` = '1024') ORDER BY `test_table`.`player_id` ASC LIMIT 1
========================The time insert take:43.81883ms

(/home/xxx/test.go:109) 
[2016-02-24 09:40:35]  [2.05ms]  SELECT  * FROM `test_table`  WHERE (`player_id` = '1024') ORDER BY `test_table`.`player_id` ASC LIMIT 1

(/home/xxx/test.go:81) 
[2016-02-24 09:40:35]  [0.61ms]  UPDATE `test_table` SET `is_active` = 'true', `charged_for_other_money` = '0', `exchanged_for_othertimes` = '0', `avatar_url` = '5', `purchased_gem` = '0', `max_currency` = '100', `signature` = '', `exchanged_for_other_currency` = '0', `gem` = '0', `nick_name` = 'jian zhao', `charged_money` = '0', `purchased_for_othertimes` = '0', `created_at` = '2016-02-24T09:40:35+08:00', `purchased_times` = '0', `charged_for_othertimes` = '0', `exchanged_times` = '0', `exchanged_currency` = '0', `max_gem` = '0', `contact` = '', `gender` = '0', `privilege_level` = '0', `privilege_end_time` = '1984-08-07T07:45:00+08:00', `charged_times` = '0', `purchased_for_other_gem` = '0', `currency` = '100', `bank_currency` = '0', `taked_currency` = '0'  WHERE (`player_id` = '1024')
========================The time save take:42.139479ms

problem is db.LogMode(true),

func (s *DB) print(v …interface{}) { s.logger.Print(v…) } cost too much time @jinzhu @zj8487 ,db.LogMode should be set false in produciton mode

@zj8487 @cristian-sima

Had same problem with second SQL query (SELECT after INSERT). Cured by the following:

gorm.DefaultCallback.Create().Remove("gorm:force_reload_after_create")