duanjie2940 2013-10-06 09:10
浏览 10
已采纳

在两个用户之间找到常见喜欢的最佳方式

I have an app that saves user likes from their fb account and then shows common likes when one user visits another one's profile. There are two ways to do that: 1. Since I already have gotten user1's likes from the database, I can save the IDs in a string like

            $user1_likes="1,2,5,6,10,15,19";

and then use this query to find common likes:

          SELECT name FROM user_like WHERE uid='user2' AND id IN ($user1)

or, I can do this query

        SELECT name FROM user_like WHERE uid='user2' AND id IN (SELECT id FROM user_like WHERE uid='user1')

The problem with the first method is, if user1 has a lot of likes (say 4000), the query might become too big for mysql to handle. The problem with method2 is that mysql should select again what it has already selected.

What do you recommend to handle this situation?

  • 写回答

2条回答 默认 最新

  • dongmu1989 2013-10-06 09:22
    关注

    max_allowed_packet has a default of 16 MB (http://dev.mysql.com/doc/refman/5.1/en/packet-too-large.html), so your first query with 4'000 likes should be no problem at all. Even 10'000 or 100'000 are no problem.

    Try to find out which one is faster if there are 10'000/100'000 likes. If you have proper indizes, the second query shouldn't be a problem either.

    Personally, I would choose the second one.

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

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据