此查询有效..一次查询50个mysql数据库并返回最新的10个结果,但是..有没有办法不查询每个状态的整个数据库(我的例子中只列出了4个状态),只是获得前10名(通过timeStamp desc)并使这个查询更有效率?
$query = "
SELECT imgURLThumb, timeStamp, title2, state, postId, title
FROM (
SELECT imgURLThumb,timeStamp, title2, state, postId, title FROM db_washington.md_postings UNION ALL
SELECT imgURLThumb,timeStamp, title2, state, postId, title FROM db_westvirginia.md_postings UNION ALL
SELECT imgURLThumb,timeStamp, title2, state, postId, title FROM db_wisconsin.md_postings UNION ALL
SELECT imgURLThumb,timeStamp, title2, state, postId, title FROM db_wyoming.md_postings
) allposts where imgURLThumb <> 'images/nopicture.png' order by timeStamp DESC LIMIT 0 , 10";
答案 0 :(得分:1)
您应该重新设计数据库,以便所有帖子都在一个数据库中,以便更容易编写此查询。为每个州建立一个数据库看起来很糟糕。您应该为所有帖子创建一个表md_postings
,其中一个字段为State
。
如果那是不可能的,那么我认为你有一个权衡:
如果你想要更好的表现,试试这个:
SELECT imgURLThumb, timeStamp, title2, state, postId, title
FROM (
(SELECT imgURLThumb,timeStamp, title2, state, postId, title
FROM db_washington.md_postings
WHERE imgURLThumb <> 'images/nopicture.png'
ORDER BY timeStamp DESC
LIMIT 10)
UNION ALL
(SELECT imgURLThumb,timeStamp, title2, state, postId, title
FROM db_westvirginia.md_postings
WHERE imgURLThumb <> 'images/nopicture.png'
ORDER BY timeStamp DESC
LIMIT 10)
UNION ALL
(SELECT imgURLThumb,timeStamp, title2, state, postId, title
FROM db_wisconsin.md_postings
WHERE imgURLThumb <> 'images/nopicture.png'
ORDER BY timeStamp DESC
LIMIT 10)
UNION ALL
(SELECT imgURLThumb,timeStamp, title2, state, postId, title
FROM db_wyoming.md_postings
WHERE imgURLThumb <> 'images/nopicture.png'
ORDER BY timeStamp DESC
LIMIT 10)
) AS allposts
WHERE imgURLThumb <> 'images/nopicture.png'
ORDER BY timeStamp DESC LIMIT 10