功能截图
之前做法,先查询每一行的前4个字段,然后动态拼接出其他的字段,效率极低,以下是优化后的代码,供参考,只提供一个优化思路,授人以鱼不如授人以渔
后台Sql语句优化(语法仅支持Oracle)
SELECT PROJ_NO, ALL_SECTION, GRAND_SEC, SPCID, FIR_SCH_ID_1166, BEGIN_DATE_1166, END_DATE_1166, FLOAT_CYC_1166, FIR_SCH_ID_1167, BEGIN_DATE_1167, END_DATE_1167, FLOAT_CYC_1167, FIR_SCH_ID_1168, BEGIN_DATE_1168, END_DATE_1168, FLOAT_CYC_1168, FIR_SCH_ID_1169, BEGIN_DATE_1169, END_DATE_1169, FLOAT_CYC_1169, FIR_SCH_ID_1170, BEGIN_DATE_1170, END_DATE_1170, FLOAT_CYC_1170, FIR_SCH_ID_1171, BEGIN_DATE_1171, END_DATE_1171, FLOAT_CYC_1171, FIR_SCH_ID_1172, BEGIN_DATE_1172, END_DATE_1172, FLOAT_CYC_1172, FIR_SCH_ID_1173, BEGIN_DATE_1173, END_DATE_1173, FLOAT_CYC_1173, FIR_SCH_ID_1174, BEGIN_DATE_1174, END_DATE_1174, FLOAT_CYC_1174, FIR_SCH_ID_1175, BEGIN_DATE_1175, END_DATE_1175, FLOAT_CYC_1175, FIR_SCH_ID_1176, BEGIN_DATE_1176, END_DATE_1176, FLOAT_CYC_1176, FIR_SCH_ID_1177, BEGIN_DATE_1177, END_DATE_1177, FLOAT_CYC_1177, FIR_SCH_ID_1178, BEGIN_DATE_1178, END_DATE_1178, FLOAT_CYC_1178, FIR_SCH_ID_1179, BEGIN_DATE_1179, END_DATE_1179, FLOAT_CYC_1179, ALL_SECTION_BEGIN_DATE, ALL_SECTION_BEGIN_DATE2 FROM (SELECT Y.PROJ_NO, Y.SPCID, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1166, Y.FIR_SCH_ID)) FIR_SCH_ID_1166, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1166, Y.BEGIN_DATE)) BEGIN_DATE_1166, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1166, Y.END_DATE)) END_DATE_1166, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1166, Y.FLOAT_CYC)) FLOAT_CYC_1166, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1167, Y.FIR_SCH_ID)) FIR_SCH_ID_1167, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1167, Y.BEGIN_DATE)) BEGIN_DATE_1167, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1167, Y.END_DATE)) END_DATE_1167, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1167, Y.FLOAT_CYC)) FLOAT_CYC_1167, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1168, Y.FIR_SCH_ID)) FIR_SCH_ID_1168, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1168, Y.BEGIN_DATE)) BEGIN_DATE_1168, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1168, Y.END_DATE)) END_DATE_1168, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1168, Y.FLOAT_CYC)) FLOAT_CYC_1168, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1169, Y.FIR_SCH_ID)) FIR_SCH_ID_1169, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1169, Y.BEGIN_DATE)) BEGIN_DATE_1169, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1169, Y.END_DATE)) END_DATE_1169, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1169, Y.FLOAT_CYC)) FLOAT_CYC_1169, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1170, Y.FIR_SCH_ID)) FIR_SCH_ID_1170, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1170, Y.BEGIN_DATE)) BEGIN_DATE_1170, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1170, Y.END_DATE)) END_DATE_1170, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1170, Y.FLOAT_CYC)) FLOAT_CYC_1170, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1171, Y.FIR_SCH_ID)) FIR_SCH_ID_1171, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1171, Y.BEGIN_DATE)) BEGIN_DATE_1171, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1171, Y.END_DATE)) END_DATE_1171, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1171, Y.FLOAT_CYC)) FLOAT_CYC_1171, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1172, Y.FIR_SCH_ID)) FIR_SCH_ID_1172, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1172, Y.BEGIN_DATE)) BEGIN_DATE_1172, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1172, Y.END_DATE)) END_DATE_1172, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1172, Y.FLOAT_CYC)) FLOAT_CYC_1172, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1173, Y.FIR_SCH_ID)) FIR_SCH_ID_1173, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1173, Y.BEGIN_DATE)) BEGIN_DATE_1173, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1173, Y.END_DATE)) END_DATE_1173, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1173, Y.FLOAT_CYC)) FLOAT_CYC_1173, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1174, Y.FIR_SCH_ID)) FIR_SCH_ID_1174, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1174, Y.BEGIN_DATE)) BEGIN_DATE_1174, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1174, Y.END_DATE)) END_DATE_1174, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1174, Y.FLOAT_CYC)) FLOAT_CYC_1174, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1175, Y.FIR_SCH_ID)) FIR_SCH_ID_1175, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1175, Y.BEGIN_DATE)) BEGIN_DATE_1175, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1175, Y.END_DATE)) END_DATE_1175, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1175, Y.FLOAT_CYC)) FLOAT_CYC_1175, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1176, Y.FIR_SCH_ID)) FIR_SCH_ID_1176, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1176, Y.BEGIN_DATE)) BEGIN_DATE_1176, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1176, Y.END_DATE)) END_DATE_1176, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1176, Y.FLOAT_CYC)) FLOAT_CYC_1176, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1177, Y.FIR_SCH_ID)) FIR_SCH_ID_1177, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1177, Y.BEGIN_DATE)) BEGIN_DATE_1177, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1177, Y.END_DATE)) END_DATE_1177, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1177, Y.FLOAT_CYC)) FLOAT_CYC_1177, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1178, Y.FIR_SCH_ID)) FIR_SCH_ID_1178, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1178, Y.BEGIN_DATE)) BEGIN_DATE_1178, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1178, Y.END_DATE)) END_DATE_1178, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1178, Y.FLOAT_CYC)) FLOAT_CYC_1178, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1179, Y.FIR_SCH_ID)) FIR_SCH_ID_1179, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1179, Y.BEGIN_DATE)) BEGIN_DATE_1179, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1179, Y.END_DATE)) END_DATE_1179, MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, 1179, Y.FLOAT_CYC)) FLOAT_CYC_1179, Y.ALL_SECTION, Y.ALL_SECTION_BEGIN_DATE, Y.ALL_SECTION_BEGIN_DATE2, Y.GRAND_SEC FROM (SELECT FSI.PROJ_NO, FSI.SPCID, FSI.STANDA_SCHEDULE_NODE_ID, SSN.NODE_NAME, SSN.NODE_SEQ, FSI.BEGIN_DATE, FSI.END_DATE, FSI.FLOAT_CYC, FSI.ALL_SECTION, FSI.ALL_SECTION_BEGIN_DATE, FSI.ALL_SECTION_BEGIN_DATE2, FSI.FIR_SCH_ID, FSI.GRAND_SEC FROM FIRST_SCHEDULE_INFO_VIEW FSI LEFT JOIN STANDA_SCHEDULE_NODE SSN ON SSN.STANDA_SCHEDULE_NODE_ID = FSI.STANDA_SCHEDULE_NODE_ID WHERE 1 = 1 AND PROJ_NO = 366 AND FVERSION_ID = 26) Y GROUP BY Y.PROJ_NO, Y.SPCID, Y.ALL_SECTION, Y.ALL_SECTION_BEGIN_DATE, Y.GRAND_SEC, Y.ALL_SECTION_BEGIN_DATE2)
后台逻辑,拼接Sql语句
string ssnSql = string.Format("SELECT T.* FROM STANDA_SCHEDULE_NODE T WHERE T.STANDA_SCHEDULE_ID = {1} AND T.IS_DEL = '8' {0} ORDER BY T.NODE_SEQ", StringUtil.isNotNullOrBlank(ssnIds) ? "AND T.STANDA_SCHEDULE_NODE_ID IN (" + ssnIds.Replace(DbUtil.colSplit, ',') + ")" : "", PlanUtil.FirstScheduleS); DataTable ssndt = DbUtil.QueryDT(ssnSql); StringBuilder sb = new StringBuilder(); if (ssndt.Rows.Count > 0) { ListssnList = BeanUtil.ToEntity (ssndt); foreach (Standa_Schedule_Node ssn in ssnList) { sb.Append(string.Format("MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, {0}, Y.FIR_SCH_ID)) FIR_SCH_ID_{0},", ssn.Standa_Schedule_Node_Id)); sb.Append(string.Format("MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, {0}, Y.BEGIN_DATE)) BEGIN_DATE_{0},", ssn.Standa_Schedule_Node_Id)); sb.Append(string.Format("MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, {0}, Y.END_DATE)) END_DATE_{0},", ssn.Standa_Schedule_Node_Id)); sb.Append(string.Format("MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, {0}, Y.FLOAT_CYC)) FLOAT_CYC_{0},", ssn.Standa_Schedule_Node_Id)); sb.Append(string.Format("MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, {0}, Y.E_BEGIN_DATE)) E_BEGIN_DATE_{0},", ssn.Standa_Schedule_Node_Id)); sb.Append(string.Format("MAX(DECODE(Y.STANDA_SCHEDULE_NODE_ID, {0}, Y.E_END_DATE)) E_END_DATE_{0},", ssn.Standa_Schedule_Node_Id)); } } string conditionSql = ""; conditionSql = sb.ToString(); string querySql = @"SELECT {2} FROM (SELECT Y.PROJ_NO, Y.SPCID, {0} Y.ALL_SECTION, Y.ALL_SECTION_BEGIN_DATE, Y.ALL_SECTION_BEGIN_DATE2, Y.GRAND_SEC FROM (SELECT FSI.PROJ_NO, FSI.SPCID, FSI.STANDA_SCHEDULE_NODE_ID, SSN.NODE_NAME, SSN.NODE_SEQ, FSI.BEGIN_DATE, FSI.END_DATE, FSI.E_BEGIN_DATE, FSI.E_END_DATE, FSI.FLOAT_CYC, FSI.ALL_SECTION, FSI.ALL_SECTION_BEGIN_DATE, FSI.ALL_SECTION_BEGIN_DATE2, FSI.FIR_SCH_ID, FSI.GRAND_SEC FROM FIRST_SCHEDULE_INFO_VIEW FSI LEFT JOIN STANDA_SCHEDULE_NODE SSN ON SSN.STANDA_SCHEDULE_NODE_ID = FSI.STANDA_SCHEDULE_NODE_ID {1}) Y GROUP BY Y.PROJ_NO, Y.SPCID, Y.ALL_SECTION, Y.ALL_SECTION_BEGIN_DATE, Y.GRAND_SEC, Y.ALL_SECTION_BEGIN_DATE2)"; if (StringUtil.isNotNullOrBlank(v.Page) && v.Page != "-1") { int total = 0; v.Dt = DbUtil.Query(String.Format(querySql, conditionSql, DbUtil.WHEREOTO + v.getCondition(), StringUtil.nullOrBlankTOStr(v.DFields, "*")), v.Start, v.Limit, ref total); v.Total = total; } else { v.Dt = DbUtil.QueryDT(string.Format(querySql, conditionSql, DbUtil.WHEREOTO + v.getCondition(), StringUtil.nullOrBlankTOStr(v.DFields, "*"))); } return v.Dt;
Oracle Decode函数用法,可参考:https://www.cnblogs.com/Lightning-Kid/p/3954202.html