AM-
AM-

Reputation: 881

Storing lists: in files or MySQL?

Which is the generally better way of storing lists of the following type:

array(
   'category'=>array(
      'key'=>'value',
      'key'=>'value'
    ),
   'category'=>array(
      'key'=>'value',
      'key'=>'value'
    )
);

Would a more efficient approach to storing these lists be a lists.php file or a serialized entry in MySQL?

Upvotes: 0

Views: 80

Answers (3)

Alex Howansky
Alex Howansky

Reputation: 53573

Don't use a database unless you need to, it adds unnecessary overhead, complication, and introduces a new point of failure. In this case, I'd encode the list as a PHP source array. It will be orders of magnitude faster and much easier to implement -- you can whip up this code in a few seconds. I don't think there's any advantage to using a database in this case. The overhead required to store a 600 element array is negligible. If you later find that the array grows too big to be reasonable, or needs to change often, you can move it to the database then. Don't over engineer it.

Upvotes: 0

GGio
GGio

Reputation: 7653

Well depends on your mysql tables structures:

  • If your tables are NORMALIZED then serializing data and storing it will break normalization.

But other than that why dont you create two tables, one storing the categories and then another storing key-value pairs referencing to that category.

CREATE TABLE Categories (
   CID INT,
   ***
   PRIMARY KEY (CID)
)ENGINE=InnoDB;

CREATE TABLE KeyValues (
   Category INT,
   Key varchar(100),
   Value varchar(100,
   FOREIGN KEY Category REFERENCES Categories(CID)
)ENGINE=InnoDB;

and you can further normalize the keyvalues table depending on your structuring. I didnt not choose primary key on purpose since it depends whether you are normalizing your tables or not. Adding autoincrement primary key would be fine as well.

this is easier and faster to select data. When you have serialized data, if you need to do some kind of calculation you first need to grab on PHP side, un-serialize it and do calculations where you could do same-thing by just one query.

Upvotes: 2

Green Black
Green Black

Reputation: 5084

I suggest you use mysql, easier to maintain, and you can easily pick out the rows you need without having to load the whole array in memory. You can also try using a nosql database like mongoDB.

Upvotes: 1

Related Questions