一个查找表,存储在 MySQL 或 PHP 中

发布于 2024-10-18 16:44:31 字数 650 浏览 1 评论 0原文

我对存储在 MySQL(独立表)或 PHP(数组)中的查找表之间的性能有疑问,所以这是我的数据(以数组形式)

$users = array(
array(name => 'a', address => 'abc', age => '14'),
array(name => 'b', address => 'def', age => '12'),
array(name => 'c', address => 'ghi', age => '13'),
array(name => 'd', address => 'jkl', age => '14'),
array(name => 'd', address => 'mno', age => '11'),
);

这是 facebook 平台上的游戏,可能有机会玩很多人同时访问。

实际上,该表应该有〜100行,但所有数据都是静态的,如果所有数据都存储在MySQL中,我可以轻松地执行任何“选择”,考虑到对MySQL的查询太多,因此我考虑存储在php数组中,但是,我不知道如何在特定条件下选择行(我知道它应该有其他方法而不是for循环)就像选择所有age = 14到另一个数组中一样。

那么,哪一款的性能更好呢? (MySQL 还是 PHP 查找表?)

I have a question regarding to the performance between a lookup table stored in MySQL (standalone table) or PHP (array), so here is my data (in array form)

$users = array(
array(name => 'a', address => 'abc', age => '14'),
array(name => 'b', address => 'def', age => '12'),
array(name => 'c', address => 'ghi', age => '13'),
array(name => 'd', address => 'jkl', age => '14'),
array(name => 'd', address => 'mno', age => '11'),
);

It is a game on the facebook platform, may have a chance with a lot people access in same time.

Actually, the table should have ~100 rows, but all data is static, if all data store in MySQL, I can do any "select" easily, consider with too much query to MySQL, therefore I consider store in php array, however, I don't know how to select rows in a specific condition (I know it should have other other method rather than for loop) just like select all age = 14 in to another array.

So, which one have the better performance? (MySQL or PHP lookup table?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白芷 2024-10-25 16:44:31

如果您知道:

  • 数据将始终是静态
  • 的您将只有 100 行
  • 您将只在查询中使用简单的单字段匹配
  • 您永远不需要高级功能,例如
  • 您将要使用的 联接获得高流量

...那么我肯定会将逻辑放在纯 PHP 中。 “删除不必要的数据库查询”始终是提高性能的第一步,我认为,仅仅因为不想编写简单的 foreach 循环而将这些简单的行放入 MySQL 中是一个坏主意。 。这太过分了。如果您使用像 APC 这样的操作码缓存(您就是这样,对吧?)那么我认为性能比较不会很接近。 (尽管我总是建议您自己对它们进行实际基准测试以确保确定。)

class Users
{

    protected $_users = array(
        array('name' => 'a', 'address' => 'abc', 'age' => '14'),
        array('name' => 'b', 'address' => 'def', 'age' => '12'),
        array('name' => 'c', 'address' => 'ghi', 'age' => '13'),
        array('name' => 'd', 'address' => 'jkl', 'age' => '14'),
        array('name' => 'e', 'address' => 'mno', 'age' => '11')
    );

    public function select($field, $value)
    {
        $list = array();
        foreach ($this->_users as $user) {
            if ($user[$field] == $value) {
                $list[] = $user;
            }
        }
        return $list;
    }

}

$users = new Users();
$list = $users->select('age', 14);

If you know:

  • the data will always be static
  • that you'll only have 100 rows
  • that you'll only ever be using simple single-field matches in your queries
  • that you'll never need advanced features like joins
  • that you're going to get high traffic

... then I'd definitely put the logic in pure PHP. "Remove unnecessary database queries" is always going to be your first step in improving performance, and dropping these dead-simple rows into MySQL for no other reason than you don't want to write a simple foreach loop is a bad idea, I think. It's overkill. If you're using a opcode cache like APC (which you are, right?) then I don't think the performance comparison will even be close. (Though I'll always recommend actually benchmarking them both yourself to be sure.)

class Users
{

    protected $_users = array(
        array('name' => 'a', 'address' => 'abc', 'age' => '14'),
        array('name' => 'b', 'address' => 'def', 'age' => '12'),
        array('name' => 'c', 'address' => 'ghi', 'age' => '13'),
        array('name' => 'd', 'address' => 'jkl', 'age' => '14'),
        array('name' => 'e', 'address' => 'mno', 'age' => '11')
    );

    public function select($field, $value)
    {
        $list = array();
        foreach ($this->_users as $user) {
            if ($user[$field] == $value) {
                $list[] = $user;
            }
        }
        return $list;
    }

}

$users = new Users();
$list = $users->select('age', 14);
我不会写诗 2024-10-25 16:44:31

PHP在性能方面肯定会更快。但查询会很难维护代码。由于网络的原因,将数据保存在 mysql 中会产生一定的开销。但是您可以通过使用 MyIASM 表并使用服务器端缓存的查询来优化它。

我投票支持启用查询缓存的 MySql 上的 MyIASM 表。

PHP will be definitely faster in terms of performance. But querying will be difficult to maintain code. Keeping data in mysql will be a overhead because of network. But you can optimize that by using MyIASM tables and using query cached on server side.

My vote is for MyIASM table on MySql with query cache enabled.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文