如何计算我需要的数据库内存量?

时间:2016-09-26 15:01:05

标签: postgresql memory

我正在创建一个包含数据库的应用。我发现我的数据库有一些免费托管,但我不确定我的数据库是否有足够的内存。所以,我的问题是如果我知道每个表有多少记录(行),如何计算硬盘上的内存大约将占用我的数据库? PS:数据库不会有任何媒体(图片,音乐等)只是数字和文字。

1 个答案:

答案 0 :(得分:1)

这是一个更实际的答案,不是优雅的,但应该给你一个起点。它基于this link

1. Construct a dummy table that is set up **exactly** how you want your real table to be.

2. Write a query that inserts 10000 random rows into the table.
   You can use a scripting language for this.
   For example, if you have three columns, all with varchar(100), try using php to write this query"

    $col1_Val = str_repeat('x', mt_rand(0,100));
    $col2_Val = str_repeat('y', mt_rand(0,100));
    $col3_Val = str_repeat('z', mt_rand(0,100));

    $bulk_query = "INSERT INTO my_dummy_table (col1, col2, col3) VALUES ($col1_Val, $col2_Val, $col3_Val),";

3. Concatenate to create a statement that will do 10000 inserts:
    for($i = 0; $i < 9999; $i++) {
        $col1_Val = str_repeat('x', mt_rand(0,100));
        $col2_Val = str_repeat('y', mt_rand(0,100));
        $col3_Val = str_repeat('z', mt_rand(0,100));
        $bulk_query .= ",($col1_Val, $col2_Val, $col3_Val)";
    }

4. Now the query has been constructed.
   Echo it or copy it in whichever way and then run the query from a script, PHPAdmin, command line, etc.

5. Now get the table size either using the link above, PHPAdmin, etc.

6. Divide the table size by 10000.
   This is your `average space requirement per row`.

7. If you know roughly how many rows your table will need, multiply `number of rows` by `average space requirement per row`.
   This your **estimated space requirement**.