-1

我正在使用 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"
4

2 回答 2

0
ALTER TABLE `records` ADD INDEX `index1` (`received`);
ALTER TABLE `data` ADD INDEX `index2` (`value `);

尝试索引您正在使用 where 子句的列或选择列

于 2012-09-03T13:57:45.613 回答
0

尝试将查询更改为:

WHERE records.received > '2012-01-01' -- some date you are sure your values fit
ORDER BY records.received DESC

并比较 EXPLAIN 语句的结果

于 2012-09-03T14:04:09.697 回答