简体   繁体   中英

Can this SQL query be made to run any faster?

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!)

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

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