Note that there are some explanatory texts on larger screens.

plurals
  1. POMysql query dont't uses index when there are variables in WHERE
    primarykey
    data
    text
    <p>I have this table:</p> <pre><code>CREATE TABLE `ClientesHora_copy` ( `dia` varchar(6) default NULL, `hora` varchar(2) default NULL, `sit` varchar(17) default NULL, `nodo` varchar(6) default NULL, `clientes` decimal(41,0) default NULL, `segundos` double default NULL, `llamadas` decimal(41,0) default NULL, `fecha` datetime default NULL, KEY `nodo_fecha` (`nodo`,`fecha`), KEY `nodo` (`nodo`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 </code></pre> <p>and this query:</p> <pre><code>SET @sitio= 'ABA000'; SET @horaini='2013-02-12 15:18:00'; SET @horafin='2013-02-12 20:36:00'; EXPLAIN SELECT nodo,sit,clientes,segundos,llamadas,fecha,hora,@horaini AS horaini,@horafin AS horafin FROM `ClientesHora_copy` WHERE nodo =@sitio AND fecha BETWEEN DATE_SUB(DATE_FORMAT(@horaini, "%Y-%m-%d %H:00:00"), INTERVAL 7 DAY) AND DATE_SUB(DATE_FORMAT(@horafin, "%Y-%m-%d %H:00:00"), INTERVAL 7 DAY) </code></pre> <p>I have this in explain</p> <pre><code>id select_type table type possible_keys key key_len ref rows Extra ------ ----------- ----------------- ------ ------------- ------ ------- ------ ------- ------------- 1 SIMPLE ClientesHora_copy ALL (NULL) (NULL) (NULL) (NULL) 2716460 Using where </code></pre> <p>But if I dont use the @sitio variable (but use the @horaini, @horafin variables):</p> <pre><code>EXPLAIN SELECT nodo,sit,clientes,segundos,llamadas,fecha,hora,@horaini AS horaini,@horafin AS horafin FROM `ClientesHora_copy` WHERE nodo ='ABA000' AND fecha BETWEEN DATE_SUB(DATE_FORMAT(@horaini, "%Y-%m-%d %H:00:00"), INTERVAL 7 DAY) AND DATE_SUB(DATE_FORMAT(@horafin, "%Y-%m-%d %H:00:00"), INTERVAL 7 DAY) </code></pre> <p>I get this:</p> <pre><code>id select_type table type possible_keys key key_len ref rows Extra ------ ----------- ----------------- ------ --------------- ---------- ------- ------ ------ ------------- 1 SIMPLE ClientesHora_copy range nodo_fecha,nodo nodo_fecha 18 (NULL) 1 Using where </code></pre> <p>Any idea of why Mysql doesnt use the index with the @sitio variable but it does with @fechaini and @fechafin?</p> <p>Thanks!</p>
    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.
    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