喜欢按钮功能 [英] like button functionality

查看:73
本文介绍了喜欢按钮功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好程序员,

我有一个关于数据库的问题。实际上我想在我的项目中创建类似的功能。我给你一个场景,根据这个你可以给我合适的建议。

假设我的网站上有多个产品(不固定,比如1000)显示...用户可以查看这些产品和可以喜欢任何产品(一个用户可以喜欢很多产品,用户也没有固定)

如果我必须检查哪个产品被哪个用户所喜欢,哪个用户喜欢多少产品那么我如何创建我的数据库桌子?



我目前正在做的是:

产品表:product_id(主键),product_name

用户表:user_id(主键),user_name

喜欢表:product_id,user_id



我创建这个像Table一样存储值为任何用户喜欢任何产品,然后我可以轻松地检索哪个用户喜欢哪个产品,这样我就可以实现我的目标,并且对于少量记录工作正常但问题是随着记录的增加,它对于获取数据来说太慢了。所以建议我做一个不影响速度的好方法。



谢谢。

解决方案

< blockquote>您是否已尝试使用主键 [ ^ ]和索引 [ ^ ]。



例如您可以使用产品和用户ID的组合在Likes表上创建复合键。它不仅可以确保您没有重复值,还可以在此表上应用索引。



我希望有所帮助!


Hello Programmers,
I have one question regarding database. Actually i want to create like functionality in my project. I am giving you a scenario, according to this you can give me suitable suggestions.
Suppose i have multiple products(not fixed, say 1000) show on my website...users can view these products and can like any product(one user can like many products and users also not fixed)
If i have to check which product is liked by which user and which user liked how many products then how i create my database table?

What i am currently doing is:
Product Table : product_id(primary key), product_name
User Table : user_id(primary key), user_name
Like Table : product_id, user_id

I create this Like Table to store values as any user like any products and then i can easily retrieve which user like which product so i am able to achieve my goal and its work fine for small number of records but problem is as records increases it goes too slow for fetching data. So suggest me a good way to do it that doesn't affect its speed.

Thanks.

解决方案

Have you already tried using Primary Keys [^]and Indexes[^].

For e.g. you could create a composite key on Likes table using a combination of Product and User Id. Not only it will makes sure that you don't have duplicate values, but will also enable to apply the indexes on this table.

I hope that helps!


这篇关于喜欢按钮功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆