Q:

How to optimize MySQL Querys

I have this table layout:
uuid bigint(20) unsigned primary auto_increment
timestamp int(11) unsigned
name varchar(255)
type enum('A','B')
subtype varchar(255)

And I'm stuck on the query, what I currently use is this:
SELECT name, COUNT(*) as count FROM table GROUP BY name

After this I'm running a loop for each 'name' to get the amount of entries with type 'B' and then again for each subtype. But this end up in about 500k querys and thats too much, there must be an easier way to do this, but I'm really new to this database stuff...

A:

You can use multiple values in GROUP BY.
SELECT name, type, subtype, COUNT(*) as count FROM table GROUP BY name, type, subtype

The result then contains one row for each name, type, subtype combination in the database with the corresponding number of entries.

