我刚启动我的网站,5 天后它变慢了,加载网站花了将近 30 秒,因为 mysql 数据库变得更大,加上 50-60k 行。
我已经搜索了几个小时的解决方案,但我没有找到任何可以实现来优化我的查询的东西。
$idstack = $_SESSION['ids'];
$stmt = $mysqli->prepare("SELECT
maps.id,
maps.name,
maps.date,
maps.mcversion,
maps.mapid,
maps.description,
maps.java,
maps.bedrock,
maps.schematic,
users.username,
users.rank,
users.verified,
(SELECT COUNT(*) FROM likes WHERE likes.mapid = maps.id) AS likes,
(SELECT COUNT(*) FROM downloads WHERE downloads.mapid = maps.id) AS downloads,
(SELECT COUNT(*) FROM views WHERE views.mapid = maps.id) AS views
FROM maps
INNER JOIN users
ON maps.userid = users.id
WHERE maps.id NOT IN ( '" . implode( "', '" , (array)$idstack ) . "' ) ORDER BY RAND() DESC LIMIT 15");
$stmt->execute();
$result = $stmt->get_result();
这个查询返回我想要的,但是当数据库有数千行时它很慢。知道如何优化它吗?
慕尼黑8549860