0

Question

The same table stored on hard drive (InnoDB engine) takes 930.72 MB when in Ram (MEMORY engine) it takes 1538.54 MB.

  • For what reason?
  • How to calculate space needed to save data in memory? Are there any general rules?

Example

Text below is not question, but allow reconstruct my database state.

We create database:

DROP DATABASE IF EXISTS test;
CREATE DATABASE IF NOT EXISTS test;
USE test;

Create table innoDB

CREATE TABLE main(
 id INTEGER UNIQUE NOT NULL AUTO_INCREMENT PRIMARY KEY,
 value INTEGER
);

Define procedure to load data

delimiter #
create procedure load_data(IN _max INTEGER)
begin
declare counter int unsigned default 0;
 truncate table main;
 start transaction;
 while counter < _max do
 set counter=counter+1;
 insert into main (value) values (counter);
 end while;
 commit;
end #
delimiter ;

Call procedure

call load_data(25000);

Create table MEMORY

-- SET max_heap_table_size = 16*1024*1024;
CREATE TABLE memory_main (
 id INTEGER UNIQUE NOT NULL AUTO_INCREMENT PRIMARY KEY,
 value INTEGER
) ENGINE=MEMORY
AS SELECT * FROM main;

We measure size of both tables and this size is not the same.

SELECT
 table_name AS `Table`,
 round(((data_length + index_length) / 1024 / 1024), 2) `Size in MB`
FROM information_schema.TABLES
WHERE table_schema = "test"
 AND table_name LIKE "%main%";
ypercubeTM
99.7k13 gold badges217 silver badges306 bronze badges
asked Dec 7, 2016 at 21:25
2
  • The table has two INTs? And 25K rows? That's 0.2MB of data. Add on some overhead, and you still get less than 1MB. I can't believe you got on the order of 1GB of table space. Commented Dec 7, 2016 at 21:58
  • In My case I saved 25 M for test, but it takes a long time, so in this example I wrote 25 k. Commented Dec 7, 2016 at 22:15

1 Answer 1

1

InnoDB stores the data in a BTree, with the PRIMARY KEY clustered. Your 8 bytes of data will expand to an average of about 30 bytes due to record overhead, block overhead, BTree characteristics, etc, etc. The BTree blocks are 16KB each. After some point in the growth of the table, chunks of 8MB are allocated, so you will find the table growing is big increments.

MEMORY stores data in a Hash. So the PK is hashed to build the data structure. I am not familiar with the details, but they are significantly different than InnoDB's BTree structure. A common technique is to double the hash size when needed, thereby also leading to significant increments at seemingly random times.

What is the goal of this exercise?

answered Dec 7, 2016 at 22:04
2
  • Goal? I started learning of databases. This topic is slightly connected with two last questions, that you answered. Commented Dec 7, 2016 at 22:29
  • You might like Rick's RoTs, which gives a lot of MySQL-specific tips. Reading between the lines, or sometimes explicitly stated, are aspects of how MySQL works. Or they may lead to more questions, which I will probably answer. Commented Dec 7, 2016 at 23:10

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.