最近我有了这个question,一切正常,直到我把它发送到DreamHost的服务器。
下面的查询似乎花了很长时间才执行,我无法弄清楚为什么一次处理这么多行。在我的本地服务器中,在0.3秒内执行了相同的查询。
SELECT feed_entries . *
FROM feed_entries
WHERE
id
IN (
SELECT e.id
FROM feed_entries AS e
INNER JOIN feeds AS f ON e.feed_id = f.id
INNER JOIN entries_categorias AS ec ON ec.entry_id = e.id
INNER JOIN categorias AS c ON ec.categoria_id = c.id
WHERE
e.deleted =0
AND
c.slug
IN ('entenda', 'google')
GROUP BY e.id
HAVING COUNT( DISTINCT ec.id ) =2
)
ORDER BY date DESC
LIMIT 1
这是我从支持团队收到的消息:
好吧,它看起来像是查询 由于这些其他查询而被锁定 像这一样,加工650万 记录,持续11秒。
# Query_time: 11.639269 Lock_time: 0.000192 Rows_sent: 2 Rows_examined:
6509098
use desenvolvimentistas;
SET timestamp=1280325753;
SELECT `e`.*, `f`.`titulo` AS `feedTitulo`, `f`.`url` AS `feedUrl` FROM
`feed_entries` AS `e`
INNER JOIN `feeds` AS `f` ON e.feed_id =f.id WHERE (e.id IN (SELECT
`e`.`id` FROM `feed_entries` AS `e`
INNER JOIN `feeds` AS `f` ON e.feed_id =f.id
INNER JOIN `entries_categorias` AS `ec` ON ec.entry_id =e.id
INNER JOIN `categorias` AS `c` ON ec.categoria_id =c.id WHERE (e.deleted
=0) AND (e.id NOT IN ('', '', '842', '853', '774', '878')) AND
(e.imagem145x145 =1) AND (c.slug IN('destaque-2')) GROUP BY `e`.`id`
HAVING (COUNT(DISTINCT ec.id) =1))) ORDER BY `e`.`date` DESC LIMIT 4;
可能查询需要0.3秒 在您的本地计算机上运行 闲置,但我们的服务器速度很慢 因为它正在运行查询150 一个时间,每个人都在处理 1530万条记录。
我让我们的mysql管理员看了一下 它和他说这是相当的 效率低下并且感到惊讶 数据库甚至可访问,因为 这些查询已被锁定。他 如果说,数据库将被禁用 它继续这种方式和原因 服务器问题。他说你应该 修复检查的行或获取MySQL PS,虽然mysql ps不会修复 它,但反而只会阻止 数据库被禁用。
以下是查询:
# Query_time: 25.944779 Lock_time: 0.000176 Rows_sent: 0 Rows_examined:
15378209
use desenvolvimentistas;
SELECT `feed_entries`.* FROM `feed_entries` WHERE (id IN (SELECT `e`.`id`
FROM `feed_entries` AS `e` INNER JOIN `feeds` AS `f` ON e.feed_id =f.id
INNER JOIN `entries_categorias` AS `ec` ON ec.entry_id =e.id INNER JOIN
`categorias` AS `c` ON ec.categoria_id =c.id WHERE (e.deleted =0) AND
(c.slug IN('entenda','google')) GROUP BY `e`.`id` HAVING (COUNT(DISTINCT
ec.id) =2))) ORDER BY `date` DESC LIMIT 1;
这是另一个需要修复的问题:
# Query_time: 27.010857 Lock_time: 0.000165 Rows_sent: 0 Rows_examined:
15382750
use desenvolvimentistas;
SET timestamp=1280325706;
SELECT `e`.*, `f`.`titulo` AS `feedTitulo`, `f`.`url` AS `feedUrl` FROM
`feed_entries` AS `e`
INNER JOIN `feeds` AS `f` ON e.feed_id =f.id WHERE (e.id IN (SELECT
`e`.`id` FROM `feed_entries` AS `e`
INNER JOIN `feeds` AS `f` ON e.feed_id =f.id
INNER JOIN `entries_categorias` AS `ec` ON ec.entry_id =e.id
INNER JOIN `categorias` AS `c` ON ec.categoria_id =c.id WHERE (e.deleted
=0) AND (c.slug IN('manchete', 'google')) GROUP BY `e`.`id` HAVING
(COUNT(DISTINCT ec.id) =2))) ORDER BY `e`.`date` DESC LIMIT 4;7:18
答案 0 :(得分:1)
我要检查的第一件事是托管服务器上有正确的索引。有时,人们在迁移数据库时忘记移动它们。
答案 1 :(得分:1)
你总是可以缓存它; - )
答案 2 :(得分:1)
理想情况下,每个问题需要一个查询 - 您可以提出的问题数量没有限制,并且可以最大限度地减少与每个查询相关的聊天。
我将您的第一个查询重写为:
SELECT t.*
FROM feed_entries t
WHERE EXISTS(SELECT NULL
FROM feed_entries AS e
JOIN feeds AS f ON e.feed_id = f.id
JOIN entries_categorias AS ec ON ec.entry_id = e.id
JOIN categorias AS c ON ec.categoria_id = c.id
WHERE e.deleted = 0
AND c.slug IN ('entenda', 'google')
AND e.id = t.id
GROUP BY e.id
HAVING COUNT( DISTINCT ec.id ) = 2)
ORDER BY date DESC
LIMIT 1
将JOIN条件中使用的列至少编入索引(如果还没有)。
我看到有关查询运行150次的评论 - 你能详细说明一下吗?
答案 3 :(得分:0)
我尝试从内部查询中创建一个查询。 然后手动将结果id添加到外部on(其中id为(1,2,3,4,5))。 虽然这不会自动提高性能,但您至少知道发生了什么。
他们描述问题的方式,甚至可能是对从外循环检查的每个单个记录测试内循环,这将通过这种方法来修复。虽然我认为MySQL足够聪明不能自己解决这个问题。