2015-11-03 15 views
0

我有一個作業隊列,它是FIFO,可以增長到0到10MM範圍的記錄。每條記錄都有一些與用戶相關的價值。我有第二個表可以包含具有優先級的USERS。這被工作線程查詢了很多。當按此優先級排序時,這會導致1MM記錄範圍內的查詢緩慢,例如,MariaDB簡單加入無臨時表的訂單

select * 
    from calcqueue 
    LEFT JOIN calc_priority USING(userId) 
    where calcqueue.isProcessing IS NULL 
    order by ISNULL(calc_priority.priority), calc_priority.priority 

對此的運行解釋讓我「Using index condition; Using temporary; Using filesort」。我嘗試這在切換到秤在較大的行數派生表,但我不能讓爲了保持保留這違背真實意圖(但至少使我的服務器迅速)

SELECT * 
    FROM 
     (SELECT priority,p,userId FROM 
       (SELECT calc_priority.priority, 
         qt_uncalc.userId, 
         ISNULL(calc_priority.priority) p 
        from 
         (SELECT userId 
          from calcqueue 
          WHERE isProcessing IS NULL 
        ) qt_uncalc 
        LEFT JOIN calc_priority USING(userId) sortedQ 
        ORDER BY p,sortedQ.priority ASC 
      ) orderedT 

有無論如何要實現這一點只使用派生表? calc_priority可以(並且確實)改變很多。所以在calcqueue插入時添加的優先級是不是一種選擇

+0

你想要返回所有10MM行嗎?也許應該有一個「限制」? –

回答

1

A計劃

蒙克在此:

 (SELECT *, 999999 AS priority 
      from calcqueue 
      LEFT JOIN calc_priority USING(userId) 
      where calcqueue.isProcessing IS NULL 
       AND calc_priority.priority IS NULL 
      LIMIT 10 
    ) 
    UNION ALL 
     (SELECT *, calc_priority.priority 
      from calcqueue 
      JOIN calc_priority USING(userId) 
      where calcqueue.isProcessing IS NULL 
      ORDER BY calc_priority.priority 
      LIMIT 10 
    ) 
    ORDER BY priority 

,包括

LIMIT 10; INDEX(isProcessing, userId) 

我試圖避免與NULL的麻煩。

B計劃

你可以改變總是設置priority到一個合適的值的應用程序,從而避免不得不做UNION

+0

我最終做了計劃B,但我想知道我是否可以測試計劃A. – jbh