title: MySQL performance boost after create & drop index
body: <p>I have a large MySQL, MyISAM table of around 4 million rows running in a core 2 duo, 8G RAM laptop.</p>
  
  <p>This table has 30 columns including varchar, decimal and int types.</p>
  
  <p>I have an index on a varchar(16). Let's call this column: "indexed_varchar_column".</p>
  
  <p>My query is</p>
  
  <pre><code>SELECT 9 columns FROM the_table WHERE indexed_varchar_column = 'something';
  </code></pre>
  
  <p>It always returns around 5000 rows for every 'something' I query against.</p>
  
  <p>An EXPLAIN to the query returns this:</p>
  
  <pre><code>+----+-------------+-------------+------+----------------------------------------------------+--------------------------------------------+---------+-------+------+-------------+
  | id | select_type | table       | type | possible_keys                                      | key                                        | key_len | ref   | rows | Extra       |
  +----+-------------+-------------+------+----------------------------------------------------+--------------------------------------------+---------+-------+------+-------------+
  |  1 | SIMPLE      | the_table   | ref  | many indexes including indexed_varchar_column      | another_index NOT: indexed_varchar_column! | 19      | const | 5247 | Using where |
  +----+-------------+-------------+------+----------------------------------------------------+--------------------------------------------+---------+-------+------+-------------+
  </code></pre>
  
  <p>First thing is I'm not sure is why another_index is chosen. In fact it chooses an index which is a composite index of indexed_varchar_column and another 2 columns (which form part of the selected ones). Perhaps this makes sense since it may make things a bit faster for not having to read 2 of the columns in the query. The real QUESTION is the <strong>following one</strong>:</p>
  
  <p>The query takes 5 seconds for every 'something' I match. On the 2nd time I query against 'something' it takes 0.15 secs (I guess because the query is being cached). When I run another query against 'something_new' it takes again 5 seconds. So, it is consistent.</p>
  
  <p>THE PROBLEM IS: I discovered that creating an index (another composite index including my indexed_varchar_column) and dropping it again produces that all further queries against new 'something_other' take only 0.15 secs. Please note that 1) I create an index 2) drop it again. So everything is in the same state.</p>
  
  <p>I guess all the operations needed for building and dropping indices make the SQL engine to cache something that is then reused. When I run EXPLAIN on a query after all this I get exactly the same as before.</p>
  
  <p>How can I proceed to understand what is cached in the create-drop index procedure so that I can cache it without manipulating indices?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Following a comment from Marc B that suggested that when mySQL creates an index it internally does a SELECT... I tried the following:</p>
  
  <pre><code>SELECT * FROM my_table;
  </code></pre>
  
  <p>It took 30 secs and returned 4 million rows. The good thing is that all further queries are very fast again (until I reboot the system). Please note that after rebooting the queries are slow again. I guess this is because mySQL is using some sort of OS caching.</p>
  
  <p>Any idea? How can I explicitly cache the table I guess?</p>
  
  <p><strong>UPDATE 2:</strong>
  Perhaps I should have mentioned that this table may be severely fragmented. It's 4 million rows but I remove lots of old fields regularly. I also add new ones. Since I had large gaps in IDs (for the rows deleted) every day I drop the primary index (ID) and create it again with consecutive numbers. The table may be then very fragmented and therefore IO must be an issue... Not sure what to do.</p>