问题
I am trying to flag all records in a table that have the minimum value for all records with a common FieldX Value.
My query was as such: TableA
Update TableA as T1
Inner Join (Select ID,Name,Min(ValueField) from TableA
where GroupFlag='X'
Group by CommonTermField) as T2
On T1.ID=T2.ID
Set MainFlag='Y';
This worked awhile back but I keep getting a timeout/table locked error and I am assuming that it is because the table is 26 million records long (with appropriate indexes). Is there a more efficient way to update vs using an inner-join in this case?
Update: After trying to run another Update/Inner-Join that previously worked and also getting a table-locked type error, it occurred to me that recently we migrated to larger servers so we would have overhead to work with these tables. I did some checking while DevOps is out and it turns out the settings weren't migrated (yet) so our "innodb_buffer_pool" which had previously been 2GB was only 128MB. I am waiting until they get in to migrate that and other settings, but am 99% sure the "inefficiency" in the query (which previously worked fine) is due to that. I will leave the Q open until then and if the innodb_pool fix works answer my own question with the settings we changed and how in case anyone else runs into this issue (seeming query inefficiency in fact mysql settings issue).
回答1:
Ok so answer to the question was Mysql settings. Apparently when we migrated servers DevOps/SysAdmin did migrate settings but didn't restart server as I jumped right into query-mode. We restarted last night and things worked swimmingly.
The issue was that innodb_buffer_pool was set to 128MB by default and our custom settings had it at 2GB.
来源:https://stackoverflow.com/questions/37039902/efficient-way-to-flag-a-record-with-min-field-value-and-common-fieldx-value-in-m