Your Question
Having .Order() function call slows down query execution by 1000x time
We have the code that is something like:
record := Record{}
tx := db.Model(record).
Where("abc1=? and abc2=? and abc3=?", aid, cid, did).Order("abc desc").Limit(1)
err := tx.Find(&record).Error
this query results following log:
[4970.976ms] [rows:1] SELECT * FROM "record" WHERE abc1='2b8fc287-c6be-495b-837e-9bdac6781b9c' and abc2='76e6a1d7-c20c-491f-8ca5-b4136d00e7e8' and abc3='d34e7e1f-182a-4fc6-9b07-2bfdd6a60a09' ORDER BY abc desc LIMIT 1
copying and running this query directly in the db takes 22ms. DB has about 200 records, so it shouldn't take long even if there is no index in the etc.
If I remove the .Order("abc desc") from the tx builder it is fast, maybe 25-30ms.
The document you expected this should be explained
Expected answer
What is the overhead here that is causing gorm to take long for .Order("abc desc").
Am I missing something?
Thanks for your help!
Comment From: a631807682
The currently tracked time is not the actual execution time of SQL, but the time returned by the complete API, including the blocking of debugging breakpoints and the time superposition caused by nested execution. We are currently discussing how to change it.
https://github.com/go-gorm/gorm/issues/6292
Comment From: elbek
I think we can close this, it was due to row locking in the db and nothing to do with gorm.