July 24, 2014

MyISAM mmap feature (5.1)

As you know MyISAM does not cache data, only indexes. MyISAM assumes OS cache is good enough and uses pread/pwrite system calls for reading/writing datafiles. However OS is not always good in this task, my benchmarks show Linux/Solaris aren’t scalable on intensive pread calls (I believe the same for Windows, but I did not test it).
In 5.1 I implemented a new feature: memory mapping for the datafiles. That can be enabled by –myisam_use_mmap=1 startup option.
In this case instead of systems call MyISAM will use memcpy function. There is a memory addressing limit for 32bit platforms – 2Gb, so the datafiles over 2GB will be used the old way – pread/pwrite functions. Mmap is available on all POSIX-compatible platforms. It will work faster for SELECT/UPDATE/INSERT inside file queries, and no performance gain (maybe a bit slower) for INSERT at the end of file. In case with INSERT at the end of file we have to use a remap technic – resize memory mmaped area to new extended size. Currenlty we call remap once per 1000 inserts at the end of file and on an exclusive operation (DELETE/UPDATE/INSERT inside file), for work with non-mmaped area we use pread/pwrite functions.
To approve effectiveness of memory mapping several benchmarks:


For benchmarks I used the sysbench tool and tested different workloads:

  1. SELECT by PRIMARY KEY (select-primary for further link)
  2. SELECT by range of PRIMARY KEY (select-range)
  3. OLTP ro queries, group of different SELECT queries (OLTP ro)
  4. UPDATE primary key (update-key)
  5. UPDATE non key column (updaye-nokey)
  6. OLTP rw queries, SELECT/INSERT/DELETE/UPDATE queries (OLTP rw)
  7. batch select queries – scan of 20.000 rows by primary key
More info about table structure and delailed queries you can find on sysbench documentation page.
Tested box:

      Red Hat Enterprise Linux AS release 3 (Taroon Update 2)
      Kernel 2.4.21-15.ELsmp
      4 x Intel(R) XEON(TM) MP CPU 2.00GHz with HyperThreading
      4GB of RAM
The results (queries/sec, more is better):
select-primary
ThreadsPreadMmapMmap/pread ratio
18894.009393.731.06
421774.8222650.341.04
1626161.7127233.201.04
6424330.4424847.331.02
25623868.3324271.741.02

image0011.gif

select-range
ThreadsPreadMmapMmap/pread ratio
11555.052516.281.62
43055.644749.041.55
163193.564365.281.37
643190.754301.651.35
2563183.874225.031.33

image004.gif

OLTP ro
ThreadsPreadMmapMmap/pread ratio
1249.58272.381.09
4608.79737.731.21
16694.52799.641.15
64684.06781.291.14
256668.79767.621.15

image005.gif

update key
ThreadsPreadMmapMmap/pread ratio
15266.306586.481.25
48443.8511446.371.36
168080.2910974.551.36
647654.2810317.601.35
2566474.268577.431.32

image003.gif

update nokey
ThreadsPreadMmapMmap/pread ratio
17665.519114.751.19
410963.0614964.021.36
1610472.8114028.801.34
6410013.0713161.231.31
2568734.1110991.451.26

image006.gif

OLTP rw
ThreadsPreadMmapMmap/pread ratio
1183.48234.361.28
4180.00243.101.35
16177.23238.381.35
64176.17237.311.35
256175.86234.181.33

image002.gif

And finally the interesting results for select batch
ThreadsPreadMmapMmap/pread ratio
113.7933.532.43
429.3699.153.38
1620.48136.896.68
6420.34137.986.78
25620.69137.976.67

image007.gif

So in conclusion memory mapping gives benefit in different cases. The value of performance gain depends of count replaced pread/pwrite calls. For select-primary we replace only one pread call and gain is ~6%, for select-batch we replace 20.000 calls and the summary benefit is ~670%

About Vadim Tkachenko

Vadim leads Percona's development group, which produces Percona Clould Tools, the Percona Server, Percona XraDB Cluster and Percona XtraBackup. He is an expert in solid-state storage, and has helped many hardware and software providers succeed in the MySQL market.

Comments

  1. stanojr says:

    uuu, that is cool
    it will be hard to backport this feature to old versions of mysql ?

  2. Vadim says:

    Not so hard, I even have a patch for 5.0 tree, but MySQL policy does not allow to include new features into GA trees.

  3. One thing that bothers me is that I’m not able to determine my OS cache efficiency.

    I use munin and RRD to graph as many stats about MySQL as possible. Number of slow queries, query cache efficiency, etc.

    The problem is I can’t figure out where Linux keeps stats on its file cache hit rate.

    Anyone have any idea? email me please and I’ll write a munin plugin for it :)

    Kevin

  4. Vadim says:

    Kevin,

    Heh, I also don’t know how to get cache hit rate.
    Some statistics is available in /proc/slabinfo (http://seth.positivism.org/man.cgi/5/slabinfo) but there is no hit statistics

  5. Vadim,

    Does –myisam_use_mmap=1 require any changes in ulimit -n settings? It is about MySQL bug #19800…

  6. Vadim says:

    Valeriy,

    I don’t think mmap require changes in ulimit -n. At least I have not heard about.

    Regarding that bug it could be out of memory error on 32bit.
    Maybe there is still restriction in 2GB summary for allocated memory and mmaped size.
    I’ll try to investigate this.

  7. Yuri says:

    Great, thank you! I’ve got about 10-15% performance on SELECTs with multiple simple joins
    using unindexes LIKE and REGEXP searches throught a table with 1m records.

  8. Michael says:

    As this article is several years old, would it be possible to post the 5.0 series patch if it will still apply to the current 5.0.54 (or other) release?

    Thanks,
    Michael

  9. Andrew says:

    I know this post is ancient, but I thought I’d ask anyway. Do you have a guideline for determining the value of myisam_mmap_size? The default is 18446744073709551615 (16 exabytes) which is clearly ridiculous. I currently have it set randomly to half the size of installed system RAM on a new cluster which is not yet in production. On an existing cluster, mmap was enabled before we noticed the default size, and we’re 99% certain this value caused MySQL to crash several times. No more crashes since disabling mmap. Thank you very much for any insight.

Speak Your Mind

*