<p>Why does the use of temp tables with a SELECT statement improve the logical I/O count? Wouldn't it increase the amount of hits to a database instead of decreasing it. Is this because the 'problem' is broken down into sections? I'd like to know what's going on behind the scenes.</p>

[{'answer_id': 72631, 'author': 'Matthias Winkelmann', 'author_id': 4494, 'author_profile': 'https://Stackoverflow.com/users/4494', 'pm_score': 0, 'selected': False, 'text': '<p>AFAIK, at least with mysql, tmp tables are kept in RAM, making SELECTs much faster than anything that hits the HD</p>\n'}, {'answer_id': 73207, 'author': 'Amy B', 'author_id': 8155, 'author_profile': 'https://Stackoverflow.com/users/8155', 'pm_score': 0, 'selected': False, 'text': "<p>There are a class of problems where building the result in a collection structure on the database side is much preferable to returning the result's parts to the client, roundtripping for each part.</p>\n\n<p>For example:  arbitrary depth recursive relationships (boss of)</p>\n\n<p>There's another class of query problems where the data is not and will not be indexed in a manner that makes the query run efficiently.  Pulling results into a collection structure, which can be indexed in a custom way, will reduce the logical IO for these queries.</p>\n"}, {'answer_id': 73977, 'author': 'Jeremiah Peschka', 'author_id': 11780, 'author_profile': 'https://Stackoverflow.com/users/11780', 'pm_score': 1, 'selected': False, 'text': "<p>I'm going to assume by temp tables you mean a sub-select in a WHERE clause.  (This is referred to as a semijoin operation and you can usually see that in the text execution plan for your query.)  </p>\n\n<p>When the query optimizer encounter a sub-select/temp table, it makes some assumptions about what to do with that data. Essentially, the optimizer will create an execution plan that performs a join on the sub-select's result set, reducing the number of rows that need to be read from the other tables.  Since there are less rows, the query engine is able to read less pages from disk/memory and reduce the amount of I/O required.</p>\n"}, {'answer_id': 75707, 'author': 'Chris Smith', 'author_id': 9073, 'author_profile': 'https://Stackoverflow.com/users/9073', 'pm_score': 3, 'selected': True, 'text': '<p>There\'s no general answer. It depends on how the temp table is being used.</p>\n\n<p>The temp table may reduce IO by caching rows created after a complex filter/join that are used multiple times later in the batch. This way, the DB can avoid hitting the base tables multiple times when only a subset of the records are needed.</p>\n\n<p>The temp table may increase IO by storing records that are never used later in the query, or by taking up a lot of space in the engine\'s cache that could have been better used by other data.</p>\n\n<p>Creating a temp table to use all of its contents once is slower than including the temp\'s query in the main query because the query optimizer can\'t see past the temp table and it forces a (probably) unnecessary <a href="http://msdn.microsoft.com/en-us/library/ms181032.aspx" rel="nofollow noreferrer">spool</a> of the data instead of allowing it to stream from the source tables.</p>\n'}]
['https://Stackoverflow.com/questions/72593', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4466/']