0

我编写了这段代码来查询mysql表customerselections、company和companycampaigns。这些表具有必要字段的索引。

查询时间(无缓存)约为 0,30。考虑到并发连接,我认为查询时间很长。是否可以用不同的方式重写以加快查询时间?

SELECT  
customerselections.customer_id, 
customerselections.selectedcompany_id,
customerselections.selection_id,

companycampaigns.campaign_id,
companycampaigns.company_id,
companycampaigns.campaign_title,
companycampaigns.campaign_detail,
companycampaigns.published,

companies.company_logo,
companies.company_id,


FROM  customerselections
LEFT JOIN companies ON customerselections.selectedcompany_id=companies.company_id 
LEFT  JOIN companycampaigns ON  companycampaigns.company_id=companies.company_id AND companycampaigns.published='1'
WHERE customerselections.customer_id='$customerid'  LIMIT $offset,$limit 
4

2 回答 2

0

与选择所有行相比,基于字段customerselections.customer_id的选择较慢,因为服务器不必进行任何过滤。因此,这将产生轻微的推动作用。

SELECT  
customerselections.*,
companycampaigns.*,
companies.*
FROM  customerselections
LEFT JOIN companies ON customerselections.selectedcompany_id=companies.company_id 
LEFT  JOIN companycampaigns ON  companycampaigns.company_id=companies.company_id AND companycampaigns.published='1'
WHERE customerselections.customer_id='$customerid'  LIMIT $offset,$limit 
于 2013-03-31T15:52:24.470 回答
0

尝试定义一个复合索引companycampaigns (company_id, published)并查看该EXPLAIN计划是否有帮助。

于 2013-03-31T16:17:44.017 回答