Skip to content
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

Closed
rts-gordon opened this issue Sep 28, 2024 · 4 comments
Closed

SQL very slow while query from a huge database table #7213

rts-gordon opened this issue Sep 28, 2024 · 4 comments
Assignees
Labels
type:question general questions

Comments

@rts-gordon
Copy link

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.

@rts-gordon rts-gordon added the type:question general questions label Sep 28, 2024
@ivila
Copy link
Contributor

ivila commented Sep 29, 2024

@rts-gordon You can just open profile in your mysql server and check what's going on.
From my experience, it might due to the stupid behavior of SQL optimizer when using parameterized sql (gorm use it automatically to prevent SQL injection, which I think you didn't try it when you test via DBeaver with raw SQL).

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)

@rts-gordon
Copy link
Author

Hi @ivila
Thanks a lot for your answer. I did some optimize for query, add force index to SQL:

Raw SQL in GORM:

SELECT login, username FROM my_table FORCE INDEX (login) WHERE login = 123456

Use GORM

err := db.DB().Clauses(hints.ForceIndex("IDX_login")).Table("my_table").Select("login, username").Where("login = ?", 123456).Find(&list1).Error

There are errors in BOTH SQL,

Error 1176 (42000): Key 'IDX_login' doesn't exist in table 'my_table'

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.

@ivila
Copy link
Contributor

ivila commented Sep 30, 2024

Hi @ivila Thanks a lot for your answer. I did some optimize for query, add force index to SQL:

Raw SQL in GORM:

SELECT login, username FROM my_table FORCE INDEX (login) WHERE login = 123456

Use GORM

err := db.DB().Clauses(hints.ForceIndex("IDX_login")).Table("my_table").Select("login, username").Where("login = ?", 123456).Find(&list1).Error

There are errors in BOTH SQL,

Error 1176 (42000): Key 'IDX_login' doesn't exist in table 'my_table'

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.
Here is the SQL I use to create the table:

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)
}

And the snapshot of its result
image

@rts-gordon
Copy link
Author

rts-gordon commented Sep 30, 2024

@ivila
I have fixed this issue with your help. Thank you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:question general questions
Projects
None yet
Development

No branches or pull requests

3 participants