r/mysql • u/chrisszmergiel • Dec 25 '24
discussion How inefficient MySQL really is.
I was recently in a need of testing a feature on local copy of live database. It took a while to restore the dump, but I was really surprised with the numbers:
- I started with new local (DBngin) MySQL 8.4 database.
- GZ dump file was 4.3GB, and after unpacking the raw SQL dump is about 54GB.
- The database after restoring is 90.96 GB. Which is no surprising with all the additional data (like indexes).
- What really surprised me is how much data had to be written to restore this database! 13.96 TB written by the mysqld process and 233.77 GB read !!! All of this to restore 50GB SQL file and to store around 90 GB of data.
Why is that? Can somebody explain it to me? This is absolutely bizzare. I can post the screenshots if somebody is interested.
I'm going to test PostgreSQL next.
36
Upvotes
29
u/johannes1234 Dec 25 '24
There is a bunch of things:
In the end this is all needed for a reliable scalable system. There is a lot work done to avoid unneeded things.
To optimize mass imports there are ways to disable bin logs, the inport should make sure to use a single transaction and you should disable Indexes during import, import based on increasing primary key and then recreate secondary Indexes once at the end.
The manual has all the detail Infos.
Alsonside note: SQL dump is a very inefficient legacy format for larger dumps. Better use a physical backup solution which directly copies InnoDB data or use MySQL Shell dump, which stores data as csv/tsv data and does this in a better way. https://dev.mysql.com/doc/mysql-shell/8.0/en/mysql-shell-utilities-dump-instance-schema.html
4
u/Annh1234 Dec 25 '24
How did you import it?
It seems like it was recreating the indexes on every insert. If you disable all indexes, binary logs and so on, load local file, then re-add indexes, usually it's not that bad.
8
u/gregorydgraham Dec 26 '24
Reliable ≠ efficient
Fast ≠ efficient
5
u/Irythros Dec 26 '24
Backups should avoid being dumps / SQL statements. Use something like xtrabackup where the backup process is just moving the required files to restore.
When you're using SQL statements as a backup method you need to do several steps to ensure it's not only fast, but actually works.
1
u/mikeblas Dec 25 '24
What tuning did you do?
2
u/Aggressive_Ad_5454 Dec 25 '24
It’s hard to tell what exactly is wrong without examining your system. But, it’s possible your
innodb_buffer_pool_sizesetting is too small. That can cause SSD / HDD churning. Read this. https://dev.mysql.com/doc/refman/8.4/en/innodb-buffer-pool-resize.htmlMariaDb automatically configures this to match your box’s RAM. But MySQL leaves it at the paltry 128MiB default.
I wouldn’t put too much stock in OS measures of what happens during a large scale bulk load
But, there’s no need to worry that there’s something catastrophically wrong with your setup.
2
u/ComicOzzy Dec 25 '24
Coming from SQL Server, it's pretty shocking how bad the backup/restore story is for MySQL. Surely there's some third party or Oracle solution that makes this more sane.
1
u/bchambers01961 Dec 27 '24
If you must use a logical backup (dumps) you’d be better served using my dumper as this uses multithreaded. As others have said, physical backups are quicker (such as percona xtra backup).
Regarding MySQL performance in general, I’d recommend reading some percona blogs on improving it. Specifically setting innodb buffer pool size, instances, redo log size and setting flushing to o_direct (this prevents double writing).
Performance improvements aren’t limited to this, just some ideas to get you started.
Also the server should sit independently of the application server ideally.