我正在使用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子句。
有人可以给我一个如何加快速度的提示。
编辑:抱歉,我忘了添加结构:
产品:PK是productId(它只有5个条目)
记录:PK是recordId,FK是productId
数据:PK是dataId和FK recordId和typeId
dataTypes:PK是typeId
在记录上有一个索引。已接受所有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"
答案 0 :(得分:0)
ALTER TABLE `records` ADD INDEX `index1` (`received`);
ALTER TABLE `data` ADD INDEX `index2` (`value `);
尝试索引您正在使用where子句的列或选择列
答案 1 :(得分:0)
尝试将查询更改为:
WHERE records.received > '2012-01-01' -- some date you are sure your values fit
ORDER BY records.received DESC
并比较EXPLAIN语句的结果