I am using a mysql database. The query is executed directly via the workbench tool.
The query looks like following:
SELECT records.received, data.value
FROM product
JOIN records
on product.productId= records.productId
JOIN data
ON records.recordId = data.recordId
JOIN dataTypes
ON data.typeId = dataTypes.typeId
ORDER BY records.received DESC
The data table has 1million entries.
The execution of this statement lasts 7 seconds. The reason seems to be the ORDER BY clause.
Can someone give me a hint how to speed it up.
Edit: sorry I forgot to add the structure:
products: PK is productId (it has only 5 entries)
records: PK is recordId and FK is productId
data: PK is dataId and FK recordId and typeId
dataTypes: PK is typeId
There is an Index on records.received and on all PK and FK.
Here is the output of EXPLAIN:
id,select_type,table,type,possible_keys,key,key_len,ref,rows,Extra
1,SIMPLE,products,const,PRIMARY,PRIMARY,4,const,1,"Using index
1,SIMPLE,dataTypes,const,PRIMARY,PRIMARY,4,const,1,"Using index"
1,SIMPLE,records,ref,"PRIMARY,productId",productId,4,const,127142,"Using where"
1,SIMPLE,data,ref,"recordId,typeId",recordId,4,top70.records.recordId,1,"Using where"
try to index your coloumns on which you are using where clause or selecting the coloumns