开发者

A lookup table, Store in MySQL or PHP

开发者 https://www.devze.com 2023-02-11 06:08 出处:网络
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)

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?)


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);


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号