Note that there are some explanatory texts on larger screens.

plurals
  1. POSimple MySQL indexing issue
    primarykey
    data
    text
    <p>I ve got this table:</p> <pre><code>CREATE TABLE IF NOT EXISTS `test1_nopart` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `idAccount` int(10) unsigned NOT NULL, `data` mediumint(8) unsigned NOT NULL, `date` date NOT NULL, PRIMARY KEY (`id`), KEY `date` (`date`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; </code></pre> <p>I fill this table with 10 000 000 rows. The repartition by date is homogenous</p> <pre><code>EXPLAIN SELECT * FROM `test1_nopart` WHERE date = "2014-03-04" </code></pre> <p>Here is the result</p> <pre><code>id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE test1_nopart ALL NULL NULL NULL NULL 7875981 Using where </code></pre> <p>=> without the explain 3.6sec for a result of 3000 rows (approx)</p> <p>As you can see the index is not used and it is not part of the possible_keys column!</p> <p>the same request with a covering index way</p> <pre><code>EXPLAIN SELECT date FROM `test1_nopart` WHERE date = "2014-03-04" </code></pre> <p>the result:</p> <pre><code>id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE test1_nopart index NULL date 3 NULL 7875981 Using where; Using index </code></pre> <p>=> without the explain 2.8sec for a result of 3000 rows (approx)</p> <p>Why MySQL doesn't use properly this index(DATE) ???</p> <p>Info: - VM Server (our dev environment, I don't know what is the hardware composition) - MySQL 5.5.8</p> <pre><code>SHOW INDEX FROM test1_nopart </code></pre> <p>result:</p> <pre><code>Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment test1_nopart 0 PRIMARY 1 id A 7875981 NULL NULL BTREE test1_nopart 1 date 1 date A 6077 NULL NULL BTREE </code></pre> <ul> <li>For date 2014-03-04 => 3134 rows</li> <li>The total (rollup) => 7 875 488 </li> <li>There is 2556 differents 'date' values in the table</li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload