3

我有 15 个查询为页面上的表格动态生成数据以用于报告。每个查询需要 250 到 900 毫秒,这意味着页面加载时间为 4 到 13 秒,具体取决于服务器负载。加载时间导致一些用户认为页面根本不会加载。

我想知道是否有某种方法可以简化查询以提供更可接受的加载时间。这是查询之一:

             <cfquery datasource="MeetingDB"  name="One">
              SELECT COUNT( meetingID ) AS countatron
              FROM case_meeting
              WHERE meetingID
              IN (

              SELECT DISTINCT a.meetingID
              FROM case_meeting a
              INNER JOIN meeting b ON a.meetingID = b.meetingID
              WHERE b.categoryID =  '1' 
              AND SUBSTRING( meetingCode, 5, 2 ) 
              BETWEEN 12 
              AND 22 
              AND SUBSTRING( meetingCode, 7, 2 ) 
              BETWEEN 01 
              AND 12 
              AND SUBSTRING( meetingCode, 9, 2 ) 
              BETWEEN 01 
              AND 31
              )
              AND caseID
              IN (
              '1',  '2',  '3', '28',  '29',  '30', '39', '40', '45'
              )
              GROUP BY meetingID
              HAVING COUNT( caseID ) > 0 AND COUNT( caseID ) < 2
              </cfquery>

              <td><cfoutput> #One.recordcount# </cfoutput></td>
4

4 回答 4

5

试试这个查询

SELECT COUNT( a.meetingID ) AS countatron
          FROM case_meeting a, case_meeting b
          WHERE a.meetingID = b.meetingID 
          AND b.categoryID =  '1' 
          AND SUBSTRING( b.meetingCode, 5, 2 ) 
          BETWEEN 12 
          AND 22 
          AND SUBSTRING( b.meetingCode, 7, 2 ) 
          BETWEEN 01 
          AND 12 
          AND SUBSTRING( b.meetingCode, 9, 2 ) 
          BETWEEN 01 
          AND 31
          AND b.caseID
          IN (
          '1',  '2',  '3', '28',  '29',  '30', '39', '40', '45'
          )
          GROUP BY a.meetingID
          HAVING COUNT( a.caseID ) = 1
于 2013-02-25T09:39:18.587 回答
2

可能值得尝试对子选择进行连接而不是使用 IN。

像这样的东西: -

SELECT COUNT( case_meeting.meetingID ) AS countatron
FROM case_meeting
INNER JOIN (
SELECT DISTINCT a.meetingID
FROM case_meeting a
INNER JOIN meeting b ON a.meetingID = b.meetingID
WHERE b.categoryID =  '1' 
AND SUBSTRING( meetingCode, 5, 2 ) BETWEEN 12 AND 22 
AND SUBSTRING( meetingCode, 7, 2 ) BETWEEN 01 AND 12 
AND SUBSTRING( meetingCode, 9, 2 ) BETWEEN 01 AND 31
) Sub1
ON case_meeting.meetingID = Sub1.meetingID
INNER JOIN (
SELECT meetingID, COUNT( caseID ) AS MeetingCaseCount
FROM case_meeting
WHERE  caseID IN ('1',  '2',  '3', '28',  '29',  '30', '39', '40', '45')
GROUP BY meetingID
) Sub2
ON case_meeting.meetingID = Sub2.meetingID
WHERE  Sub2.MeetingCaseCount > 0 AND Sub2.MeetingCaseCount < 2
GROUP BY case_meeting.meetingID
于 2013-02-25T09:46:35.107 回答
1

如果您有很多不依赖于彼此的查询,请查看 cfthread。这将允许您同时运行查询。

确保彻底测试它。我有过使用 cfthread 对数据库服务器产生不利影响的经历。

尽管如此,它仍然值得一试。

于 2013-02-25T13:10:06.970 回答
0

我想在 MySql 中创建一个过程并使用我的 sql 中的参数调用它是最合适的。您还可以使用动态参数创建视图

于 2013-02-25T09:39:11.260 回答