简体   繁体   English

存储不变数据的最佳方式

[英]Best way to store unchanging data

As I work on my games I've been using MySQL databases a lot. 当我处理我的游戏时,我一直在使用MySQL数据库。

But more recently I've been putting a lot of SELECT results in Memcache, because they're unlikely to change. 但是最近我在Memcache中放了很多SELECT结果,因为它们不太可能改变。

Which leads me to wonder, wouldn't it be better to just have a PHP file that defines arrays of data? 这让我想知道,仅仅拥有一个定义数据数组的PHP文件会不会更好? Sure, this means I have to load in the entire array just to access one row, but I believe that this is insignificant compared to the time saved not having to wait for MySQL. 当然,这意味着我必须加载整个数组只是为了访问一行,但我相信与无需等待MySQL的时间相比,这是微不足道的。

Am I right on this path of thinking? 我是否正确思考这条道路? Should I move my unchanging game data (such as character base stats, compatibility charts, item data, etc.) to PHP files? 我应该将我不变的游戏数据(例如角色基础统计数据,兼容性图表,项目数据等)移动到PHP文件中吗? Or should I keep using MySQL+Memcache? 或者我应该继续使用MySQL + Memcache?

Which is better, and why? 哪个更好?为什么?

It would seem that some benchmarks are in order. 似乎有些基准是有序的。

I put together a few scripts to test storing the data as a PHP array, as a CSV file, and in a MySQL DB accessed via PDO. 我整理了一些脚本来测试将数据存储为PHP数组,CSV文件以及通过PDO访问的MySQL数据库。

Array Test 阵列测试

Here are the scripts for the array test: 以下是阵列测试的脚本:

array-inc.php: 阵列inc.php:

$data = array(
    array(
        "key"   => "something",
        "value" => "something else"
    ),
    array(
        "key"   => "something",
        "value" => "something else"
    ),
    array(
        "key"   => "something",
        "value" => "something else"
    ),
    array(
        "key"   => "something",
        "value" => "something else"
    ),
    array(
        "key"   => "something",
        "value" => "something else"
    )
);

array-test.php 阵列test.php的

$start = microtime( TRUE );

for( $i = 0; $i < 10000; ++$i ) {
    include( "array-inc.php" );
    $retrieve = $data[2];
}

$finish = microtime( TRUE );

print $finish - $start;

CSV Test CSV测试

Here are the scripts for the CSV test: 以下是CSV测试的脚本:

values.csv: values.csv:

key,value
something,something else
something,something else
something,something else
something,something else
something,something else

csv.php: csv.php:

$start = microtime( TRUE );

for( $i = 0; $i < 10000; ++$i ) {
    $fp = fopen( "values.csv", "r" );

    $data = array();
    while(( $line = fgetcsv( $fp )) !== FALSE ) {
        $data[] = $line;
    }

    $retrieve = $data[2];
}

$finish = microtime( TRUE );

print $finish - $start;

MySQL Test MySQL测试

And here is the script for the MySQL test (the table has an id, key, and value column with the same five rows and values as the above two): 这里是MySQL测试的脚本(该表有一个id,key和value列,其中包含与上述两行相同的五行和值):

mysql.php: mysql.php:

$start = microtime( TRUE );

for( $i = 0; $i < 10000; ++$i ) {
    $query = "SELECT * FROM `values` WHERE id = :id";
    $result = $pdo->prepare( $query );
    $result->execute( array( ":id" => 2 ));
    $retrieve = $result->fetch( PDO::FETCH_ASSOC );
}

$finish = microtime( TRUE );

print $finish - $start;

Each of these is set to access one element from the stored data, and to loop through the process 10,000 times so that the time could be more accurately measured. 这些中的每一个都设置为从存储的数据访问一个元素,并循环过程10,000次,以便可以更准确地测量时间。 Here are the results: 结果如下:

I ran each of these tests three times and received the following values: 我运行了三次这些测试,并收到以下值:

  • Array: 阵:
    • 1.050, 1.020, 1.114 seconds 1.050,1.020,1.114秒
  • CSV: CSV:
    • 1.264, 1.232, 1.105 seconds 1.264,1.232,1.105秒
  • MySQL: MySQL的:
    • 1.038, 1.149, 1.171 seconds 1.038,1.149,1.171秒

It seems that CSV is the clear loser, but Array and MySQL are very close with Array perhaps taking a slight lead in performance. 似乎CSV是明显的输家,但Array和MySQL非常接近于Array可能在性能方面略微领先。 Feel free to tweak the code above to provide results that are more in line with your environment. 您可以随意调整上面的代码,以提供更符合您环境的结果。

The answer to your question is problem specific. 您的问题的答案是特定问题。 It's a trade off between performance, memory and maintainability. 这是性能,内存和可维护性之间的权衡。 Even if you want to name the cons and pros for candidate solutions, you need to assume some parameters. 即使您想为候选解决方案命名缺点和专业人员,您也需要假设一些参数。 Here are parameters affecting the decision you will make: 以下是影响您将做出的决定的参数:

  1. Size of data: What is the total size of data you have. 数据大小:您拥有的数据总大小是多少。 ie your database file size. 即您的数据库文件大小。

  2. Average size of memory needed to requests loading the data: Considering parameter named above and average number of concurrent requests how much memory is required for them. 请求加载数据所需的平均内存大小:考虑上面提到的参数和平均并发请求数,需要多少内存。

  3. Average ratio of total data you need per request: Each time you load the data, how much of it you will be working with it and how easily can you select this portion of data. 每个请求所需的总数据的平均比率:每次加载数据时,您将使用多少数据以及选择这部分数据的容易程度。

  4. How much (CPU) time does it take to extract the aimed portion of data out of total set: It can vary vastly from a simple PHP array to a MySQL SELECT query. 从总集中提取数据的目标部分需要多少(CPU)时间:从简单的PHP数组到MySQL SELECT查询,它可以有很大的不同。

Knowing these parameters you can ask the question if one solution is better or the other. 了解这些参数后,您可以询问一个解决方案是更好还是另一个解决方案。 In any case your solution will be one of the following: 无论如何,您的解决方案将是以下之一:

  • Included in code. 包含在代码中。
  • Configuration files ( XML , INI , CSV , JSON and etc.). 配置文件( XMLINICSVJSON等)。
  • Database server ( MySQL ). 数据库服务器( MySQL )。
  • Local database ( SQLite ). 本地数据库( SQLite )。
  • Cached in memory (Cache servers like memcached , or maybe PHP 's shared memory ). 缓存在内存中(缓存服务器,如memcached ,或者PHP的共享内存 )。

I know I didn't answer your question but that's because it's impossible to answer it unless you specify the mentioned parameters. 我知道我没有回答你的问题,但那是因为除非你指定上述参数,否则不可能回答它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM