Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9006595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:27:11+00:00 2026-06-16T01:27:11+00:00

There are two tables as follows in my problem. CREATE TABLE `t_user_relation` ( `User_id`

  • 0

There are two tables as follows in my problem.

CREATE TABLE `t_user_relation` (
  `User_id` INT(32) UNSIGNED NOT NULL  ,
  `Follow_id` INT(32) UNSIGNED NOT NULL ,
  PRIMARY KEY (`User_id`,Follow_id)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

CREATE TABLE `t_user_info`(
    `User_id` int(32) unsigned NOT NULL  ,
    `User_name` varchar(20) NOT NULL ,
    `User_avatar` varchar(60) NOT NULL ,
    `Msg_count` int(32) unsigned DEFAULT '0' ,
    `Fans_count` int(32) unsigned DEFAULT '0' ,
    `Follow_count` int(32) unsigned DEFAULT '0' ,
    PRIMARY KEY (`User_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

What I am trying to do is to update the Fans_count filed of the t_user_info table. My update statement is as follows:

 UPDATE t_user_info set t_user_info.Fans_count=(SELECT COUNT(*) FROM t_user_relation
 WHERE t_user_relation.Follow_id=t_user_info.User_id);

But it execute really slow! The table t_user_info consist of 20,445 records and t_user_relation consist of 1,809,915 records.Can anyone help me improve the speed! Thanks for any advices!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:27:13+00:00Added an answer on June 16, 2026 at 1:27 am

    I would try this:

    UPDATE
      t_user_info inner join
      (SELECT Follow_id, COUNT(*) as cnt
       FROM t_user_relation
       GROUP BY Follow_id) s
      on t_user_info.User_id=s.Follow_id
    SET t_user_info.Fans_count=s.cnt
    

    I’m using a subquery to calculate the count of rows for every Follow_id in table t_user_relation:

    SELECT Follow_id, COUNT(*) as cnt
    FROM t_user_relation
    GROUP BY Follow_id
    

    I am then joining the result of this query with t_user_info, and I am updating Fans_count where the join succeeds, setting it to the count calculated in the subquery.

    A query written like this usually runs faster because the resulting rows from the subquery are calculated only once, before the join, while in your solution your subquery is calculated once for every user row.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are two tables: CREATE TABLE STORE ( ID INTEGER NOT NULL, MEDICINE_ID INTEGER
There are two tables Table 1 select * from manage_tcp; +----+----------+--------+ | id |
There are two tables Table A id column_a 01 abc 01 abc 02 abc
there are two tables tbldoctor and tblschedule tbldoctor doc_id(PK) docMrId docfname 1 22 manish
There are two tables: A, with rows (row_id, column1, ...) and B, with rows
There are two tables. One is users info users, one is comments info comments.
Lets say that there are two tables: Order ID (PK) DateExecuted OrderStep ID (PK)
Say, there are two tables. One contains a field with long text values (for
alt text http://img502.imageshack.us/img502/7245/75088152.jpg There are two tables that I join them together, one of
I need to merge two SELECT statements. There are two tables in my database,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.