For regular CTAS queries, temp data from a SELECT query will be written to to a staging directory under the database location. The code to control this is in SemanticAnalyzer.java
However, CTAS queries allow specifying a LOCATION for the new table. Its possible for this location to be on a different filesystem than the database location. If this happens temp data will be written to the database filesystem and will be copied to the table filesystem in MoveTask.
This extra copying of data can drastically affect performance. Rather than always use the database location as the staging dir for CTAS queries, Hive should first check if there is an explicit LOCATION specified in the CTAS query. If there is, staging data should be stored under the LOCATION directory.