查询需求如下:
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')第4条少了一个and,重新写一下
正因为分表后不确定要查哪个表,所以不能分表而是分区
分区后的表对你而言依然是一个整表(虽然被分成了多个文件),实际查询的是那个文件由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
楼上说的不就是一个用户关联表么?为什么是两个?一个就可以实现这个功能了。

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7


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

SublimeText3 Linux new version
SublimeText3 Linux latest version

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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function