2013-02-25 33 views
3

我有15個查詢爲報表的目的動態生成頁面上的表的數據。每個查詢需要250到900毫秒,這意味着根據服務器負載,頁面加載時間爲4到13秒。加載時間導致一些用戶認爲該頁面根本不會加載。如何提高ColdFusion MySQL的查詢時間?

我想知道是否有某種方法可以簡化查詢以提供更可接受的加載時間。這裏是其中一個查詢:

   <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> 

回答

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 
+0

嗨,這是拋出錯誤。只有一個表名爲case_meeting – blarg 2013-02-25 09:45:21

+0

PL您能否發佈錯誤 – Meherzad 2013-02-25 09:45:55

+0

您的SQL語法中有錯誤; ('1','2','3','28','29','30','')檢查與您的MySQL服務器版本對應的手冊, 3'在第14行 – blarg 2013-02-25 09:50:57

0

我想在MySql中創建一個過程,並從我的SQL調用它的參數是最合適的。你還可以創建動態PARAM

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 
+0

嗨在這個查詢中提到兩次'meetingID'是不明確的。 – blarg 2013-02-25 09:49:07

+0

完成了一些小修正。再試一次 – Kickstart 2013-02-25 09:57:55

1

如果你有很多不互相依賴的查詢,然後看看cfthread。這將允許您同時運行查詢。

請確保您完全測試。我有一個使用cfthread會對數據庫服務器產生不利影響的經驗。

它仍然值得一試。