SQL优化

SELECT
	tc.id AS tcId,
	tc.contract_code AS contractCode,
	tc.project_name AS projectName,
	sd.dept_name AS deptName,
	su.user_name AS userName,
	tc.singn_date AS singnDate,
	tc.amount AS amount,
	sdd.dict_label AS decorateCom,
	sdd1.dict_label AS brand,
	sdd2.dict_label AS projectType,
	sdd3.dict_label AS projectClass,
	tcost.setup_cost AS setupCost,
	tp.anzhuang AS anzhuang,
	IFNULL( SUM( tic.device_count ), 0 ) AS deviceCount,
	IFNULL( SUM( tic.price ), 0 ) AS price,
	IFNULL( SUM( tic.delivery_cost ), 0 ) AS deliveryCost,
	IFNULL( SUM( tic.amount ), 0 ) AS ticamount,
	IFNULL( SUM( tic.out_device_count ), 0 ) AS toDeviceCount,
	IFNULL( SUM( tic.out_price ), 0 ) AS toPrice,
	IFNULL( SUM( tic.out_amount ), 0 ) AS toAmount,
	IFNULL( SUM( tic.delivery_cost_pay ), 0 ) AS deliveryCostPay,
	tic.worker_type AS workerType,
	tic.installer AS installer,
	tic.debuger AS debuger,
	tic.delivery AS delivery,
	sdd6.dict_label AS worker,
	sdd7.dict_label AS INSTALL,
	sdd9.dict_label AS debug,
	sdd8.dict_label AS delive,
	tr.construct_time AS constructTime,
	tr.debug_time AS debugTime 
FROM
	t_contract tc
	LEFT JOIN t_install_cost tic ON tc.id = tic.contract_id
	LEFT JOIN sys_dept sd ON tc.dept_id = sd.dept_id
	LEFT JOIN sys_user su ON tc.salesman = su.user_id
	LEFT JOIN t_cost tcost ON tc.id = tcost.id
	LEFT JOIN t_record tr ON tc.id = tr.contract_id
	LEFT JOIN ( SELECT dict_value, dict_label FROM sys_dict_data WHERE dict_type = 'sys_install_cost_installer' ) sdd4 ON tic.installer = sdd4.dict_value
	LEFT JOIN ( SELECT dict_value, dict_label FROM sys_dict_data WHERE dict_type = 'sys_install_cost_installer' ) sdd5 ON tic.debuger = sdd5.dict_value
	LEFT JOIN ( SELECT dict_value, dict_label FROM sys_dict_data WHERE dict_type = 'sys_decorate' ) sdd ON tc.decorate_com = sdd.dict_value
	LEFT JOIN ( SELECT dict_value, dict_label FROM sys_dict_data WHERE dict_type = 'sys_brand' ) sdd1 ON tc.brand = sdd1.dict_value
	LEFT JOIN ( SELECT dict_value, dict_label FROM sys_dict_data WHERE dict_type = 'sys_install_cost_type' ) sdd2 ON tic.project_type = sdd2.dict_value
	LEFT JOIN ( SELECT dict_value, dict_label FROM sys_dict_data WHERE dict_type = 'sys_install_cost_class' ) sdd3 ON tic.project_class = sdd3.dict_value
	LEFT JOIN ( SELECT dict_value, dict_label FROM sys_dict_data WHERE dict_type = 'sys_install_cost_worker' ) sdd6 ON tic.worker_type = sdd6.dict_value
	LEFT JOIN ( SELECT dict_value, dict_label FROM sys_dict_data WHERE dict_type = 'sys_install_cost_installer' ) sdd7 ON tic.installer = sdd7.dict_value
	LEFT JOIN ( SELECT dict_value, dict_label FROM sys_dict_data WHERE dict_type = 'sys_install_cost_delivery' ) sdd8 ON tic.delivery = sdd8.dict_value
	LEFT JOIN ( SELECT dict_value, dict_label FROM sys_dict_data WHERE dict_type = 'sys_install_cost_installer' ) sdd9 ON tic.debuger = sdd9.dict_value
	LEFT JOIN ( SELECT contract_id, sum( CASE WHEN pay_obj = '1' THEN amount ELSE 0 END ) anzhuang FROM t_payment GROUP BY contract_id ) tp ON tcost.id = tp.contract_id 
WHERE
	tc.state != '0' 
GROUP BY
	tc.contract_code 
	LIMIT 10 

运行速度:
在这里插入图片描述
在需要where判断的字段添加索引后:

CREATE index sys_dict_data_type_index on sys_dict_data(dict_type);
CREATE index t_contract_state_index on t_contract(state);

运行速度:
在这里插入图片描述

SQL小白,如果还能继续优化或者有什么做得不妥的地方,欢迎大佬指点

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值