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

MySQL Parameterized Query execute time too long #7068

Open
MakeEarthBetter opened this issue Jun 14, 2024 · 3 comments
Open

MySQL Parameterized Query execute time too long #7068

MakeEarthBetter opened this issue Jun 14, 2024 · 3 comments
Assignees
Labels
type:question general questions

Comments

@MakeEarthBetter
Copy link

When I execute a SQL like:

select age from table1 where mobile in (?,?,?......); // about 4k '?' here

The time for execute will be so so so long , maybe 8 seconds.

However, if I splice SQL dicrectly, it will be 0.5s to execute.
Like:
select age from table1 where mobile in (1,2,3,4,5...);

But I really want to use Parameterized Query for safe. There is another question : the quantity of '?' is not certain.

Has anyone encountered this problem?

I need your help plz

@ivila
Copy link

ivila commented Jun 14, 2024

@MakeEarthBetter
Is there any index on your mobile field? If so, then that your situation could due to that when using PrepareStatement(your Parameterized Query), sometimes MySQL would use wrong indexes or ignore some indexes you think it should.
Try change your sql to:

# assume you have a index idx_mobile of your mobile field
select age from table1 FORCE INDEX(idx_mobile) where mobile in (?,?,?......);

Just don't rely too much on MySQL query optimizer, there are many edge cases it can't handle.

@MakeEarthBetter
Copy link
Author

@MakeEarthBetter Is there any index on your mobile field? If so, then that your situation could due to that when using PrepareStatement(your Parameterized Query), sometimes MySQL would use wrong indexes or ignore some indexes you think it should. Try change your sql to:

# assume you have a index idx_mobile of your mobile field
select age from table1 FORCE INDEX(idx_mobile) where mobile in (?,?,?......);

Just don't rely too much on MySQL query optimizer, there are many edge cases it can't handle.

Thanks for your solution so much and It has decreased the time to 2.5 seconds!
But another question appeared:
1: model.DB.Raw(select age from table1 where mobile in (1,2,3,4,5...);) // time:0.04s
2: model.DB.Raw(select age from table1 where mobile in (?,?,?,?,?...);, paramList...) // time:2.5s
3: execute in MySQL Client: select age from table1 where mobile in (1,2,3,4,5...); // time:1.0s

It really puzzled me. Do you know what may makes that difference?

@ivila
Copy link

ivila commented Jun 17, 2024

You can just open the profile settings in your mysql server to see what happened, check this.
I guess:

  1. the 0.04s: there are cache in your server or client, or there are mistakes in your codes.
  2. the 2.5s: the cost of PrepareStatement.
  3. the 1.0s: what it really should be.

But as what I said, just open the profile settings in your mysql server to see what happened.

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