duancaishi1897 2017-08-02 19:08
浏览 40
已采纳

处理哈希数据的最佳算法

Question is about best way to handle data.
Let's assume we have such key -> value data:

"user@gmail.com": { "name": "John",
                    "age": 20,
                    "job": "developer",
                    "favourite_food": ['taco', 'steak']
                    //...etc
                  }
 //...etc

There is a lot of data for users with key "email", like a million. And usually I had to search users by their email.
But today my boss came up to me and said he want to search users by their names and of course keep possibility to search by email. On the other day he said he want my program to realize search by age and so on.
My first thought was to iterate over data with, for example, this php code:

 foreach($email as $data){
   foreach($data as $k => $v){
     if($v == 'search value'){
       return $email;
     }
   }
 }

But this solution is not good for big amount of data.
My second idea was to iterate over first data and create for each email own table to make it look like this:

$a = "user@gmail.com": {//all data}
$b = "John" : {//all data including email}
$c = "developer":{//all other data}
// and so on

But my users getting older with time, so I have to update user age every time the data in my main object changes.
So, my question is, what is the best way to implement such logic using any programming language?


Some notes:
It had to be done by using programming language without touching MySQL or any other DB.

  • 写回答

1条回答 默认 最新

  • dor2p0520 2017-08-02 19:41
    关注
    1. I think using the year of birth of users instead of age might be better in this situation.

    2. You can use index if you are using database. If not, I think you can create index by yourself. A simple index strategy is:

    Do not change the original data, but add index dicts where the keys are index and values are email. Like in python you can add two indices, name and yearofbirth:

    name = {"John": ["xx@xx.com", "cc@cc.com", "aa@c.com"],
            "Mike": ["aa@aa.com", ...],
            #...etc}
    yearofbirth = {"1981":["xx@xx.com", "cc@cc.com"],
                   #...etc}
    

    In this way, you can search by name or yearofbirth to get the email and then fetch the original data. And it is fast.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像