I will try to explain myself quickly. I have a database called 'artikli' which has about 1M records. On this table i run a lots of different queryies but 1 particular is causing problems (long execution time) when ORDER by is present.
This is my table structure:
CREATE TABLE IF NOT EXISTSartikli
(id
int(11) NOT NULL,name
varchar(250) NOT NULL,datum
datetime NOT NULL,kategorije_id
int(11) default NULL,id_valute
int(11) default NULL,podogovoru
int(1) default '0',cijena
decimal(10,2) default NULL,valuta
int(1) NOT NULL default '0',cijena_rezerva
decimal(10,0) NOT NULL,cijena_kupi
decimal(10,0) default NULL,cijena_akcija
decimal(10,2) NOT NULL,period
int(3) NOT NULL default '30',dostupnost
enum('svugdje','samobih','samomojgrad','samomojkanton') default 'svugdje',zemlja
varchar(10) NOT NULL,slike
varchar(500) NOT NULL,od_s
varchar(34) default NULL,od_id
int(10) unsigned default NULL,vrsta
int(1) default '0',trajanje
datetime default NULL,izbrisan
int(1) default '0',zakljucan
int(1) default '0',prijava
int(3) default '0',izdvojen
decimal(1,0) NOT NULL default '0',izdvojen_kad
datetime NOT NULL,izdvojen_datum
datetime NOT NULL,sajt
int(1) default '0', PRIMARY KEY (id
), KEYbrend
(brend
), KEYkanton
(kanton
), KEYdatum
(datum
), KEYcijena
(cijena
), KEYkategorije_id
(kategorije_id
,podogovoru
,sajt
,izdvojen
,izdvojen_kad
,datum
) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
And this is the query:
SELECT artikli.datum as brojx,
artikli.izdvojen as i,
artikli.izdvojen_kad as ii,
artikli.cijena as cijena, artikli.name
FROM artikli
WHERE artikli.izbrisan=0 and artikli.prodano!=3
and artikli.zavrseno=0 and artikli.od_id!=0
and (artikli.sajt=0 or (artikli.sajt=1 and artikli.dostupnost='svugdje'))
and kategorije_id IN (18)
ORDER by i DESC, ii DESC, brojx DESC
LIMIT 0,20
What i want to do is to avoid Filesort which is very slow.
The problem is that you don't have an index on the
izdvojen
,izdvojen_kad
anddatum
columns that are used by theORDER BY
.Note that the large index you have starting with
kategorije_id
can't be used for sorting (although it will help somewhat with the where clause) because the columns you are sorting by are at the end of the index.