2016-06-13 141 views
-1

我有一個很長但很簡單的查詢,似乎要花費很長時間才能返回結果(超過2秒)。
下面是該查詢:簡單查詢需要很長時間才能執行

SELECT * 
FROM `posts` 
WHERE (`title` = 'Surprise Kanye West performance ends in fans\' disappointment' 
     AND `content` = '<p>It was an only-in-New-York moment: the announcement of a surprise Kanye West performance that drew throngs of people to the streets of Manhattan in the middle of the night. But instead of a concert to remember, the night ended with a hoard of disappointed fans and allegations that police used pepper spray to disperse them.<br/>Popular: <a href=\"http://podcast.cnn.com/anderson-cooper-360/episode/all/065F3vnWEzaATm/ac360-special-2016-01-07.html\" rel=\"noreferrer\" target=\"_blank\">Guns in America</a> | <a href=\"http://podcast.cnn.com/anderson-cooper-360/episode/all/09mJDnGHBvEtl7/6cgs1a.1-1.html\" rel=\"noreferrer\" target=\"_blank\">Sanders Demands Clinton Apologize</a> | <a href=\"http://podcast.cnn.com/fareed-zakaria-gps/episode/all/3m0KewVpkReuAh/gfaw6g.1-1.html\" rel=\"noreferrer\" target=\"_blank\">Blindsided: How ISIS Shook The World</a><br/></p>' 
     AND `poster` = '') 
    OR (`title` = 'Surprise Kanye West performance ends in fans\' disappointment' 
     AND `url` = 'http://www.cnn.com/2016/06/06/entertainment/kanye-west-surprise-concert-canceled/index.html' 
     AND `poster` = '') 
    OR `url` = 'http://www.cnn.com/2016/06/06/entertainment/kanye-west-surprise-concert-canceled/index.html' LIMIT 1; 

下面是該表中的列:

http://i.imgur.com/w9qcpH2.png

我還沒有設置任何索引上的任一列是否有幫助。我該怎麼做才能使這個查詢運行得更快?

回答

0

如果您在(title, content, poster)上放置複合索引,並且在url上放置了另一個索引,則應該看到加速。

請注意井。像這樣的查詢的每個OR子句只能使用一個索引。因此,它不會幫助此查詢在titlecontentposter上放置單獨的索引。

編輯:您的EXPLAIN結果宣佈查詢計劃程序逐個檢查將近34,000行以查找結果集。如果您的查詢使用索引,則EXPLAIN會告訴您。

如果用EXPLAIN作爲查詢的前綴,你會得到MySQL給你一些查詢規劃器的統計信息。

閱讀:http://use-the-index-luke.com/

+0

道歉,我編輯了我的帖子,以清楚地說明沒有索引已應用於任何列。 – user6461291

+0

關於以'EXPLAIN'爲前綴的查詢,我已經試過了,但我不確定我能從給出的信息中得出什麼結論(供將來使用):http://i.imgur.com/TV5D0YN.png – user6461291

+0

也,我是否也會在'(​​標題,網址,海報)'上添加複合索引? – user6461291

0

我將在url添加索引,和其他化合物一個上title, poster。然後,由於MySQL可能會在使用OR查詢的索引時遇到問題。

SELECT * 
FROM `posts` 
WHERE `title` = ? AND `content` = ? AND `poster` = ? 
UNION 
SELECT * 
FROM `posts` 
WHERE `title` = ? AND `poster` = ? 
UNION 
SELECT * 
FROM `posts` 
WHERE `url` = ? 
LIMIT 1 
; 

雖然,我想知道一點點,爲什麼你甚至由巨大的內容字符串查詢?

相關問題