我是新來的高級查詢,所以我可能有一些概念錯誤的,因爲當數據庫有超過100萬的記錄,我得到這樣的迴應ROM我的查詢......MySQL查詢時間太長
ERROR 2013: Lost connection to MySQL server during query
是!它實際上需要很長時間才能在它結束之前嘔吐。
我的查詢是這樣的......
SELECT users.username,
table_1.field_abc, table_1.field_def,
table_2.field_ghi, table_2.field_jkl
FROM users
LEFT JOIN table_1 ON table_1.username = users.username
LEFT JOIN table_2 ON table_2.username = users.username
WHERE
table_1.field_abc REGEXP "(spork|yellow)" OR
table_1.field_def REGEXP "(spork|yellow)" OR
table_2.field_ghi REGEXP "(spork|yellow)" OR
table_2.field_jkl REGEXP "(spork|yellow)"
GROUP BY users.username
ORDER BY
(
(CASE WHEN table_1.field_abc LIKE "%spork%" THEN 1 ELSE 0 END) +
(CASE WHEN table_1.field_abc LIKE "%yellow%" THEN 1 ELSE 0 END) +
(CASE WHEN table_1.field_def LIKE "%spork%" THEN 1 ELSE 0 END) +
(CASE WHEN table_1.field_def LIKE "%yellow%" THEN 1 ELSE 0 END) +
(CASE WHEN table_2.field_ghi LIKE "%spork%" THEN 1 ELSE 0 END) +
(CASE WHEN table_2.field_ghi LIKE "%yellow%" THEN 1 ELSE 0 END) +
(CASE WHEN table_2.field_jkl LIKE "%spork%" THEN 1 ELSE 0 END) +
(CASE WHEN table_2.field_jkl LIKE "%yellow%" THEN 1 ELSE 0 END)
)DESC;
我在http://sqlfiddle.com/#!2/cbbda/28
,因爲只有少數記錄在sqlfiddle樣品運行快,但發佈了一個樣本數據集(只有少數記錄)我嘗試在自己的服務器上覆制記錄,並且查詢只運行了幾條記錄,並且在添加一百萬條記錄後非常緩慢。
是否有任何可能的方法讓我的結果更快?
這看起來像一個有趣的查詢......嘿......你有比較列上的索引? – 99823
+1實際上花時間縮進你的sql很好 –
@Loren請原諒我...我是新的...索引? –