简体   繁体   中英

Total Registered Users Count

What's the most efficient way of counting the total number of registered users on a website?

I was thinking of using the following query, but if this table contained 1000's of users, the execution time will be very long.

mysql_query("SELECT COUNT(*) FROM users");

Instead, I thought of creating a separate table that will hold this value. Each time a new user registers, or a current one deleted, this value gets updated.

My Question:

Is it possible to carry out an INSERT and UPDATE in one query? - The INSERT will be for storing the new users details, and the UPDATE to increment the total users value.

I'm very interested in your thoughts on this.

If there is a better and faster way to find out the total registered users, I'm very interested to know ;

Cheers ;)

You can use triggers to update the value every time you make an INSERT, UPDATE or DELETE.

if this table contained 1000's of users, the execution time will be very long.

I doubt that it would be that slow for thousands of users. If you had millions of users then it would probably be too slow.

And does your count need to be 100% accurate?

If an approximate row count is sufficient, SHOW TABLE STATUS can be used.

(Source)

By the way, if you are using MyISAM then your original query will be close to instant because the row count is stored already with this storage engine.

You don't do an insert and update in one query. but rather, you do them in one "Transaction".

Transactions have a concept of "atomicity", which means that other processes cannot see "part" of the transaction - it is all or nothing.

If this concept is not familiar to you, you may wish to look it up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM