按字母顺序显示用户好友的最佳方式

发布于 2024-08-18 10:12:26 字数 533 浏览 11 评论 0原文

在我使用 PHP/MySQL 开发的社交网络上,我有一个朋友页面,它将显示用户拥有的所有朋友,就像大多数网络一样。我在 MySQL 中有一个朋友表,它只有几个字段。 auto_ID, from_user_ID, to_friend_ID, date

我想让朋友页面有几个不同的选项来对结果进行排序,

  1. 按 auto_ID 基本上按照添加朋友的顺序。它只是一个自动递增的 id

  2. 按日期新朋友,将使用日期字段

  3. 按朋友姓名,将有一个按字母顺序排列的列表。

按字母顺序排列是我需要一些建议的地方。我将有一个字母表 AZ 的列表,当用户单击 K 时,它将显示所有以 K 开头的用户名,依此类推。诀窍是它需要很快,因此在用户表上执行 JOIN 不是一个选项,尽管大多数人会认为它很快,但这不是我想要的此操作的性能。我的一个想法是在我的友谊表中添加一个额外的字段,并将用户名的第一个字母存储在其中。用户可以随时更改名称,因此我必须确保在用户更改名称时更新可能的数千条记录。

有更好的方法吗?

On a social network I am working on in PHP/MySQL, I have a friends page, it will show all friends a user has, like most networks do. I have a friend table in MySQL, it only has a few fields. auto_ID, from_user_ID, to_friend_ID, date

I would like to make the friends page have a few different options for sorting the results,

  1. By auto_ID which is basically in the order a friend was added. It is just an auto increment id

  2. new friends by date, will use the date field

  3. By friends name, will have a list in alphabetical order.

The alphabetical is where I need some advice. I will have a list of the alphabet A-Z, when a user clicks on K it will show all the user's name starting with K and so on. The trick is it needs to be fast so doing a JOIN on the user's table is not an option, even though most will argue it is fast, it is not the performance I want for this action. One idea I had is to add an extra field to my friendship table and store the first letter of the users name in it. User's can change there name at anytime so I would have to make sure this is updated on possible thousands of records, anytime a user changes there name.

Is there a better way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烛影斜 2024-08-25 10:12:26

好吧,如果您不想进行联接,那么将用户的姓名或缩写存储在 Friendship 表上实际上是您唯一的其他可行选择。您提到每次名称更改时都必须更新数千条记录的问题,但这真的是一个问题吗?除非您谈论的是 Facebook 或 MySpace 等主要社交网站,否则普通用户是否真的有足够的朋友来解决这个问题?然后你必须将其乘以用户更改名称的概率,我想这对于每个用户来说并不是经常发生的事情。

如果这些更新实际上并不重要,您可以随时将其放在后台或延迟在非高峰时段进行。当然,您会牺牲每秒的准确性,但实际上,大多数用户会注意到吗?可能不会。

编辑:请注意,我上面的回答仅适用于您已经拥有这些级别的用户的情况。如果您基本上仍在开发网站,只需担心它能否正常运行,并在扩展问题成为真正的问题时担心它们。

Well if you don't want to do a join, then storing the user's name or initials on the friendships table is really your only other viable option. You mention the problem of having to update thousands of records every time a name changes, but is this really a problem? Unless you're talking about a major social networking site like Facebook, or maybe MySpace, does the average user really have enough friends to make this problematic? And then you have to multiply that by the probability that a user will change their name, which I would imagine isn't something that happens very often for each user.

If those updates are in fact non-trivial, you could always background or delay that to happen during non-peak times. Sure you would sacrifice up-to-the-second accuracy, but really, would most users even notice? Probably not.

Edit: Note, my answer above really only applies if you already have those levels of users. If you are still basically developing your site, just worry about getting it working, and worry about scaling problems when they become real problems.

画▽骨i 2024-08-25 10:12:26

您还可以查看像 memcached 这样的缓存解决方案。您可以有一个始终更新 memcached 哈希的后台进程,然后当您需要此数据时,它已经在内存中了。

You could also look at a caching solution like memcached. You can have a background process that is always updating a memcached hash and then when you want this data it is already in memory.

凉城 2024-08-25 10:12:26

我只需加入包含名称的表,然后对名称进行排序。假设一个非常正常的表格布局:

Table Person:
ID,
名,

表好友:
自动ID,
来自_用户_ID,
to_friend_ID,
date

你可以做这样的事情:

Select person.id, person.firstname, person.lastname, friend.auto_id 
from Friend
left join on person where person.id = friend.to_friend_ID
where friend.from_user_ID = 1
order by person.lastname, person.firstname

或者

Select person.id, person.firstname, person.lastname, friend.auto_id 
from Friend
left join on person where person.id = friend.to_friend_ID
where friend.from_user_ID = 1
order by friend.date desc

我真的建议在朋友表中添加一列来保留第一个字母,不需要复制这样的数据(并且必须担心保持同步),这就是联接的作用为了。

I'd just join on the table that contains the name and then sort on the name. Assuming a pretty normal table layout:

Table Person:
ID,
FirstName,
LastName

Table Friend:
auto_ID,
from_user_ID,
to_friend_ID,
date

You could do things like:

Select person.id, person.firstname, person.lastname, friend.auto_id 
from Friend
left join on person where person.id = friend.to_friend_ID
where friend.from_user_ID = 1
order by person.lastname, person.firstname

or

Select person.id, person.firstname, person.lastname, friend.auto_id 
from Friend
left join on person where person.id = friend.to_friend_ID
where friend.from_user_ID = 1
order by friend.date desc

I'd really recommend adding a column in the friend table to keep the first letter around, no need to duplicate data like that (and have to worry about keeping it in sync), that's what joins are for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文