我正在使用 mysql 数据库。查询直接通过工作台工具执行。查询如下所示:
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
数据表有 100 万个条目。该语句的执行持续 7 秒。原因似乎是 ORDER BY 子句。
有人可以给我一个提示如何加快速度。
编辑:对不起,我忘了添加结构:
products:PK 是 productId(它只有 5 个条目)
记录:PK 是 recordId,FK 是 productId
data:PK是dataId,FK是recordId和typeId
dataTypes:PK 是 typeId
Records.received 和所有 PK 和 FK 上都有一个索引。
这是解释的输出:
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"