How Triggers May Significantly Affect the Amount of Memory Allocated to Your MyS...
source link: https://www.percona.com/blog/how-triggers-may-significantly-affect-the-amount-of-memory-allocated-to-your-mysql-server/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
How Triggers May Significantly Affect the Amount of Memory Allocated to Your MySQL Server
MySQL stores active table descriptors in a special memory buffer called the table open cache. This buffer is controlled by configuration variables table_open_cache
that hold the maximum number of table descriptors that MySQL should store in the cache, and table_open_cache_instances
that stores the number of the table cache instances. With default values of table_open_cache=4000
and table_open_cache_instances=16
, MySQL will create 16 independent memory buffers that will store 250 table descriptors each. These table cache instances could be accessed concurrently, allowing DML to use cached table descriptors without locking each other.
If you use only tables, the table cache does not require a lot of memory because descriptors are lightweight, and even if you significantly increase the value of the table_open_cache
, the required memory amount would not be so high. For example, 4000 tables will take up to 4000 x 4K = 16MB in the cache, 100.000 tables will take up to 390MB that is also not quite a huge number for this number of tables.
However, if your tables have triggers, it changes the game.
For the test I created a table with a single column and inserted a row into it:
Then I flushed the table cache and measured how much memory it uses:
Then I accessed the table to put it into the cache.
16 table descriptors took less than 16 KiB in the cache.
Now let’s try to create some triggers on this table and see if it changes anything.
Then let’s flush the table cache and test memory usage again.
Initial state:
After I put the tables into the cache:
As a result, in addition to 75.17 KiB in the table cache, 611.12 KiB is occupied by the memory/sql/sp_head::main_mem_root
. That is the "Mem root for parsing and representation of stored programs."
This means that each time when the table is put into the table cache, all associated triggers are put into the memory buffer, storing their definitions.
FLUSH TABLES
command clears the stored programs cache as well as the table cache:
More triggers increase memory usage when put into the cache.
For example, if we create five more triggers and repeat our test we will see the following numbers:
Numbers for the event memory/sql/sp_head::main_mem_root
differ six times:
Note that the length of the trigger definition affects the amount of memory allocated by the memory/sql/sp_head::main_mem_root
.
For example, if we define the triggers as follow:
The resulting amount of memory is 1.89 MiB instead of 3.58 MiB for the longer trigger definition.
Note that having a single table cache instance requires less memory to store trigger definitions. E.g. for our small six triggers, it will be 121.12 KiB instead of 1.89 MiB:
Conclusion
When you access tables that have associated triggers, their definitions are put into the stored programs cache even when not fired. This was reported at MySQL Bug #86821 and closed as “Not a Bug” by Oracle. This is, certainly, not a bug, but the table and stored routines cache design. Still, it is good to be prepared, so you are not surprised when you run short of memory faster than you expect. Especially if you have many triggers with long definitions.
STAY UP-TO-DATE With Percona!
Join 50,000+ of your fellow open-source enthusiasts! Our newsletter provides updates on Percona open source software releases, technical resources, and valuable MySQL, MariaDB, PostgreSQL, and MongoDB-related articles. Get information about Percona Live, our technical webinars, and upcoming events and meetups where you can talk with our experts.
By submitting my information I agree that Percona may use my personal data in send communication to me about Percona services. I understand that I can unsubscribe from the communication at any time in accordance with the Percona Privacy Policy.
Author
Sveta joined Percona in 2015. Her main professional interests are problem solving, working with tricky issues, bugs, finding patterns that can solve typical issues quicker and teaching others how to deal with MySQL issues, bugs and gotchas effectively. Before joining Percona Sveta worked as a Support Engineer in the MySQL Bugs Analysis Support Group in MySQL AB-Sun-Oracle.
She is the author of the book "MySQL Troubleshooting" and JSON UDF functions for MySQL.
Leave a Reply Cancel reply
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK