소스 검색

开票查询速度优化

徐滕 1 개월 전
부모
커밋
af5a559b23
1개의 변경된 파일131개의 추가작업 그리고 60개의 파일을 삭제
  1. 131 60
      src/main/resources/mappings/modules/workinvoice/WorkInvoiceDao.xml

+ 131 - 60
src/main/resources/mappings/modules/workinvoice/WorkInvoiceDao.xml

@@ -3,7 +3,7 @@
 <mapper namespace="com.jeeplus.modules.workinvoice.dao.WorkInvoiceDao">
 
 	<sql id="workInvoiceColumns">
-		DISTINCT(a.id) AS "id",
+		(a.id) AS "id",
 		a.number AS "number",
 		a.process_instance_id AS "processInstanceId",
 		a.client_id AS "client.id",
@@ -477,8 +477,30 @@
 	<select id="findList" resultType="WorkInvoice" >
 		SELECT
 			<include refid="workInvoiceColumns"/>
-			,wid.number as "widNumber"
-			,wid.total_money as "widTotalMoney"
+		,(SELECT wid.number
+		FROM work_invoice_detail wid
+		<where>
+			a.id = wid.invoice_id
+			<if test="widNumber != null and widNumber != ''">
+				AND wid.number LIKE
+				<if test="dbName == 'oracle'">'%'||#{widNumber}||'%'</if>
+				<if test="dbName == 'mssql'">'%'+#{widNumber}+'%'</if>
+				<if test="dbName == 'mysql'">concat('%',#{widNumber},'%')</if>
+			</if>
+		</where>
+		LIMIT 1) AS widNumber
+		,(SELECT wid.total_money
+		FROM work_invoice_detail wid
+		<where>
+			a.id = wid.invoice_id
+			<if test="widNumber != null and widNumber != ''">
+				AND wid.number LIKE
+				<if test="dbName == 'oracle'">'%'||#{widNumber}||'%'</if>
+				<if test="dbName == 'mssql'">'%'+#{widNumber}+'%'</if>
+				<if test="dbName == 'mysql'">concat('%',#{widNumber},'%')</if>
+			</if>
+		</where>
+		LIMIT 1) AS widTotalMoney
 			,a.is_sz_cloud as "isSzCloud"
 
 		,sa.name as "accountCheckingArea"
@@ -493,15 +515,63 @@
 		JOIN sys_office o ON o.id = a.office_id
 		JOIN sys_office s ON s.id = a.company_id
 		LEFT join sys_office so on so.id = a.office_id
-		left join work_invoice_project_relation wipr on a.id = wipr.invoice_id
-		left join rural_project_records rprs on wipr.project_id = rprs.id
-		left join rural_project_records rpr on rpr.id = wipr.project_id
-		left join project_report_data prds on rpr.id = prds.project_id
-		left join work_invoice_detail wid on a.id = wid.invoice_id
 		LEFT JOIN sys_user sua ON sua.id = a.account_checking_user_id
 		left join sys_area sa on sa.id = a.area_id
 		<where>
 			a.del_flag = #{DEL_FLAG_NORMAL}
+
+			<if test="widNumber != null and widNumber != ''">
+				AND EXISTS (
+				SELECT 1
+				FROM work_invoice_detail wid
+				<where>
+					a.id = wid.invoice_id
+
+					AND wid.number LIKE
+					<if test="dbName == 'oracle'">'%'||#{widNumber}||'%'</if>
+					<if test="dbName == 'mssql'">'%'+#{widNumber}+'%'</if>
+					<if test="dbName == 'mysql'">concat('%',#{widNumber},'%')</if>
+				</where>
+				)
+			</if>
+
+			<!-- 核心优化1:projectName 过滤(替换原有左连接逻辑) -->
+			<if test="projectName != null and projectName != ''">
+				AND EXISTS (
+				SELECT 1
+				FROM work_invoice_project_relation wipr
+				LEFT JOIN rural_project_records rpr ON wipr.project_id = rpr.id
+				WHERE a.id = wipr.invoice_id
+				AND (
+				wipr.details LIKE
+				<if test="dbName == 'oracle'">'%'||#{projectName}||'%'</if>
+				<if test="dbName == 'mssql'">'%'+#{projectName}+'%'</if>
+				<if test="dbName == 'mysql'">concat('%',#{projectName},'%')</if>
+				OR
+				rpr.project_name LIKE
+				<if test="dbName == 'oracle'">'%'||#{projectName}||'%'</if>
+				<if test="dbName == 'mssql'">'%'+#{projectName}+'%'</if>
+				<if test="dbName == 'mysql'">concat('%',#{projectName},'%')</if>
+				)
+				)
+			</if>
+
+			<!-- 核心优化2:reportNumber 过滤(替换原有左连接逻辑) -->
+			<if test="reportNumber != null and reportNumber != ''">
+				AND EXISTS (
+				SELECT 1
+				FROM work_invoice_project_relation wipr
+				LEFT JOIN rural_project_records rpr ON wipr.project_id = rpr.id
+				LEFT JOIN project_report_data prds ON rpr.id = prds.project_id
+				WHERE a.id = wipr.invoice_id
+				AND prds.number LIKE
+				<if test="dbName == 'oracle'">'%'||#{reportNumber}||'%'</if>
+				<if test="dbName == 'mssql'">'%'+#{reportNumber}+'%'</if>
+				<if test="dbName == 'mysql'">concat('%',#{reportNumber},'%')</if>
+				)
+			</if>
+
+
 			<if test="number != null and number != ''">
 				AND a.number LIKE
 				<if test="dbName == 'oracle'">'%'||#{number}||'%'</if>
@@ -635,12 +705,6 @@
 			<if test="(accountCheckingUserId != null and accountCheckingUserId != '') or (accountCheckingUserName != null and accountCheckingUserName != '')">
 				AND (a.account_checking_user_id = #{accountCheckingUserId} or sua.name like concat('%',#{accountCheckingUserName},'%'))
 			</if>
-			<if test="widNumber != null and widNumber != ''">
-				AND wid.number LIKE
-				<if test="dbName == 'oracle'">'%'||#{widNumber}||'%'</if>
-				<if test="dbName == 'mssql'">'%'+#{widNumber}+'%'</if>
-				<if test="dbName == 'mysql'">concat('%',#{widNumber},'%')</if>
-			</if>
 		    <if test="newDrawerId != null and newDrawerId != ''">
 				and new_drawer_id like
 				<if test="dbName == 'oracle'">'%'||#{newDrawerId}||'%'</if>
@@ -653,23 +717,6 @@
 				<if test="dbName == 'mssql'">'%'+#{newDrawer}+'%'</if>
 				<if test="dbName == 'mysql'">concat('%',#{newDrawer},'%')</if>
 			</if>
-			<if test="projectName != null and projectName != ''">
-				and (wipr.details like
-				<if test="dbName == 'oracle'">'%'||#{projectName}||'%'</if>
-				<if test="dbName == 'mssql'">'%'+#{projectName}+'%'</if>
-				<if test="dbName == 'mysql'">concat('%',#{projectName},'%')</if>
-				or rpr.project_name like
-				<if test="dbName == 'oracle'">'%'||#{projectName}||'%'</if>
-				<if test="dbName == 'mssql'">'%'+#{projectName}+'%'</if>
-				<if test="dbName == 'mysql'">concat('%',#{projectName},'%')</if>
-		    	)
-			</if>
-			<if test="reportNumber != null and reportNumber != ''">
-				and prds.number like
-				<if test="dbName == 'oracle'">'%'||#{reportNumber}||'%'</if>
-				<if test="dbName == 'mssql'">'%'+#{reportNumber}+'%'</if>
-				<if test="dbName == 'mysql'">concat('%',#{reportNumber},'%')</if>
-			</if>
 
 
 			${sqlMap.dsf}
@@ -1444,7 +1491,8 @@
 	</select>
 
 	<select id="queryCount" resultType="int" >
-		SELECT count(DISTINCT(a.id))
+
+		SELECT count((a.id))
 		FROM work_invoice a
 		LEFT JOIN sys_user su ON su.id = a.create_by
 		LEFT JOIN work_client_info w ON  w.id = a.client_id
@@ -1454,11 +1502,6 @@
 		JOIN sys_office o ON o.id = a.office_id
 		JOIN sys_office s ON s.id = a.company_id
 		LEFT join sys_office so on so.id = a.office_id
-		left join work_invoice_detail wid on a.id = wid.invoice_id
-		left join work_invoice_project_relation wipr on a.id = wipr.invoice_id
-		left join rural_project_records rprs on wipr.project_id = rprs.id
-		left join rural_project_records rpr on rpr.id = wipr.project_id
-		left join project_report_data prds on rpr.id = prds.project_id
 		LEFT JOIN sys_user sua ON sua.id = a.account_checking_user_id
 <!--		<if test="client != null and client.name != null and client.name != ''">-->
 <!--			LEFT JOIN work_client_info w ON  w.id = a.client_id-->
@@ -1468,6 +1511,57 @@
 <!--		left join project_report_data prd on p.id = prd.project_id-->
 		<where>
 			a.del_flag = #{DEL_FLAG_NORMAL}
+			<if test="widNumber != null and widNumber != ''">
+				AND EXISTS (
+				SELECT 1
+				FROM work_invoice_detail wid
+				<where>
+					a.id = wid.invoice_id
+
+					AND wid.number LIKE
+					<if test="dbName == 'oracle'">'%'||#{widNumber}||'%'</if>
+					<if test="dbName == 'mssql'">'%'+#{widNumber}+'%'</if>
+					<if test="dbName == 'mysql'">concat('%',#{widNumber},'%')</if>
+				</where>
+				)
+			</if>
+
+			<!-- 核心优化1:projectName 过滤(替换原有左连接逻辑) -->
+			<if test="projectName != null and projectName != ''">
+				AND EXISTS (
+				SELECT 1
+				FROM work_invoice_project_relation wipr
+				LEFT JOIN rural_project_records rpr ON wipr.project_id = rpr.id
+				WHERE a.id = wipr.invoice_id
+				AND (
+				wipr.details LIKE
+				<if test="dbName == 'oracle'">'%'||#{projectName}||'%'</if>
+				<if test="dbName == 'mssql'">'%'+#{projectName}+'%'</if>
+				<if test="dbName == 'mysql'">concat('%',#{projectName},'%')</if>
+				OR
+				rpr.project_name LIKE
+				<if test="dbName == 'oracle'">'%'||#{projectName}||'%'</if>
+				<if test="dbName == 'mssql'">'%'+#{projectName}+'%'</if>
+				<if test="dbName == 'mysql'">concat('%',#{projectName},'%')</if>
+				)
+				)
+			</if>
+
+			<!-- 核心优化2:reportNumber 过滤(替换原有左连接逻辑) -->
+			<if test="reportNumber != null and reportNumber != ''">
+				AND EXISTS (
+				SELECT 1
+				FROM work_invoice_project_relation wipr
+				LEFT JOIN rural_project_records rpr ON wipr.project_id = rpr.id
+				LEFT JOIN project_report_data prds ON rpr.id = prds.project_id
+				WHERE a.id = wipr.invoice_id
+				AND prds.number LIKE
+				<if test="dbName == 'oracle'">'%'||#{reportNumber}||'%'</if>
+				<if test="dbName == 'mssql'">'%'+#{reportNumber}+'%'</if>
+				<if test="dbName == 'mysql'">concat('%',#{reportNumber},'%')</if>
+				)
+			</if>
+
 			<if test="number != null and number != ''">
 				AND a.number LIKE
 				<if test="dbName == 'oracle'">'%'||#{number}||'%'</if>
@@ -1602,29 +1696,6 @@
 			<if test="(accountCheckingUserId != null and accountCheckingUserId != '') or (accountCheckingUserName != null and accountCheckingUserName != '')">
 				AND (a.account_checking_user_id = #{accountCheckingUserId} or sua.name like concat('%',#{accountCheckingUserName},'%'))
 			</if>
-			<if test="widNumber != null and widNumber != ''">
-				AND wid.number LIKE
-				<if test="dbName == 'oracle'">'%'||#{widNumber}||'%'</if>
-				<if test="dbName == 'mssql'">'%'+#{widNumber}+'%'</if>
-				<if test="dbName == 'mysql'">concat('%',#{widNumber},'%')</if>
-			</if>
-			<if test="projectName != null and projectName != ''">
-				and (wipr.details like
-				<if test="dbName == 'oracle'">'%'||#{projectName}||'%'</if>
-				<if test="dbName == 'mssql'">'%'+#{projectName}+'%'</if>
-				<if test="dbName == 'mysql'">concat('%',#{projectName},'%')</if>
-				or rpr.project_name like
-				<if test="dbName == 'oracle'">'%'||#{projectName}||'%'</if>
-				<if test="dbName == 'mssql'">'%'+#{projectName}+'%'</if>
-				<if test="dbName == 'mysql'">concat('%',#{projectName},'%')</if>
-				)
-			</if>
-			<if test="reportNumber != null and reportNumber != ''">
-				and prds.number like
-				<if test="dbName == 'oracle'">'%'||#{reportNumber}||'%'</if>
-				<if test="dbName == 'mssql'">'%'+#{reportNumber}+'%'</if>
-				<if test="dbName == 'mysql'">concat('%',#{reportNumber},'%')</if>
-			</if>
 			${sqlMap.dsf}
 		</where>
 		<choose>