views:

86

answers:

2

I have a table with 8 million records that looks like this:

CREATE TABLE `bdp_dosis` (
  `ID_INSTITUTION` varchar(4) NOT NULL default '',
  `ID_SERVICE` char(2) NOT NULL default '',
  `SECUENCIAL` char(3) NOT NULL default '',
  `TYPE` char(1) NOT NULL default '',
  `DATE_LECTURA` varchar(8) NOT NULL default '',
  `ID_TARJETA` varchar(9) default NULL,
  `DATE_ASIGNACION` varchar(8) NOT NULL default '',
  `DOSIS_1` int(11) default NULL,
  `DOSIS_2` int(11) default NULL,
  `COMMENT` char(1) NOT NULL default '',
  `CATEGORIA` char(2) default NULL,
  `TYPE_TRABAJO` char(2) default NULL,
  PRIMARY KEY  (`ID_INSTITUTION`, `ID_SERVICE`,`SECUENCIAL`, `TYPE`, `DATE_LECTURA`, `DATE_ASIGNACION`, `COMMENT`)) ENGINE=MyISAM

The design of the table can't be changed but some indexes will make it perform less bad with this query:

select d.ID_SERVICE,d.SECUENCIAL,d.TYPE,d.DATE_LECTURA,d.DATE_ASIGNACION,d.DOSIS_1,d.DOSIS_2,d.COMCOMMENT
from bdp_dosis d
where d.ID_INSTITUTION='46C7'
and d.DATE_asignacion>'20080100'
and d.DATE_asignacion<'20081232' and locate(d.COMMENT,'WQ')<>0
order by d.ID_SERVICE,d.SECUENCIAL,d.TYPE,d.fecha_lectura desc

The EXPLAIN command says:

id  select_type  table  type  possible_keys  key      key_len  ref    
1   SIMPLE       d      ref   PRIMARY        PRIMARY  4        const  
rows    Extra
254269  Using where; Using filesort

I tried to put an index at ID_INSTITUTION-ID_SERVICE with no big results. Any idea?

+3  A: 

A key on ID_INSTITUTION and DATE_asignacion is more likely to help.

If that still doesn't get you very far, you might want to look into going to mysql 5.1 and using partitioning.

chaos
+1  A: 

Indices work best on columns in the where clause, since they help filter the result set out. Ergo, indices should be put on ID_INSTITUTION AND DATE_asignacion.

However, those indices will do you little to no good if you have a locate command in your where clause: This will generally kick off a table scan.

For speed purposes, I'd also make DATE_asignacion an integer, since int comparisons are faster than string comparisons.

Eric
Or, y'know, a `DATE`.
chaos
@chaos: Could be a smart date key. Use these all the time in data warehouses.
Eric
A date type would be perfect but it's not my decision.
polyphony