正如你从这里(https://dev.twitter.com/docs/api/1/get/statuses/home_timeline)看到的,一条推文有很多信息(字段),因此在MySQL中存储推文信息并不容易。
如果我将此 JSON 制作成数组,它就不会是 1 深度数组。例如,就像此 JSON 中的 URL 实体一样,它可能在一个"实体"字段中有很多 URL。
我是否应该将此信息存储为一个字符串,例如"urls[{"aa": a, "bb": b}, {"aa": c, "bb": d}]"? 或者有没有最好的存储方法?
{
"coordinates": null,
"favorited": false,
"created_at": "Fri Jul 16 16:58:46 +0000 2010",
"truncated": false,
"entities": {
"urls": [
{
"expanded_url": null,
"url": "http://www.flickr.com/photos/cindyli/4799054041/",
"indices": [
75,
123
]
}
],
"hashtags": [
],
"user_mentions": [
{
"name": "Stephanie",
"id": 15473839,
"indices": [
27,
39
],
"screen_name": "craftybeans"
}
]
},
"text": "got a lovely surprise from @craftybeans. She sent me the best tshirt ever. http://www.flickr.com/photos/cindyli/4799054041/ ::giggles::",
"annotations": null,
"contributors": null,
"id": 18700887835,
"geo": null,
"in_reply_to_user_id": null,
"place": null,
"in_reply_to_screen_name": null,
"user": {
"name": "cindy li",
"profile_sidebar_border_color": "AD0066",
"profile_background_tile": false,
"profile_sidebar_fill_color": "AD0066",
"created_at": "Wed Nov 29 06:08:08 +0000 2006",
"profile_image_url": "http://a1.twimg.com/profile_images/553508996/43082001_N00_normal.jpg",
"location": "San Francisco, CA",
"profile_link_color": "FF8500",
"follow_request_sent": false,
"url": "http://www.cindyli.com",
"favourites_count": 465,
"contributors_enabled": false,
"utc_offset": -28800,
"id": 29733,
"profile_use_background_image": true,
"profile_text_color": "000000",
"protected": false,
"followers_count": 3395,
"lang": "en",
"notifications": true,
"time_zone": "Pacific Time (US & Canada)",
"verified": false,
"profile_background_color": "cfe8f6",
"geo_enabled": true,
"description": "Just me, Cindy Li.Giving cute substance since 1997.'r'nMarried to @themattharris.'r'nProduct designer for Yahoo! ",
"friends_count": 542,
"statuses_count": 4847,
"profile_background_image_url": "http://a3.twimg.com/profile_background_images/3368753/twitter_flowerbig.gif",
"following": true,
"screen_name": "cindyli"
},
"source": "web",
"in_reply_to_status_id": null
},
将其存储在列中。然后,您可以在要快速查找的内容上放置索引 - 而不仅仅是能够搜索所有内容。以您建议的格式搜索特定内容将是一场噩梦。
如果你想继续使用其中的位,请编写一个对象来处理它的数据库方面,或者至少是一个可以弹出或将它们取出的函数。现在看起来可能有更多的工作,但从长远来看,它将在以后为您节省更多的精力。
编辑:是的,我会将每个数据位保存在自己的列中。话虽如此,您可能不需要存储每一点信息。例如,如果您不想保留"用户提及"的信息,请完全跳过它。
编辑 2L 以正确看待这一点,假设您想搜索"Bob"。如果您有如下所示的列结构:
+------+-----------+-----------+-----+
| user | favorited | truncated | url |
+------+-----------+-----------+-----+
| Bob | true | false | ... |
| Sue | true | true | ... |
| Tom | true | false | ... |
+------+-----------+-----------+-----+
你可以只写一个死的简单查询。
与类似的东西:
+--------------------------------------------------------------+
| tweetData |
+--------------------------------------------------------------+
| user:Bob;favorited:true;truncated:false;url:www.example.com |
| user:Sue;favorited:true;truncated:true;url:www.example2.com |
| user:Tom;favorited:true;truncated:false;url:www.example2.com |
+--------------------------------------------------------------+
想象一下,试图找出鲍勃被收藏的次数。您必须每次提取整行,执行一些操作/正则表达式/欺骗来获取字段,然后手动对其进行统计。
苛刻但真实的答案是阅读数据库设计基础知识。似乎您认为必须将其存储在一张表中。您希望将其拆分为多个表并将它们连接在一起。