mysql - How to make sql more efficient. Need just the count -


i need find number of users:

  1. who have 1 or more fans
  2. who have 2 fans
  3. who have 3 fans

here sql developed answer #1

  select users.id, count(fans.id)     users      join fans on users.id = fans.user_id group users.id   having count(fans.id) > 0 

above query works getting user records , calculating size of array. has terrible performance when number of users in thousands. how refactor query count of users?

i using mysql, might need project using postgresql.

a subquery nicely.

select    count(user.id)     users     (select count(fans.id) fans user_id = users.id) > 0 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -