简单查询需要很长时间才能执行

时间:2016-06-13 19:30:13

标签: mysql sql

我有一个很长但很简单的查询,似乎需要花费相当长的时间才能返回结果(超过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

如果有帮助,我没有在任何列上设置任何索引。我该怎么做才能让这个查询运行得更快?

2 个答案:

答案 0 :(得分:0)

如果您在$(this).data('myForm', myForm); 上放置复合索引,在(title, content, poster)上放置另一个索引,则应该看到加速。

注意。像这样的查询的每个url子句只能使用一个索引。因此,它不会帮助此查询在ORtitlecontent上放置单独的索引。

修改:您的poster结果宣布查询计划程序正在逐个检查大约34K行,以查找结果集。如果您的查询使用索引,EXPLAIN会告诉您。

如果您的查询前缀为EXPLAIN,那么您将获得MySQL以提供一些查询计划程序统计信息。

阅读本文:http://use-the-index-luke.com/

答案 1 :(得分: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
;

虽然,我不得不怀疑为什么你甚至用那个巨大的内容字符串查询?