Arithmetic with columns in different tables in mysql [optmization] -
in following query:
select (select nick nicks n n.pid=p.id limit 1 order id desc) nick , ( ( ( select count(*) kills k k.pid = p.id ) + ( select count(*) votos v v.pid = p.id ) ) - (select count(*) deaths d d.pid = p.id ) ) score , (select count(*) kills k k.pid = p.id ) kills , (select count(*) deaths d d.pid = p.id ) deaths , (select count(*) headshots h h.pid = p.id ) headshots , (select count(*) votos v v.pid = p.id ) reputation players p p.uuid='steam_x:x:xxxxxx' group kills
this query works fine... think there exists better way this. can me optimize query?
here better way write query:
select p.*, (kills + reputation - deaths) score (select (select nick nicks n n.pid = p.id order id desc limit 1 ) nick, (select count(*) kills k k.pid = p.id ) kills, (select count(*) deaths d d.pid = p.id ) deaths, (select count(*) headshots h h.pid = p.id ) headshots, (select count(*) votos v v.pid = p.id ) reputation players p p.uuid = 'steam_x:x:xxxxxx' ) p group kills;
note: don't understand group by
doing. aggregating 1 column, rest of columns have indeterminate values. perhaps intend order by
.
i guessing overhead materializing subquery before group by
less additional subqueries. version may have comparable performance.
for either version, want following indexes:
players(uuid)
kills(pid)
deaths(pid)
headshots(pid)
votos(pid)
Comments
Post a Comment