我已经编写了这段代码来查询mysql表的customerselections,companies和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
答案 0 :(得分: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
答案 1 :(得分:0)
尝试在companycampaigns (company_id, published)
上定义综合索引并查看EXPLAIN
计划,看看它是否有帮助。