微博关注用户表的设计
查询需求如下:
1.查询关注了哪些用户。
2.查询被哪些用户关注了。
3.查询和某个用户共同的关注列表。
4.查询是否互相关注。
用一个表,以 user_id follow_user_id add_time列的形式可以实现上述功能。
可是关注量比较大,需要按用户 id来分表。不知道怎样设计表结构好。
求助友友们,请多多给建议。 谢谢
回复讨论(解决方案)
以 用户id 来分区
用户ID吧,每个表之间都用用户id
以 用户id 来分区
版主 我是想用用用户id来分表,可是分表后上面的功能就不好实现了。比如我要查 哪些用户关注了我,分表后;我不确定要查哪个表。只有将所有分表都查一次才可以知道。
单独保存一个关注表
或者把关注你的用户也存到用户表里可行不?
正因为分表后不确定要查哪个表,所以不能分表而是分区
分区后的表对你而言依然是一个整表(虽然被分成了多个文件),实际查询的是那个文件由mysql决定而不是你决定
如果你依然想自己分表,那么需要交换 user_id、follow_user_id 列的内容形成新表,再对两个表分表
以 用户id 来分区
版主 我是想用用用户id来分表,可是分表后上面的功能就不好实现了。比如我要查 哪些用户关注了我,分表后;我不确定要查哪个表。只有将所有分表都查一次才可以知道。
不要分表呀,直接写sql语句就可以了
1.查询关注了哪些用户:select `follow_user_id` from `表` where `user_id`='你的id'
2.查询被哪些用户关注了:select `user_id` from `表` where `follow_user_id`='你的id'
3.查询和某个用户共同的关注列表。select * from `表` where `user_id` in ('你的id', '某个用户id')
4.查询是否互相关注:select * from `表` where `user_id`='你的id' and `follow_user_id`='某个用户id' `user_id` in (select `follow_user_id` from `表` where `user_id`='某个用户id')
当然估计还有更优秀的写法,特别是第4条
select * from `表` where `user_id`='你的id' and `follow_user_id`='某个用户id' and `user_id` in (select `follow_user_id` from `表` where `user_id`='某个用户id')
正因为分表后不确定要查哪个表,所以不能分表而是分区
分区后的表对你而言依然是一个整表(虽然被分成了多个文件),实际查询的是那个文件由mysql决定而不是你决定
如果你依然想自己分表,那么需要交换 user_id、follow_user_id 列的内容形成新表,再对两个表分表
谢谢回答 我会参考一下的
正因为分表后不确定要查哪个表,所以不能分表而是分区
分区后的表对你而言依然是一个整表(虽然被分成了多个文件),实际查询的是那个文件由mysql决定而不是你决定
如果你依然想自己分表,那么需要交换 user_id、follow_user_id 列的内容形成新表,再对两个表分表
版主 分区是什么概念呢。吾只懂电脑分区阿
还有这句话可以详细说一下吗
”那么需要交换 user_id、follow_user_id 列的内容形成新表,再对两个表分表“
谢谢啦!
关于表分区可参见 http://www.baidu.com/s?ie=utf-8&bs=mysql%E5%88%86%E5%8C%BA%E8%A1%A8&f=8&rsv_bp=1&wd=mysql%E5%88%86%E5%8C%BA&rsv_sug3=1&rsv_sug=1&rsv_sug1=1&rsv_sug4=26&inputT=796
关注可以是单向的,比如这个
user_id follow_user_id1 21 31 4
按 user_id 分表后,你整检查所有的表才能得到与 follow_user_id 相连的 user_id
所以你需要另有一组以 follow_user_id 分组的表
follow_user_id user_id 2 13 14 1
关于表分区可参见 http://www.baidu.com/s?ie=utf-8&bs=mysql%E5%88%86%E5%8C%BA%E8%A1%A8&f=8&rsv_bp=1&wd=mysql%E5%88%86%E5%8C%BA&rsv_sug3=1&rsv_sug=1&rsv_sug1=1&rsv_sug4=26&inputT=796
关注可以是单向的,比如这个
user_id follow_user_id1 21 31 4
按 user_id 分表后,你整检查所有的表才能得到与 follow_user_id 相连的 user_id
所以你需要另有一组以 follow_user_id 分组的表
follow_user_id user_id 2 13 14 1
版主
"follow_user_id 分组的表" 也是要分表的吧
那这个方案可以支持查,哪些人关注了我吗。
比如我的用户id是1 按follow_user_id 尾数进行分表。
follow_user_tbl_1
follow_user_id user_id
1 1
11 1
21 1
follow_user_tbl_2
follow_user_id user_id
2 1
12 1
22 1
如果 user_id follow_user_id 表示 user_id 被 follow_user_id 关注
那么 follow_user_id user_id 表示 follow_user_id 关注了 user_id
你把主体确定了,事情就清楚了
单独保存一个关注表
或者把关注你的用户也存到用户表里可行不?
单独保存一个关注表不大可行 因为数据量太大啦
关注我的用户存到用户表不合理
如果 user_id follow_user_id 表示 user_id 被 follow_user_id 关注
那么 follow_user_id user_id 表示 follow_user_id 关注了 user_id
你把主体确定了,事情就清楚了
版主 吾听了你的,开始明白你的想法啦。 谢谢了。要分表可能只有这样啦
ollow_user_tbl_1
follow_user_id user_id
1 1
11 1
21 1
follow_user_tbl_2
follow_user_id user_id
2 1
12 1
22 1
楼上说的不就是一个用户关联表么?为什么是两个?一个就可以实现这个功能了。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Alipay PHP...

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...
