Refresh MySQL views?
At work I'm constantly being told that when changes are made to a MySQL db that the views need to be 'refreshed'. The accepted manual solution seems to be going into Workbench, right clicking, and hitting 'Refresh All'
Is this just meant to be clearing the cache? Or does that rebuild the views from scratch, or is this totally bogus? They seem to be able t开发者_JS百科o tell when views have not been 'refreshed', and I'm not sure they understand it any more than, "Because things need to be refreshed when they are changed."
If it is just clearing the cache, would 'FLUSH TABLES WITH READ LOCK' be enough?
Views do not need to be refreshed when the data changes. When you query them they will fetch the newest data.
They might need to be recreated if your table structure changes:
The view definition is “frozen” at creation time, so changes to the underlying tables afterward do not affect the view definition. For example, if a view is defined as SELECT * on a table, new columns added to the table later do not become part of the view.
Source
I was working on a project and my view didn't show new data because I was trying to make a join on a NULL field to another table. So I just updated the new data with a correct value for that field and it worked.
精彩评论