-
-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SQL very slow while query from a huge database table #7213
Comments
@rts-gordon You can just open profile in your mysql server and check what's going on. You can use force index to make it use the right index you want, just don't rely on the SQL optimizer of MySQL, it's stupid in some scenarios(especially when you have lots of records in your table) |
Hi @ivila Raw SQL in GORM:
Use GORM
There are errors in BOTH SQL,
There is an index "IDX_login" on column "login" and it is visiable. Is there miss index in GORM? or any thing I did wrong? Thank you again. |
Maybe you can submit a minimum reproduction example first, I test your codes myself, it runs without any error. CREATE TABLE my_table (
id bigint(11) unsigned NOT NULL AUTO_INCREMENT,
login bigint(11) unsigned NOT NULL,
username varchar(256) NOT NULL,
PRIMARY KEY (`id`),
KEY `IDX_login`(`login`)
); here is the code I run for testing func test_run(dsn string) {
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
fmt.Println(err)
panic(err)
}
db = db.Debug()
var list1 []map[string]interface{}
err = db.Clauses(hints.ForceIndex("IDX_login")).Table("my_table").Select("login, username").Where("login = ?", 123456).Find(&list1).Error
fmt.Println(err)
fmt.Println(list1)
} |
@ivila |
Your Question
Hi there,
There is a huge MySQL table in my system, more than 14,000,000 records in it, and there are primary key and indexes.
Query 200 records from DB via DBeaver with raw SQL, it takes 110ms;
Query 200 records from DB via GORM , it takes 22s, is there something wrong.
There are GORM config:
MySQL: version 8
Golang: 1.22.1
GORM: v1.25.10
driver/mysql v1.5.7
Can you please take a look at this, thanks a lot for this.
Query from a huge table will take less than 500ms via GORM.
The text was updated successfully, but these errors were encountered: