简单查询需要很长时间才能执行
问题描述:
我有一个很长但很简单的查询,似乎要花费很长时间才能返回结果(超过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
我还没有设置任何索引上的任一列是否有帮助。我该怎么做才能使这个查询运行得更快?
答
如果您在(title, content, poster)
上放置复合索引,并且在url
上放置了另一个索引,则应该看到加速。
请注意井。像这样的查询的每个OR
子句只能使用一个索引。因此,它不会帮助此查询在title
,content
和poster
上放置单独的索引。
编辑:您的EXPLAIN
结果宣布查询计划程序逐个检查将近34,000行以查找结果集。如果您的查询使用索引,则EXPLAIN
会告诉您。
如果用EXPLAIN
作为查询的前缀,你会得到MySQL给你一些查询规划器的统计信息。
答
我将在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
;
虽然,我想知道一点点,为什么你甚至由巨大的内容字符串查询?
道歉,我编辑了我的帖子,以清楚地说明没有索引已应用于任何列。 – user6461291
关于以'EXPLAIN'为前缀的查询,我已经试过了,但我不确定我能从给出的信息中得出什么结论(供将来使用):http://i.imgur.com/TV5D0YN.png – user6461291
也,我是否也会在'(标题,网址,海报)'上添加复合索引? – user6461291