我们在具有以下规格的专用服务器上运行我们的系统
- 双至强 2.8Ghz
- 8GB 内存
- 2 个 250GB 存储空间
- 1TB 每月数据传输
我所有的代码都在 php 中,数据库是 mysql
目前我们的数据库中有大约 50 万条记录;这可能每年至少增加 50 万
我正面临一个问题,即我们的一个主页的加载时间非常长。我注意到在他们的本地服务器上加载时间是 2 秒,在我们的服务器上是 18 秒。在本地服务器上使用相同的查询和服务器数据库的精确副本。
您将采取哪些步骤来找出加载时间差异如此之大的原因?
通过我们系统的每个调用都被加载到数据库中。目前有 370k 条记录
每个呼叫都与一个活动、资费和定价模型相关联。
我们的查询计算:
a) 每个活动的总通话次数,
b) 每个活动的总通话费用
问题在于,过去的大部分数据(通话费用)可以通过更新资费、定价、活动开始日期、结束日期等来改变。这导致了一些不稳定,因为表格包括通话费用 另一种方法是计算即时通话费用。所以每次有查询时都要在代码中计算。但是有 40 万条记录,而且每年可能会增加一百万条记录
做这个的最好方式是什么?请参阅下面的查询字符串:
a) 平台活动线索数据查询
SELECT c.tariff, c.campaign_name, c.ad_status, c.ad_control, cd.company, cd.customer_name, pm.pricing_model_name, c.start_date, c.end_date,
(
SELECT p.payment_date
FROM payments p
WHERE p.campaign_id = c.campaign_id
ORDER BY p.payment_date DESC
LIMIT 1) AS LastPaidOn,
(
SELECT p.payment_amount
FROM payments p
WHERE p.payment_status = 'Complete' AND p.campaign_id = c.campaign_id
ORDER BY p.payment_date DESC
LIMIT 1) AS LastPaidAmount,
(CASE WHEN NOW() BETWEEN c.start_date AND c.end_date THEN 'Live' ELSE 'Dead' END) AS STATUS,
(CASE WHEN c.group_name = c.campaign_id THEN '' ELSE c.group_name END)
AS group_name_processed,
(
SELECT IFNULL(SUM(call_leads_purchased),0)
FROM payments
WHERE campaign_id = c.campaign_id AND payment_status = 'Complete') AS CallLeadsPurchased,
(
SELECT IFNULL(SUM(cd.call_leads_used),0)
FROM call_data cd
WHERE cd.campaign_id = c.campaign_id) AS CallLeadsUsed
FROM campaign_data c
JOIN customer_details cd ON cd.customer_id = c.customer_id
JOIN pricing_model pm ON pm.pricing_model_id = c.pricing_model_id
HAVING STATUS = 'Live'
ORDER BY c.campaign_name
b) 仪表板查询
/来电摘要/
SELECT
(
SELECT SUM(call_leads_used)
FROM call_data cd
WHERE 1 = 1 AND cd.call_date >= '2010-03-06' AND campaign_id IN(6)) AS SummaryCallLeadsUsed,
(
SELECT COUNT(*)
FROM call_data cd
WHERE 1 = 1 AND cd.call_date >= '2010-03-06' AND campaign_id IN(6)) AS SummaryReceivedCalls,
(
SELECT COUNT(*)
FROM call_data cd
WHERE 1 = 1 AND cd.call_date >= '2010-03-06' AND campaign_id IN(6) AND cd.call_status = 'Unanswered') AS SummaryUnanswered,
(
SELECT SUM(call_leads_used)
FROM call_data cd
WHERE campaign_id IN(6)) AS TotalCallLeadsUsed,
(
SELECT SUM(call_leads_purchased)
FROM payments
WHERE campaign_id IN(6) AND payment_status = 'Complete') AS TotalLeadsPurchased
/对于饼图/
SELECT
'Free Calls' AS TYPE, COUNT(*) AS calls
FROM call_data cd,
campaign_data cad
WHERE cd.campaign_id = cad.campaign_id AND cd.call_date >= '2010-03-06' AND cd.is_processed = 1 AND cd.applied_charges = 0 AND cad.customer_id = 6
GROUP BY cad.customer_id UNION
SELECT
'Charged Calls' AS TYPE, COUNT(*) AS calls
FROM call_data cd,
campaign_data cad
WHERE cd.campaign_id = cad.campaign_id AND cd.call_date >= '2010-03-06' AND cd.is_processed = 1 AND cd.applied_charges > 0 AND cad.customer_id = 6
GROUP BY cad.customer_id
/*For Calls per day chart*/
SELECT COUNT(*) AS calls, DATE_FORMAT(call_date, '%Y-%m-%d') AS call_day
FROM call_data cd,
campaign_data cad
WHERE cd.campaign_id = cad.campaign_id AND cd.call_date >= '2010-03-06' AND cd.is_processed = 1 AND cad.customer_id = 6
GROUP BY call_day
ORDER BY call_day
/*For total pages */
SELECT COUNT(*) AS expr
FROM call_data cd,
campaign_data cad
WHERE cd.campaign_id = cad.campaign_id AND cd.is_processed = 1 AND cd.call_date >= '2010-03-06' AND cad.customer_id = 6
/*Calls data*/
SELECT
cad.campaign_name,
cd.call_date,
cd.caller_id,
cd.call_duration,
cd.call_status,
cd.call_duplication,
cd.applied_charges
FROM call_data cd,
campaign_data cad
WHERE cd.campaign_id = cad.campaign_id AND cd.is_processed = 1 AND cd.call_date >= '2010-03-06' AND cad.customer_id = 6
ORDER BY cd.call_date DESC
LIMIT 0, 20