我该如何优化这个mysql查询?

时间:2013-12-05 09:58:52

标签: mysql performance

有人可以帮我优化这个查询吗?我可以添加一些索引,只从修改后的(CURRENT_TIMESTAMP)字段中获取日期?

由于 杰里米

SELECT a.providerName AS Assureur, b.insuranceType AS Produit, c.votedFor, c.votedFor2, c.email, c.comment, c.modified
FROM insuranceproviders AS a, insurancetypes AS b, insurancevotes AS c
WHERE a.id = c.providerId
AND b.id = c.insTypeId

3 个答案:

答案 0 :(得分:1)

以下联接查询看起来更好:

SELECT a.providerName AS Assureur, b.insuranceType AS Produit,
    c.votedFor, c.votedFor2, c.email, c.comment, c.modified
FROM insuranceproviders AS a
    INNER JOIN insurancetypes AS b ON a.id = c.providerID
    INNER JOIN  insurancevotes AS c ON b.id = c.insTypeId

并且您应该添加以下INDEX:

ALTER TABLE insuranceproviders ADD INDEX(id);
ALTER TABLE insurancetypes ADD INDEX (providerID, insTypeId);
ALTER TABLE insurancevotes ADD INDEX(insTypeId);

答案 1 :(得分:1)

您可以尝试以下方法:
[这里我正在做左连接,因为我不知道表结构,而你也可以有内连接]

SELECT a.providerName AS Assureur, b.insuranceType AS Produit, c.votedFor, c.votedFor2, c.email, c.comment, c.modified
FROM insurancevotes AS c
LEFT JOIN insuranceproviders AS a ON   a.id = c.providerId
LEFT JOIN insurancetypes AS b ON b.id = c.insTypeId

答案 2 :(得分:0)

使用a.id,c.providerId,b.id,c.id(如果有)的索引,c.insTypeId

相关问题