duanmo7075 2013-06-28 05:09
浏览 36
已采纳

如何最有效地为许多用户ID选择匹配用户ID的行/记录?

In my database I have a table containing records with their own primary id key and a user id key. Client side, in my javascript, I have an array full of many many user ids. For each id in this array, I wish to search and select the record(s) in the table with the matching user id associated. I'm not sure if I have the best, most efficient method of doing so.

Right now, this is what I am thinking:

Semi-psuedo code:

Javascript

for each element (user id) in array: 
     .get( 'script.php', userid_data, function(response){
             // ..do something with the row/record data of that specific user...
           }

So for each user id in that array, I perform an AJAX get() request, sending that specific user id to a php script which searches the entire table for a record/row with a user id match.

PHP/MySQL (script.php)

/*..some connection code..*/
/*.. some receive code..*/
/* query code: */

 "SELECT * FROM `recordTable` WHERE `userid` =" .var_containing_userid.";"

/*...some array to json encoding..*/
/* return attribute data */         

However, though this is the only way I can think of, this just does not seem to be the most efficient way right now to me.

This is because, theoretically, both the array of user id's and the number of records could be infinitely many (or just a lot) in the future. I'm not sure how fast this would run if there were hundreds of IDs and an ever growing number of records in this table.

Is there a better/faster way? How large could the table of records grow before the execution of such a method becomes significantly slow? I am using PhpMyAdmin.

  • 写回答

2条回答 默认 最新

  • doushenyi9104 2013-06-28 05:19
    关注

    Try this query:

       SELECT * FROM recordTable WHERE userid IN $user_id_array
    

    $user_id_array is the array containing user_ids.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法