mysql语句优化

这是年中详细月查询

select DATE_FORMAT(c.orderDate,'%Y') as '年份',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 1  then c.totalAmount else  0 end  ) as '一月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 2  then c.totalAmount else  0 end  ) as '二月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 3  then c.totalAmount else  0 end  ) as '三月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 4  then c.totalAmount else  0 end  ) as '四月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 5  then c.totalAmount else  0 end  ) as '五月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 6  then c.totalAmount else  0 end  ) as '六月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 7  then c.totalAmount else  0 end  ) as '七月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 8  then c.totalAmount else  0 end  ) as '八月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 9  then c.totalAmount else  0 end  ) as '九月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 10  then c.totalAmount else  0 end  ) as '十月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 11  then c.totalAmount else  0 end  ) as '十一月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 12  then c.totalAmount else  0 end  ) as '十二月',
sum(c.totalAmount) as '总金额' 
FROM erp$sales$sales_order c 
LEFT JOIN erp$base$customer k ON c.customer_id=k.id
LEFT JOIN erp$base$customer_category f  ON  k.customer_category_id =f.id 
LEFT JOIN erp$base$area a ON  c.area_id=a.id
where DATE_FORMAT(c.orderDate,'%Y')=2015
UNION ALL select DATE_FORMAT(c.orderDate,'%Y') as '年份',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 1  then c.totalAmount else  0 end  ) as '一月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 2  then c.totalAmount else  0 end  ) as '二月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 3  then c.totalAmount else  0 end  ) as '三月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 4  then c.totalAmount else  0 end  ) as '四月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 5  then c.totalAmount else  0 end  ) as '五月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 6  then c.totalAmount else  0 end  ) as '六月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 7  then c.totalAmount else  0 end  ) as '七月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 8  then c.totalAmount else  0 end  ) as '八月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 9  then c.totalAmount else  0 end  ) as '九月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 10  then c.totalAmount else  0 end  ) as '十月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 11  then c.totalAmount else  0 end  ) as '十一月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 12  then c.totalAmount else  0 end  ) as '十二月',
sum(c.totalAmount) as '总金额' 
FROM erp$sales$sales_order c 
LEFT JOIN erp$base$customer k ON c.customer_id=k.id
LEFT JOIN erp$base$customer_category f  ON  k.customer_category_id =f.id 
LEFT JOIN erp$base$area a ON  c.area_id=a.id
where DATE_FORMAT(c.orderDate,'%Y')=2014

还有别一种更好

select DATE_FORMAT(c.orderDate,'%Y') as '年份',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 1  then c.totalAmount else  0 end  ) as '一月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 2  then c.totalAmount else  0 end  ) as '二月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 3  then c.totalAmount else  0 end  ) as '三月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 4  then c.totalAmount else  0 end  ) as '四月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 5  then c.totalAmount else  0 end  ) as '五月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 6  then c.totalAmount else  0 end  ) as '六月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 7  then c.totalAmount else  0 end  ) as '七月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 8  then c.totalAmount else  0 end  ) as '八月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 9  then c.totalAmount else  0 end  ) as '九月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 10  then c.totalAmount else  0 end  ) as '十月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 11  then c.totalAmount else  0 end  ) as '十一月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 12  then c.totalAmount else  0 end  ) as '十二月',
sum(c.totalAmount) as '总金额' 
FROM erp$sales$sales_order c 
LEFT JOIN erp$base$customer k ON c.customer_id=k.id
LEFT JOIN erp$base$customer_category f  ON  k.customer_category_id =f.id 
LEFT JOIN erp$base$area a ON  c.area_id=a.id
where (DATE_FORMAT(c.orderDate,'%Y')=2015 OR DATE_FORMAT(c.orderDate,'%Y')=2014) GROUP BY DATE_FORMAT(c.orderDate,'%Y');
对于MySQL语句优化,以下是一些建议: 1. 确保正确的索引:创建适当的索引可以显著提高查询性能。根据查询的特点和表的结构,选择合适的索引列和索引类型。 2. 优化查询语句:使用正确的查询语句可以减少数据库的负担。避免使用SELECT *,只选择需要的列。合理使用JOIN,避免多次嵌套子查询。 3. 避免全表扫描:全表扫描会导致性能下降。通过合理使用索引、优化查询语句和分页等方式,尽量避免全表扫描。 4. 使用EXPLAIN分析查询计划:通过使用EXPLAIN命令分析查询计划,可以了解MySQL是如何执行查询的,从而找到潜在的性能问题。 5. 合理配置服务器参数:根据服务器的硬件资源和实际负载情况,合理配置MySQL的参数,如缓冲区大小、连接数等。 6. 避免过度使用触发器和存储过程:触发器和存储过程会增加数据库的负担,应谨慎使用,并定期检查其性能影响。 7. 定期优化表结构:根据实际需求和数据变化情况,定期检查和优化表结构,包括字段类型、表关系等。 8. 合理使用缓存:使用缓存可以减少数据库访问次数,提高性能。根据业务需求,合理选择缓存策略和工具。 9. 定期维护和优化数据库:定期进行数据库维护工作,如备份、索引重建、碎片整理等,保持数据库的健康状态。 请注意,以上建议仅供参考,具体优化策略需要根据实际情况进行分析和调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值