Skip to content

Update getNextAutoincrement to not use showTableStatus #1721

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

Closed
wants to merge 5 commits into from

Conversation

jonashrem
Copy link
Contributor

@jonashrem jonashrem commented Jul 6, 2021

Description (*)

update getNextAutoincrement to not use showTableStatus for performants reasons.

Related Pull Requests

magento/magento2#33433
#1712

Fixed Issues (if relevant)

/

Manual testing scenarios (*)

test import/export

Questions or comments

/

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All automated tests passed successfully (all builds are green)

@github-actions github-actions bot added the Component: ImportExport Relates to Mage_ImportExport label Jul 6, 2021
@jonashrem jonashrem changed the title Patch 6 update getNextAutoincrement to not use showTableStatus Jul 6, 2021
@colinmollenhour
Copy link
Member

Looks great, what is the oldest version of MySQL where this code will work?

colinmollenhour
colinmollenhour previously approved these changes Jun 9, 2022
@fballiano
Copy link

was this code tested on OM?

@colinmollenhour
Copy link
Member

I confirmed that the "SELECT AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES" query works on MySQL 5.6 and up and MariaDb 10.3 and up. (probably some earlier versions, these are just the oldest I checked)

I've not tested an import in Magento, @jonashrem did you test an import with the latest version of this PR?

@fballiano fballiano changed the base branch from 1.9.4.x to main May 15, 2023 20:11
@fballiano
Copy link

I've rebased this and tested it briefly creating new customer/addresses/products, the autoincrements work fine.

I've no idea if this is really a performance improvement or if it could create a problem.

But now it's ready to be merged, or it should be closed.

@jonashrem
Copy link
Contributor Author

Thanks

Copy link
Member

@colinmollenhour colinmollenhour left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approve, but I didn't perform a end-to-end test.

@jonashrem Please confirm this has been tested in it's current state. Thanks!

@addison74 addison74 changed the title update getNextAutoincrement to not use showTableStatus Update getNextAutoincrement to not use showTableStatus Aug 11, 2023
@jonashrem
Copy link
Contributor Author

sorry this got under.

I did not test the current variant with up to date OpenMage.

Do you have some specific test szenario in mind?

@fballiano
Copy link

I also will test this out as soon as possible

Copy link
Contributor

@kiatng kiatng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested. It is faster by about 50% to sub mini-sec from 1.5ms.

Mage::getResourceHelper('importexport')->getNextAutoincrement('sales_flat_order_item');

@fballiano
Copy link

is it possible that the result of this call gets cached somewhere?

on my DB I've an autoincrement value of 65:
Screenshot 2023-09-13 alle 15 03 34

but when I run this:
Screenshot 2023-09-13 alle 15 03 57

I get 63:
Screenshot 2023-09-13 alle 15 03 54

@colinmollenhour
Copy link
Member

is it possible that the result of this call gets cached somewhere?

Very odd, I don't see how that is possible.. Are you 100% certain the two tests were run against the same database?

@elidrissidev
Copy link
Member

is it possible that the result of this call gets cached somewhere?

I encountered this issue the first time I tested this PR but forgot to post it. It seems performance_schema is not guaranteed to contain the most up-to-date value, sometimes ANALYZE TABLE is necessary to get accurate results. Maybe just a simple MAX() + 1 query would do the job here.

@fballiano
Copy link

@colinmollenhour I'm sure it is the same database

@elidrissidev AFAIK MAX+1 is not safe in some cases, I don't remember exactly on top of my head, zf1 autoincrement function uses the "last insert id", would that be better at that point? or just leave everything is it is now?

@colinmollenhour
Copy link
Member

colinmollenhour commented Sep 14, 2023

Ahh, I still don't see how essentially the same query via the console and via PHP can have different results, but it appears the information schema data is indeed cached within MySQL for an entire 24 hours by default. This can be controlled by information_schema_stats_expiry. This can be set as a session variable just before the query and yield correct results which isn't a terrible approach, but not sure if that is really an improvement in performance at that point.

Perhaps your IDE is setting this session variable behind the scenes and that is why the results are different?

https://stackoverflow.com/a/65996809/187780

The MAX(id) seems to be just as fast. It might not be accurate if there were deletions or if there were simultaneous transactions that incremented the auto increment but have not been committed yet or were rolled back (I'm not 100% sure on this but somewhat certain). However, that may not be an issue or even a regression - is this method going to be used in a multi-threaded environment?

@fballiano
Copy link

the problem I see is when I run from the PHP CLI, not from the Mysql client (I use SequelAce) which anyway has an expire of 0

Screenshot 2023-09-14 alle 17 47 19

@fballiano
Copy link

but actually if I run php cli I get an expire of 24h
Screenshot 2023-09-14 alle 17 53 26

weird... I'll try to find where is this set...

@colinmollenhour
Copy link
Member

It's probably just a feature of SequelAce so it makes sense now. :)

@fballiano
Copy link

If I do something like this:

Screenshot 2023-09-14 alle 18 01 34

then it works ok.

not sure about all of this :-\

@colinmollenhour
Copy link
Member

In my tests information_schema method takes about half the time of show table status, but considering you have to set a session variable first it requires two round trips at least on the first use. And then there is possibly a performance penalty of disabling the information schema cache which I have no idea how to measure. The MAX(id)+1 method is the fastest by far. This is on a very big and active table from a production database.
image

So I think the MAX()+1 method is a very good suggestion as long as this method isn't used with tables that have deleted rows. If that is not a safe assumption to make (probably isn't) then I think the old method should be kept and no change made since there is not that much benefit for increased complexity and uncertainty.

Perhaps the better change would be to update where this method is used to only call the method once or use MAX(id)+1 instead but leave getNextAutoincrement alone?

@colinmollenhour
Copy link
Member

I don't think stats cache expiry should be set globally if it is not absolutely necessary - that should be up to the DBA to decide ideally. It could be set just before querying information schema, perhaps using a static variable so that it is only done once.

@fballiano
Copy link

sure, that's why I was testing it in the initStatement of the openmage connection, if we remove the GLOBAL it still works

Screenshot 2023-09-14 alle 18 49 22 Screenshot 2023-09-14 alle 18 49 44

Copy link

@fballiano fballiano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have to block the merge because of #1721 (comment)

what are we doing about this?

@kiatng
Copy link
Contributor

kiatng commented Sep 19, 2023

I think we should close it.

@kiatng kiatng self-requested a review September 19, 2023 09:42
@fballiano
Copy link

me too, unless something completely different comes out, this is too risky

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: ImportExport Relates to Mage_ImportExport
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants