1. A method of optimizing a database query, the method comprising: (a) for a first database query where at least one field is materialized into a first temporary table during execution of the first database query, generating a first statistic for the first temporary table prior to creation of the first temporary table; and (b) optimizing the first database query using the first statistic.