Storing arrays in MySQL?

前端 未结 6 698
无人共我
无人共我 2021-01-14 07:44

On the Facebook FQL pages it shows the FQL table structure, here is a screenshot below to show some of it (screenshot gone).

You will notice that some items are an

相关标签:
6条回答
  • 2021-01-14 08:41

    The correct way to store an array in a database is by storing it as a table, where each element of the array is a row in the table.

    Everything else is a hack, and will eventually make you regret your decision to try to avoid an extra table.

    0 讨论(0)
  • 2021-01-14 08:45

    There are two options for storing as an array:

    The first, which you mentioned, is to make one, or several, tables, and enumerate each possible key you intend to store. This is the best for searching and having data that makes sense.

    However, for what you want to do, use serialize(). Note: DO NOT EVER EVER EVER try to search against this data in its native string form. It is much faster (and saner) to just reload it, call unserialize(), and then search for your criteria than to develop some crazy search pattern to do your bidding.

    EDIT: If it were me, and this were something I was seriously developing for others to use (or even for myself to use, to be completely honest), I would probably create a second lookup table to store all the keys as columns; Heck, if you did that, mysql_fetch_assoc() could give you the array you wanted just by running a quick second query (or you could extract them out via a JOINed query). However, if this is just quick-and-dirty to get whatever job done, then a serialized array may be for you. Unless you really, really don't care about ever searching that data, the proper column-to-key relationship is, I think most would agree, superior.

    0 讨论(0)
  • 2021-01-14 08:45

    Split it out into other tables. You can serialize it but that will guarantee that you will want to query against that data later. Save yourself the frustration later and just split it out now.

    0 讨论(0)
  • 2021-01-14 08:47

    I guarantee you that Facebook is not storing that data in arrays inside their database.

    The thing you have to realize about FQL is that you are not querying Facebook's main data servers directly. FQL is a shell, designed to provide you access to basic social data without letting you run crazy queries on real servers that have performance requirements. Arbitrary user-created queries on the main database would be functional suicide.

    FQL provides a well-designed data return structure that is convenient for the type of data that you are querying, so as such, any piece of data that can have multiple associations (such as "meeting_for") gets packaged into an array before it gets returned as an API result.

    As other posters have mentioned, the only way to store a programming language structure (such as an array or an object) inside a database (which has no concept of these things), is to serialize it. Serializing is expensive, and as soon as you serialize something, you effectively make it unusable for indexing and searching. Being a social network, Facebook needs to index and search almost everything, so this data would never exist in array form inside their main schemas.

    Usually the only time you ever want to store serialized data inside a database is if it's temporary, such as session data, or where you have a valid performance requirement to do so. Otherwise, your data quickly becomes useless.

    0 讨论(0)
  • 2021-01-14 08:48

    you can serialize the array, insert it, and then unserialize it when you retrieve it.

    0 讨论(0)
  • 2021-01-14 08:49

    They might be using multiple tables with many-to-many relationships, but use joins and MySql's GROUP_CONCAT function to return the values as an array for those columns in one query.

    0 讨论(0)
提交回复
热议问题