原语句特别复杂,10多个表做关联,还有子查询,执行时间13秒多。
SELECT
t.NEW_ID AS id,
t.CAR_MODE AS carMode,
t.CAR_TYPE_ID AS carTypeId,
t.VINCODE AS vinCode,
t.DERAILLEUR_TYPE AS derailleurType,
t.EXHAUST AS exhaust,
t.TONNAGE AS tonnage,
t.`LOAD` AS loadNum,
t.QUOTEDPRICE AS quotedPrice,
t.YIELDLY AS yieldly,
t.MARKET_PRICE AS marketPrice,
t.ASKER_NEW AS asker,
t.ASKER AS askerOld,
t.FILES AS files,
t.FILE_IDS AS fileIds,
t.TELEPHONE AS telephone,
t.IS_EXIGENCE AS isExigence,
t.CAR_CLASS_ID AS carClassId,
t.REQUEST_TYPE_ID AS requestTypeId,
t.CUSTOMER_ID AS customerId,
t.REQUEST_TIME AS requestTime,
t.REQUEST_STATE AS requestState,
t.FLOW_STATE AS flowState,
t.DESCRIPT AS descript,
t.REMARK AS remark,
t.PARENT_REQUEST_ID AS parentRequestId,
t.PERSON AS person,
t.OPEN_DATE AS openDate,
t.IP AS ip,
t.OLD_REQUEST_ID AS oldRequestId,
t.SECOND_FLAG AS secondFlag,
t.UN_DESC_HANDLE AS unDescHandle,
t.UN_FLAG AS unFlag,
t.REQUEST_RESOURCE AS requestResource,
t.SERVICE_GROUP_ID AS serviceGroupId,
t.DATA_TYPE AS dataType,
ct.`DESC` AS carTypeDesc,
cc.`DESC` AS carClassDesc,
u. NAME AS u_name,
u.USERNAME AS u_username,
u.COMPANY_ID AS u_companyId,
c. NAME AS c_name,
p.NEW_ID AS p_id,
p.EMPLOYEE_ID AS p_employeeId,
p.RESULT_DESC AS p_resultDesc,
p.QUERY_TIME AS p_queryTime,
p.END_TIME AS p_endTime,
p.PROCESS_TYPE AS p_processType,
p.FILE_URL AS p_fileUrl,
p.FILE_URL_NEW AS p_fileUrlNew,
p.FILE_IDS AS p_fileIds,
p.FILES AS p_files,
p.VEHICLE_MODEL AS p_vehicleModel,
p.EVALUATE AS p_evaluate,
p.NOTMY AS p_notMy,
f.NEW_ID AS f_id,
f.EMPLOYEE_NAME AS f_employeeName,
f.OPERATION_USER_ID AS f_operationUserId,
f.REMARK AS f_remark,
sv. NAME AS sv_name,
sp. NAME AS sp_name,
sf. NAME AS sf_name,
sfo. NAME AS sfo_name,
pc. NAME AS pc_name
FROM
bs_request_content t
LEFT JOIN dic_car_class cc ON cc.NEW_ID = t.CAR_CLASS_ID
LEFT JOIN dic_car_type ct ON ct.NEW_ID = t.CAR_TYPE_ID
LEFT JOIN auth_client_user u ON u.NEW_ID = t.CUSTOMER_ID
LEFT JOIN bs_company c ON c.NEW_ID = u.COMPANY_ID
LEFT JOIN bs_process p ON p.REQUEST_ID = t.NEW_ID
LEFT JOIN bs_request_forward f ON f.REQUEST_ID = t.NEW_ID
AND f.DEL_FLAG = 0
LEFT JOIN auth_server_user sp ON sp.NEW_ID = p.EMPLOYEE_ID
LEFT JOIN auth_server_user sv ON sv.NEW_ID = p.QUERY_EMPLOYEE_ID
LEFT JOIN auth_server_user sf ON sf.NEW_ID = f.EMPLOYEE_ID
LEFT JOIN auth_server_user sfo ON sfo.NEW_ID = f.OPERATION_USER_ID
LEFT JOIN bs_process_template_class pc ON pc. CODE = p.PROCESS_TYPE
WHERE
1 = 1
AND (
t.REQUEST_RESOURCE = '2'
OR EXISTS (
SELECT
'Y'
FROM
bs_request_forward y
WHERE
y.REQUEST_ID = t.NEW_ID
AND y.DEL_FLAG = '0'
AND y.SERVICE_GROUP_ID = '4028f8ba4a844758014a849540c80001'
)
)
AND NOT EXISTS (
SELECT
'N'
FROM
bs_request_forward n
WHERE
n.REQUEST_ID = t.NEW_ID
AND n.DEL_FLAG = '0'
AND n.SERVICE_GROUP_ID <> '4028f8ba4a844758014a849540c80001'
)
AND t.REQUEST_STATE = '7'
AND t.REQUEST_TIME >= concat('2017-06-01', ' 00:00:00')
AND t.REQUEST_TIME <= concat('2017-06-21', ' 23:59:59')
ORDER BY t.id DESC
LIMIT 0,30;
优化后 0.208s,执行效率提升了62.5倍,改写后的SQL如下:
SELECT
*
FROM
(
SELECT
t.id AS pkid,
t.NEW_ID AS id,
t.CAR_MODE AS carMode,
t.CAR_TYPE_ID AS carTypeId,
t.VINCODE AS vinCode,
t.DERAILLEUR_TYPE AS derailleurType,
t.EXHAUST AS exhaust,
t.TONNAGE AS tonnage,
t.`LOAD` AS loadNum,
t.QUOTEDPRICE AS quotedPrice,
t.YIELDLY AS yieldly,
t.MARKET_PRICE AS marketPrice,
t.ASKER_NEW AS asker,
t.ASKER AS askerOld,
t.FILES AS files,
t.FILE_IDS AS fileIds,
t.TELEPHONE AS telephone,
t.IS_EXIGENCE AS isExigence,
t.CAR_CLASS_ID AS carClassId,
t.REQUEST_TYPE_ID AS requestTypeId,
t.CUSTOMER_ID AS customerId,
t.REQUEST_TIME AS requestTime,
t.REQUEST_STATE AS requestState,
t.FLOW_STATE AS flowState,
t.DESCRIPT AS descript,
t.REMARK AS remark,
t.PARENT_REQUEST_ID AS parentRequestId,
t.PERSON AS person,
t.OPEN_DATE AS openDate,
t.IP AS ip,
t.OLD_REQUEST_ID AS oldRequestId,
t.SECOND_FLAG AS secondFlag,
t.UN_DESC_HANDLE AS unDescHandle,
t.UN_FLAG AS unFlag,
t.REQUEST_RESOURCE AS requestResource,
t.SERVICE_GROUP_ID AS serviceGroupId,
t.DATA_TYPE AS dataType,
ct.`DESC` AS carTypeDesc,
cc.`DESC` AS carClassDesc,
u. NAME AS u_name,
u.USERNAME AS u_username,
u.COMPANY_ID AS u_companyId,
c. NAME AS c_name,
p.NEW_ID AS p_id,
p.EMPLOYEE_ID AS p_employeeId,
p.RESULT_DESC AS p_resultDesc,
p.QUERY_TIME AS p_queryTime,
p.END_TIME AS p_endTime,
p.PROCESS_TYPE AS p_processType,
p.FILE_URL AS p_fileUrl,
p.FILE_URL_NEW AS p_fileUrlNew,
p.FILE_IDS AS p_fileIds,
p.FILES AS p_files,
p.VEHICLE_MODEL AS p_vehicleModel,
p.EVALUATE AS p_evaluate,
p.NOTMY AS p_notMy,
f.NEW_ID AS f_id,
f.EMPLOYEE_NAME AS f_employeeName,
f.OPERATION_USER_ID AS f_operationUserId,
f.REMARK AS f_remark,
sv. NAME AS sv_name,
sp. NAME AS sp_name,
sf. NAME AS sf_name,
sfo. NAME AS sfo_name,
pc. NAME AS pc_name
FROM
bs_request_content t
LEFT JOIN dic_car_class cc ON cc.NEW_ID = t.CAR_CLASS_ID
LEFT JOIN dic_car_type ct ON ct.NEW_ID = t.CAR_TYPE_ID
LEFT JOIN auth_client_user u ON u.NEW_ID = t.CUSTOMER_ID
LEFT JOIN bs_company c ON c.NEW_ID = u.COMPANY_ID
LEFT JOIN bs_process p ON p.REQUEST_ID = t.NEW_ID
LEFT JOIN bs_request_forward f ON f.REQUEST_ID = t.NEW_ID
AND f.DEL_FLAG = 0
LEFT JOIN auth_server_user sp ON sp.NEW_ID = p.EMPLOYEE_ID
LEFT JOIN auth_server_user sv ON sv.NEW_ID = p.QUERY_EMPLOYEE_ID
LEFT JOIN auth_server_user sf ON sf.NEW_ID = f.EMPLOYEE_ID
LEFT JOIN auth_server_user sfo ON sfo.NEW_ID = f.OPERATION_USER_ID
LEFT JOIN bs_process_template_class pc ON pc. CODE = p.PROCESS_TYPE
WHERE
1 = 1
AND (
t.REQUEST_RESOURCE = '2'
OR EXISTS (
SELECT
'Y'
FROM
bs_request_forward y
WHERE
y.REQUEST_ID = t.NEW_ID
AND y.DEL_FLAG = '0'
AND y.SERVICE_GROUP_ID = '4028f8ba4a844758014a849540c80001'
)
)
AND NOT EXISTS (
SELECT
'N'
FROM
bs_request_forward n
WHERE
n.REQUEST_ID = t.NEW_ID
AND n.DEL_FLAG = '0'
AND n.SERVICE_GROUP_ID <> '4028f8ba4a844758014a849540c80001'
)
AND t.REQUEST_STATE = '7'
AND t.REQUEST_TIME >= concat('2017-06-01', ' 00:00:00')
AND t.REQUEST_TIME <= concat('2017-06-21', ' 23:59:59')
) tmp_t
ORDER BY
tmp_t.pkid DESC
LIMIT 0,
30;