views:

33

answers:

1

I'm using mySQL InnoDB tables. The query is simple but it just takes too long to run. Its bound to run even slower once I upload it to a server (not to mention that these two tables will keep growing in size).

The 'author' table size is 3,045 records. The 'book' table size is 5,278 records.

SELECT author.*, count( auth_id ) AS author_count
FROM author
LEFT JOIN book ON author.id = book.auth_id
GROUP BY author.id

I'm wondering if there is some trick I can employ to make the query run at least twice as fast as it is now (it currently takes about 10.5 seconds to run - an eternity when displaying web content!)

A: 

1) Is this returning what you want?

2) You should list in the GROUP BY clause all the fields from the SELECT clause that are not in an agregate function (like COUNT in this case, but also could be AVG or SUM).

eKek0
MySQL does not require all non-aggregate columns to be included in the GROUP BY and will choose a random row to supply the other column values. In this particular case, that's okay because presumably "author" will contribute only a single row to each group.
Larry Lustig
That's why I purposefully did not include the other table columns in the GROUP BY - I know that you normally should but I got away with not doing it.
Steve