0

我想结合两个查询的结果。我使用了选择联合,但它强加了我的网站。(记录我的 php 内存限制是 128M)我如何在不强加服务器的情况下组合这些查询的结果?

第一次查询

SELECT mybb_posts.pid, mybb_posts.dateline, mybb_posts.subject, mybb_users.username, mybb_users.avatar, mybb_posts.message
FROM mybb_posts, mybb_users
WHERE mybb_posts.uid = mybb_users.uid AND mybb_posts.fid <> 170 AND mybb_posts.fid <> 77 AND mybb_posts.fid <> 96
ORDER BY mybb_posts.dateline DESC

第二次查询

SELECT ek_yorumlar.id, ek_yorumlar.zaman, ek_yorumlar.icisim, mybb_users.username, mybb_users.avatar, ek_yorumlar.turu
FROM ek_yorumlar, mybb_users
WHERE ek_yorumlar.gonderen = mybb_users.uid
ORDER BY ek_yorumlar.zaman DESC 

联合查询

SELECT * FROM(  
SELECT mybb_posts.pid, mybb_posts.dateline, mybb_posts.subject, mybb_users.username, mybb_users.avatar, mybb_posts.message
FROM mybb_posts, mybb_users
WHERE mybb_posts.uid = mybb_users.uid AND mybb_posts.fid <> 170 AND mybb_posts.fid <> 77 AND mybb_posts.fid <> 96
ORDER BY mybb_posts.dateline DESC
) AS T  
UNION ALL
SELECT * FROM (  
SELECT ek_yorumlar.id, ek_yorumlar.zaman, ek_yorumlar.icisim, mybb_users.username, mybb_users.avatar, ek_yorumlar.turu
FROM ek_yorumlar, mybb_users
WHERE ek_yorumlar.gonderen = mybb_users.uid
ORDER BY ek_yorumlar.zaman DESC 
) AS T 
ORDER BY dateline DESC LIMIT 25
4

1 回答 1

0

您正在达到内存限制,因为此查询将所有符合条件的行添加到内存中,然后将其减少到 25 行。如果首先减少每个组件中的行,它应该通过只查看每个查询的一半然后每个返回 25 行来减少内存负担,因此它只会考虑 50 行。

SELECT * FROM(  
SELECT mybb_posts.pid, mybb_posts.dateline, mybb_posts.subject, mybb_users.username, mybb_users.avatar, mybb_posts.message
FROM mybb_posts
inner join  mybb_users on mybb_posts.uid = mybb_users.uid 
WHERE mybb_posts.fid not in (170 ,77 ,96)
ORDER BY mybb_posts.dateline DESC LIMIT 25
) AS T  
UNION 
SELECT * FROM (  
SELECT ek_yorumlar.id, ek_yorumlar.zaman, ek_yorumlar.icisim, mybb_users.username, mybb_users.avatar, ek_yorumlar.turu
FROM ek_yorumlar
inner join  mybb_users on ek_yorumlar.gonderen = mybb_users.uid
ORDER BY ek_yorumlar.zaman DESC LIMIT 25
) AS T 
ORDER BY dateline DESC LIMIT 25

或者,您可以创建临时表作为减少在任何单个步骤中执行的计算量的一种方式......

create temporary table firsthalf
 SELECT mybb_posts.pid, mybb_posts.dateline, mybb_posts.subject, mybb_users.username, mybb_users.avatar, mybb_posts.message
    FROM mybb_posts
    inner join  mybb_users on mybb_posts.uid = mybb_users.uid 
    WHERE mybb_posts.fid not in (170 ,77 ,96)
    ORDER BY mybb_posts.dateline DESC LIMIT 25;
create temporary table secondhalf
    SELECT ek_yorumlar.id, ek_yorumlar.zaman, ek_yorumlar.icisim, mybb_users.username, mybb_users.avatar, ek_yorumlar.turu
    FROM ek_yorumlar
    inner join  mybb_users on ek_yorumlar.gonderen = mybb_users.uid
    ORDER BY ek_yorumlar.zaman DESC LIMIT 25;

select * from firsthalf
union all 
select * from secondhalf
ORDER BY dateline DESC LIMIT 25
于 2013-06-20T10:41:41.500 回答