This query needs some testing for performance. It might need to be broken into smaller parts.

Also, `$wpdb->comments`, etc is how you should refer to table names for multisite support.