2016-06-13 2 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 절은 하나의 인덱스 만 사용할 수 있습니다. 따라서이 쿼리는 title, contentposter에 별도의 인덱스를 넣는 데 도움이되지 않습니다.

: EXPLAIN 결과는 쿼리 계획자가 결과 세트를 찾기 위해 거의 34K 개의 행을 검사한다고 발표합니다. 쿼리에서 인덱스를 사용하는 경우 EXPLAIN이 알려줍니다.

접두어에 EXPLAIN을 붙이면 MySQL에서 몇 가지 쿼리 플래너 통계를 얻을 수 있습니다. 다른 화합물 title, poster 하나를 http://use-the-index-luke.com/

+0

죄송합니다. 어떤 게시물에도 색인이 적용되지 않았 음을 분명히하기 위해 게시물을 편집했습니다. – user6461291

+0

'EXPLAIN' 접두어에 관해서는, 나는 이것을 시도했지만 주어진 정보로부터 결론을 내릴 수 있을지 확신하지 못합니다. (나중에 사용하기 위해) : http://i.imgur.com/TV5D0YN.png – user6461291

+0

또한 ,'(title, url, poster)'에 복합 색인을 넣을 수 있습니까? – user6461291

0

나는 url에 인덱스를 추가하고 :

이 읽기. 그렇다면 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 
; 

비록, 내가 조금 궁금해야?

관련 문제