【发布时间】:2012-09-03 13:53:59
【问题描述】:
我正在使用 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 上都有一个索引。
这是 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"
【问题讨论】:
-
你的表上定义了哪些索引?
-
看来您可以从 FROM CLAUSE 中排除表
product。 -
你认为应该多快?
-
抱歉,我添加了更多信息。我希望它少于 1 秒。
-
@danihp:我应该如何将它排除在产品表中?