2017-06-08 41 views
2

我想完成的是;在「文檔創建列」字段中查詢'電子郵件'是否'&'通用製作'行',總計與「電子郵件OCR」'documents_created相同。如果沒有,請拉那批。最後,如果附件計數的電子郵件OCR後不到7項在&通用生產的文件被拉到然後返回表示結果SQL範圍條件小於,大於和介於

enter image description here

enter image description here

下面

當前查詢:

use N 
SELECT id, 
     type, 
     NAME, 
     log_time , 
     start_time , 
     documents_created , 
     pages_created, 
     processed, 
     processed_time  

FROM  N_LF_OCR_LOG 

WHERE  

    -- Log time is current day 
    log_time between  CONVERT(date, getdate()) AND CONVERT(datetime,floor(CONVERT(float,getdate()))) + '23:59:00'  

    -- Documents created is NULL or non zero 
    AND (documents_created IS NULL OR documents_created <> 0) 

    or  (documents_created is null and log_time between  CONVERT(date, getdate()) AND CONVERT(datetime,floor(CONVERT(float,getdate()))) + '23:59:00') 

    -- Filter for specific types 
    AND type IN ('Email OCR In', 
      'Universal Production') 

    -- Filter to rows where number of pages and documents created are not equal 
    AND documents_created <2 and pages_created >2 

ORDER BY log_time 
,id asc 
,processed_time asc 

任何想法如何納入這一點?我是一個新手。謝謝

回答

1

創建索引時,您只需指定要建立索引的列。爲範圍查詢或完全匹配創建索引沒有區別。您可以將多個列添加到同一索引,以便所有列都可以從索引中受益,因爲可以選擇當時每個表中只有一個索引來支持查詢。

您可以創建只覆蓋你where -clause索引:

alter table N_LF_OCR_LOG add index test1(log_time, documents_created, type, pages_created); 

或者還可以添加所需的列排序入索引。在索引中的列的順序很重要,必須是相同的查詢排序:

alter table N_LF_OCR_LOG add index test1(log_time, id, processed_time, documents_created, type, pages_created); 

或添加也包含返回的列覆蓋索引,所以你沒有加載任何值從你的表中,並可以通過使用索引來完成查詢。這爲查詢提供了最佳的響應時間。但索引在磁盤上佔用更多空間。

alter table N_LF_OCR_LOG add index test1(log_time, id, processed_time, documents_created, type, pages_created, NAME, start_time, processed); 

使用查詢中的explain關鍵字可以看出索引執行的效果。

+0

謝謝!我試着把它添加到我的where子句中,它有一個錯誤。你能列出它包含在代碼中嗎? – Jenesis

+1

請勿將其添加到您現有的查詢中。這些是分開的陳述。在執行查詢之前,您必須執行一次。每次在表格中自動插入一個新行時,索引都會保持不變並得到更新。 也許您應該閱讀關於添加索引以便更好理解的基礎知識:https://dev.mysql.com/doc/refman/5.7/en/create-index.html – Simulant