The bigger smaller than the smaller one

Ouvir com webReader

I was trying to determine if the storage size of a VARCHAR field in MySQL had any fixed influence in the key size. I've created a few tables, but an interesting thing came out, as you will see. Let's create the test tables:

MySQL:
  1. CREATE TABLE idx_varchar_size ( a VARCHAR(5) NOT NULL, b VARCHAR(20) NOT NULL ) ENGINE=MyISAM;
  2.  
  3. INSERT INTO idx_varchar_size('abcef','1234567890123456789012345678901234567890');

I did this a couple of times:

MySQL:
  1. INSERT INTO idx_varchar_size SELECT * FROM idx_varchar_size;

I actually used this table to be the source data to feed into the test tables:

MySQL:
  1. mysql> CREATE TABLE idx_varchar_mixed ( a VARCHAR(20) NOT NULL, key idx_big(a) ) ENGINE=MyISAM;
  2. Query OK, 0 rows affected (0.01 sec)
  3.  
  4. mysql> CREATE TABLE idx_varchar_big ( a VARCHAR(20) NOT NULL, key idx_big(a) ) ENGINE=MyISAM;
  5. Query OK, 0 rows affected (0.00 sec)
  6.  
  7. mysql> CREATE TABLE idx_varchar_small ( a VARCHAR(5) NOT NULL, key idx_small(a) ) ENGINE=MyISAM;
  8. Query OK, 0 rows affected (0.01 sec)
  9.  
  10. mysql> INSERT INTO idx_varchar_small SELECT a FROM idx_varchar_size ;
  11. Query OK, 374706 rows affected (2.04 sec)
  12. Records: 374706  Duplicates: 0  WARNINGS: 0
  13.  
  14. mysql> INSERT INTO idx_varchar_big SELECT b FROM idx_varchar_size ;
  15. Query OK, 374706 rows affected (3.38 sec)
  16. Records: 374706  Duplicates: 0  WARNINGS: 0
  17.  
  18. mysql> INSERT INTO idx_varchar_mixed SELECT a FROM idx_varchar_size ;
  19. Query OK, 374706 rows affected (3.06 sec)
  20. Records: 374706  Duplicates: 0  WARNINGS: 0

So I've created a small dataset, a "big" dataset, and a "big" schema holding a small dataset. Let's see the output of SHOW TABLE STATUS:

MySQL:
  1. mysql> SHOW table status;
  2. +-------------------+--------+---------+------------+--------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+---------------------+-------------------+----------+----------------+---------+
  3. | Name              | Engine | Version | Row_format | Rows   | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | AUTO_INCREMENT | Create_time         | Update_time         | Check_time          | Collation         | Checksum | Create_options | Comment |
  4. +-------------------+--------+---------+------------+--------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+---------------------+-------------------+----------+----------------+---------+
  5. | idx_varchar_big   | MyISAM |      10 | Dynamic    | 374706 |             24 |     8992944 | 281474976710655 |       818176 |         0 |           NULL | 2009-08-20 14:33:52 | 2009-08-20 14:34:07 | 2009-08-20 14:34:09 | latin1_swedish_ci |     NULL |                |         |
  6. | idx_varchar_mixed | MyISAM |      10 | Dynamic    | 374706 |             20 |     7494120 | 281474976710655 |       798720 |         0 |           NULL | 2009-08-20 14:32:28 | 2009-08-20 14:34:33 | 2009-08-20 14:34:35 | latin1_swedish_ci |     NULL |                |         |
  7. | idx_varchar_size  | MyISAM |      10 | Dynamic    | 374706 |             32 |    11990592 | 281474976710655 |      5514240 |         0 |           NULL | 2009-08-20 13:02:49 | 2009-08-20 13:06:23 | NULL                | latin1_swedish_ci |     NULL |                |         |
  8. | idx_varchar_small | MyISAM |      10 | Dynamic    | 374706 |             20 |     7494120 | 281474976710655 |      4599808 |         0 |           NULL | 2009-08-20 14:32:40 | 2009-08-20 14:32:53 | 2009-08-20 14:32:54 | latin1_swedish_ci |     NULL |                |         |
  9. +-------------------+--------+---------+------------+--------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+---------------------+-------------------+----------+----------------+---------+
  10. 4 rows in SET (0.00 sec)

Well, this is odd. My small table as Index_length a lot bigger (5 times) than my ''big'' dataset?? Maybe it's a SHOW TABLE STATUS bug, let's see how much space the data files actually use.

CODE:
  1. [root@speedy test]# ls -la /var/lib/mysql/test/idx_varchar_{big,mixed,small}*.MYI
  2. -rw-rw---- 1 mysql mysql  818176 Ago 20 14:34 /var/lib/mysql/test/idx_varchar_big.MYI
  3. -rw-rw---- 1 mysql mysql  798720 Ago 20 14:34 /var/lib/mysql/test/idx_varchar_mixed.MYI
  4. -rw-rw---- 1 mysql mysql 4599808 Ago 20 14:32 /var/lib/mysql/test/idx_varchar_small.MYI

Nope. It's true. After a quick thinking, I reminded that MySQL can pack the keys and now this resembles the benefit of packed indexes. Let's make a simple comparison with an explicited packed key:

MySQL:
  1. mysql> CREATE TABLE idx_varchar_small_packed ( a VARCHAR(5) NOT NULL, key idx_small(a) ) ENGINE=MyISAM PACKED_KEYS=1;
  2. Query OK, 0 rows affected (0.01 sec)
  3.  
  4. mysql> INSERT INTO idx_varchar_small_packed SELECT a FROM idx_varchar_size ;
  5. Query OK, 374706 rows affected (2.04 sec)
  6. Records: 374706  Duplicates: 0  WARNINGS: 0

CODE:
  1. [root@speedy test]# ls -la /var/lib/mysql/test/idx_varchar_small_packed.MYI
  2. -rw-rw---- 1 mysql mysql  798720 Ago 20 18:14 /var/lib/mysql/test/idx_varchar_small_packed.MYI

Indeed, it does - it's the same size as idx_varchar_mixed. But it already seems to answer our initial question: VARCHAR size won't influence the key size unnecessary (compare idx_varchar_mixed with idx_varchar_small_packed).

But now I got curious about the smaller size of the key for the bigger dataset. Is it feasible to assume that the MyISAM storage engine, when PACK_KEYS is not specified, it auto-selects a minimum length for VARCHARs which it thinks it worths packing them? The documentation makes no reference to it:

PACK_KEYS takes effect only with MyISAM tables. Set this option to 1 if you want to have smaller indexes. This usually makes updates slower and reads faster. Setting the option to 0 disables all packing of keys. Setting it to DEFAULT tells the storage engine to pack only long CHAR, VARCHAR, BINARY, or VARBINARY columns.

MySQL:
  1. CREATE TABLE idx_varchar_big2 ( a VARCHAR(20) NOT NULL, key idx_big(a) ) ENGINE=MyISAM PACK_KEYS=1;
  2. Query OK, 0 rows affected (0.01 sec)
  3.  
  4. INSERT INTO idx_varchar_big2 SELECT * FROM idx_varchar_big ;
  5. Query OK, 374706 rows affected, 65535 WARNINGS (2.27 sec)
  6. Records: 374706  Duplicates: 0  WARNINGS: 0

CODE:
  1. [root@speedy test]# ls -la /var/lib/mysql/test/idx_varchar_{big,mixed,small,vbig}*.MYI
  2. -rw-rw---- 1 mysql mysql  818176 Ago 20 18:52 /var/lib/mysql/test/idx_varchar_big2.MYI
  3. -rw-rw---- 1 mysql mysql  818176 Ago 20 14:34 /var/lib/mysql/test/idx_varchar_big.MYI

So they match the very same bytes and now I want to know which 'minimum' is that!. I'll be creating many tables (using a simple mental algorithm to speed up) until the packed index pops up. I'll also use the 'b' field from idx_varchar_size to fill each test table column completely to force the key to be bigger (see what otherwise happened with idx_varchar_mixed!), so ignore the warnings after the INSERT INTO. I eventually came up to the split value:

MySQL:
  1. CREATE TABLE idx_varchar_small7 ( a VARCHAR(7) NOT NULL, key idx_verybig(a) ) ENGINE=MyISAM;
  2. Query OK, 0 rows affected (0.01 sec)
  3.  
  4. CREATE TABLE idx_varchar_small8 ( a VARCHAR(8) NOT NULL, key idx_verybig(a) ) ENGINE=MyISAM;
  5. Query OK, 0 rows affected (0.01 sec)

MySQL:
  1. mysql> INSERT INTO idx_varchar_small7 SELECT b FROM idx_varchar_size;
  2. Query OK, 374706 rows affected, 65535 WARNINGS (2.25 sec)
  3. Records: 374706  Duplicates: 0  WARNINGS: 374706
  4.  
  5. mysql> INSERT INTO idx_varchar_small8 SELECT b FROM idx_varchar_size;
  6. Query OK, 374706 rows affected, 65535 WARNINGS (2.34 sec)
  7. Records: 374706  Duplicates: 0  WARNINGS: 374706

CODE:
  1. [root@speedy test]# ls -la /var/lib/mysql/test/idx_varchar_small?.MYI
  2. -rw-rw---- 1 mysql mysql 5366784 Ago 20 20:07 /var/lib/mysql/test/idx_varchar_small7.MYI
  3. -rw-rw---- 1 mysql mysql  801792 Ago 20 20:08 /var/lib/mysql/test/idx_varchar_small8.MYI

I really suspect this value is some kind of measure of efficiency.

I'll postpone (keep reading) some calculations on this because now just popped up a question about our conclusion to the initial question: Does the size of a VARCHAR field in MySQL have any fixed influence in a NOT packed key size?

To answer that, let's create the 'small' and 'big' tables with explicit PACK_KEYS=0:

MySQL:
  1. mysql> CREATE TABLE idx_varchar_small_nopack ( a VARCHAR(5) NOT NULL, key idx_small(a) ) ENGINE=MyISAM PACK_KEYS=0;
  2. Query OK, 0 rows affected (0.01 sec)
  3.  
  4. mysql> CREATE TABLE idx_varchar_mixed_nopack ( a VARCHAR(20) NOT NULL, key idx_small(a) ) ENGINE=MyISAM PACK_KEYS=0;
  5. Query OK, 0 rows affected (0.02 sec)
  6.  
  7. mysql> INSERT INTO idx_varchar_small_nopack SELECT a FROM idx_varchar_size;       
  8. Query OK, 374706 rows affected (2.47 sec)
  9. Records: 374706  Duplicates: 0  WARNINGS: 0
  10.  
  11. mysql> INSERT INTO idx_varchar_mixed_nopack SELECT a FROM idx_varchar_size;   
  12. Query OK, 374706 rows affected (3.20 sec)
  13. Records: 374706  Duplicates: 0  WARNINGS: 0

CODE:
  1. [root@speedy ~]# ls -la /var/lib/mysql/test/idx_varchar*{nopack,small,mixed}.MYI
  2. -rw-rw---- 1 mysql mysql  798720 Ago 20 14:34 /var/lib/mysql/test/idx_varchar_mixed.MYI
  3. -rw-rw---- 1 mysql mysql 4599808 Ago 21 00:23 /var/lib/mysql/test/idx_varchar_mixed_nopack.MYI
  4. -rw-rw---- 1 mysql mysql 4599808 Ago 20 14:32 /var/lib/mysql/test/idx_varchar_small.MYI
  5. -rw-rw---- 1 mysql mysql 4599808 Ago 21 00:22 /var/lib/mysql/test/idx_varchar_small_nopack.MYI

These new 'nopack' tables are indeed of the same size, so it's safe to say:

VARCHAR size won't influence the key size unnecessary

The efficiency of a packed key entry

The VARCHAR index entry is tipically like this:

1 (number of bytes of same prefix) + N (bytes of different suffix) 4 (record pointer size)

I'll remeber that my datasets were Latin1, so each character matches to a single byte. Now 8-5=3. If we had packed keys for a VARCHAR(6) and the data was kind of sequential for each record (like rows being generated by nested loops, such as "aaa", "aab", "aac", etc), thus unique but highly ''packable'', the sum of bytes would be something like this:

1 (number of bytes of same prefix) + 1 (bytes of different suffix) 4 (record pointer size) = 6

This packed index entry size matches the length of VARCHAR; everything below 6 would waste overhead with the first byte for the prefix for no gain at all, right? Which means that to be worthy, PACKED_KEYS should be applied to VARCHARs bigger than 6. Assuming that there is a bytecode separator (High Performance MySQL uses an analogy with a colon, like in "5,a" or "5,b"), we can shift that rule to:

To be worthy, PACKED_KEYS should be applied to VARCHARs bigger than 7!

Now we should confirm there is indeed a separator. I thought of using an hex viewer to see if I could come with a pattern. The best would be to look at MYI specification (either in MySQL source code or MySQL Documentation/Wiki):

HTML:
  1. 00000000  fe fe 07 01 00 03 01 4d  00 b0 00 64 00 c4 00 01  |.......M...d....|
  2. 00000010  00 00 01 00 08 01 00 00  00 00 30 ff 00 00 00 00  |..........0.....|
  3. [... typical file format heading, some 00 and ff ...]
  4. 00000400  03 fd 00 08 31 32 33 34  35 36 37 38 00 00 00 00  |....12345678....|
  5. 00000410  00 00 0e 14 0e 28 0e 3c  0e 50 0e 64 0e 78 0e 8c  |.....(.<.P.d.x..|
  6. 00000420  0e a0 0e b4 0e c8 0e dc  0e f0 0d 01 04 0e 18 0e  |................|
  7. 00000430  2c 0e 40 0e 54 0e 68 0e  7c 0e 90 0e a4 0e b8 0e  |,.@.T.h.|.......|
  8. 00000440  cc 0e e0 0e f4 0d 02 08  0e 1c 0e 30 0e 44 0e 58  |...........0.D.X|
  9. 00000450  0e 6c 0e 80 0e 94 0e a8  0e bc 0e d0 0e e4 0e f8  |.l..............|
  10. [...]
  11. 000c3300  0e 80 0e 94 0e a8 0e bc  0e d0 0e e4 0e f8 0d 59  |...............Y|
  12. 000c3310  0c 0e 20 0e 34 0e 48 0e  5c 0e 70 0e 84 0e 98 0e  |.. .4.H.\.p.....|
  13. 000c3320  ac 0e c0 0e d4 00 00 00  00 00 00 00 00 00 00 00  |................|
  14. 000c3330  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
  15. *
  16. 000c3400  80 67 00 00 00 00 03 02  00 08 31 32 33 34 35 36  |.g........123456|
  17. 000c3410  37 38 00 00 00 71 0d 18  00 00 00 00 03 04 0d 32  |78...q.........2|
  18. [... nonsense data (to me) that could be a file format footer/terminator ...]
  19. 000c3860  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
  20. *
  21. 000c3c00

Notice the data is shown in plain text('12345678'). The [...] indicates content that shows the pattern (sorry can't highlight it) -- actually, it seems to be repeated in intervals of 0x400 (this value definitely has a name and a resason). Now:

  • The "number of bytes of same prefix" should be a constant, and that's probably the value that causes the pattern. I can't explain why it's 0x0e and not 0x08, but that's definitely overhead for some reason.
  • My different suffix is "", since the data is repeated, which is 0 bytes (non-existent) in "bytes of different suffix" above.
  • More, our data is stored adjacentely (the order of "referred rows" in the index is the same as in the data file), and we know that PACKED_KEYS can also pack "record pointer size" above. The offset for the next record (in the data file) is almost certainly smaller than 256 - even with an eventual MYD format overhead, the only field is 8 chars/bytes long -, so it can easily fit on a single byte, which is the way an intelligent packing of the pointer size the should be done.

Therefore, I suspect that a packed key row would take 1+1=2 bytes, forming a kind of pattern composed by the first byte a constant value, and the second an evenly increasing value - although there isn't an ''index entry separator''. And that's just what we're looking at the above hexdump: the second one is increasing 0x14 (20d) sequentially, and that also suggests each record (of 8 bytes) in the data file is actually stored in 20 bytes blocks.

Of course I should fundament some assumptions by looking at the specs, but it seems pretty obvious: and I just wanted to test what I completed minutes ago. Testes were done with MySQL 5.1.37 on an x86. :-)


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button

4 Responses to “The bigger smaller than the smaller one”

  1. [...] my previous conclusions about VARCHAR influence on index size could be quite storage engine specific, I’d like to see if we can extend them to InnoDB, so I took [...]

  2. Actually, as regarding to the value 0×400, I just bumped on this page about Estimating Query Performance, where it reads:

    In MySQL, an index block is usually 1,024 bytes and the data pointer is usually four bytes.

    Maybe there’s more info about the subject, but that’s enough for now.

  3. Also worth mentioning, the posts by peter in MySQL Performance Blog about PACK_KEYS effect on write intensive scenarios and JOINs, respectively:

    http://www.mysqlperformanceblog.com/2008/08/12/beware-of-myisam-key-cache-mutex-contention/

    http://www.mysqlperformanceblog.com/2006/05/13/to-pack-or-not-to-pack-myisam-key-compression/

  4. [...] do tamanho dos campos VARCHAR não influenciar o tamanho dos índices, este tipo de dados (e outros semelhantes) comportam uma série de características que podem [...]

Leave a Reply