Tobias Herkula
Tobias Herkula

Reputation: 135

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...

Upvotes: 0

Views: 80

Answers (2)

KingCronus
KingCronus

Reputation: 4519

SELECT name, type, subtype, Count(*) FROM table GROUP BY name, type, subtype

How about that?

Upvotes: 0

flo
flo

Reputation: 2018

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.

Upvotes: 2

Related Questions