如何修复慢速sql查询
I'm making a wp guestbook and i've made a mistake somewhere. At first i've tried to use join but coud not get it to work properly because of all those conditions so i wrote this:
我正在制作一本wp留言簿,我在某个地方犯了一个错误。起初我已经尝试使用join但是由于所有这些条件而无法让它正常工作所以我写了这个:
$query = "
(select *,
(select count(cid) from ctable WHERE nid = vid) as posts,
(select timestamp from ctable where nid = vid order by timestamp desc limit 1) as lt,
(select count(vid) from ntable) as total
FROM ntable
)";
It does exactly what it supossed to do but it is very slow. I know i shout use join but i cant figure it out.
它确实完成了它所做的事情,但它非常缓慢。我知道我大喊使用加入,但我无法弄明白。
3 个解决方案
#1
0
As per my Exp level ..try to use the nolocks as I updated your query that is ,
按照我的Exp级别..来使用nolocks,因为我更新了你的查询,
$query = '(select *,
(select count(cid) from ctable with(nolock) WHERE nid = vid) as posts,
(select timestamp from ctable with(nolock) where nid = vid order by timestamp desc limit 1) as lt,
(select count(vid) from ntable with(nolock)) as total
FROM ntable with(nolock)
)';
and you are using the Order by for with timestamp column and instead of that use getdate()-1
(means days count for the old data so that query will improve its performance.)
并且您使用Order by for timestamp列而不是使用getdate() - 1(表示旧数据的天数,以便查询将提高其性能。)