Jaxel
Well-known member
So I have a query, its a single query, so it doesn't take a lot of resources:
However, something I would like to do is determine the position of each row in the table. So I add a small nested select statement to my SQL query.
So technically this is a single query. But I wonder if this code has a major performance hit because I think it may actually be an extra query PER ROW. So if the script returns 50 rows, it has the performance hit of 51 queries. Am I right in this assumption?
Would there be a better way to get what I'm looking for?
Code:
SELECT EWRgrado_ranks.*, xf_user.*, EWRgrado_ladders.*
FROM EWRgrado_ranks
RIGHT JOIN xf_user ON (xf_user.user_id = EWRgrado_ranks.user_id)
LEFT JOIN EWRgrado_ladders ON (EWRgrado_ladders.ladder_id = EWRgrado_ranks.ladder_id)
WHERE EWRgrado_ranks.rank_active = 1
ORDER BY EWRgrado_ranks.rank_score DESC
However, something I would like to do is determine the position of each row in the table. So I add a small nested select statement to my SQL query.
Code:
SELECT EWRgrado_ranks.*, xf_user.*, EWRgrado_ladders.*,
(SELECT COUNT(*) FROM EWRgrado_ranks AS ranks WHERE ranks.rank_score > EWRgrado_ranks.rank_score) AS position
FROM EWRgrado_ranks
RIGHT JOIN xf_user ON (xf_user.user_id = EWRgrado_ranks.user_id)
LEFT JOIN EWRgrado_ladders ON (EWRgrado_ladders.ladder_id = EWRgrado_ranks.ladder_id)
WHERE EWRgrado_ranks.rank_active = 1
ORDER BY EWRgrado_ranks.rank_score DESC
So technically this is a single query. But I wonder if this code has a major performance hit because I think it may actually be an extra query PER ROW. So if the script returns 50 rows, it has the performance hit of 51 queries. Am I right in this assumption?
Would there be a better way to get what I'm looking for?