我正在尝试使我的数据库更加优化,并且正处于索引它的开始,但不知道如何正确地做到这一点。
我有这个查询:
$year = date("Y");
$thisYear = $year;
//$nextYear = $thisYear + 1;
$sql = mysql_query("SELECT SUM(points) as userpoints
FROM ".$prefix."_publicpoints
WHERE date BETWEEN '$thisYear" . "-01-01' AND '$thisYear" . "-12-31' AND fk_player_id = $playerid");
$row = mysql_fetch_assoc($sql);
$userPoints = $row['userpoints'];
$sql = mysql_query("SELECT
fk_player_id
FROM ".$prefix."_publicpoints
WHERE date BETWEEN '$thisYear" . "-01-01' AND '$thisYear" . "-12-31'
GROUP BY fk_player_id
HAVING SUM(points) > $userPoints");
$row = mysql_fetch_assoc($sql);
$userWrank = mysql_num_rows($sql)+1;
我不知道如何索引这个?我已经尝试索引 fk_player_id 但它仍然查看所有行(287937)。
我已将日期字段编入索引,这在 EXPLAIN 中返回:
1 SIMPLE nf_publicpoints range IDXdate IDXdate 3 NULL 143969 使用 where 和 push 条件;使用临时...
我也有 2 次呼叫同一张桌子......可以一次完成吗?
我如何索引这个和/或它可以做得更聪明?