SQL optimization tips -
i beginner sql , have performance problems query. tips? query running on huge database...
select z_screenshots.guid, z_screenshots.player_name, z_screenshots.server, z_screenshots.map, z_screenshots.created, z_screenshots.uploaded, z_screenshots.uploader_id, z_screenshots.filesize z_screenshots inner join ( select clients.guid clients clients.id not in ( select clients.id clients inner join ( select client_id penalties penalties.inactive = 0 and(penalties.type = 'ban' or (penalties.type = 'tempban' , from_unixtime(penalties.time_expire) > now()) ) group penalties.client_id ) penalties on clients.id = penalties.client_id ) ) clients on z_screenshots.guid = clients.guid order z_screenshots.uploaded desc limit ?, ?;
i advise rewrite query.
select s.guid, s.player_name, s.server, s.map, s.created, s.uploaded, s.uploader_id, s.filesize z_screenshots s inner join clients c on s.guid = c.guid left join ( select distinct client_id penalties penalties.inactive = 0 , (penalties.type = 'ban' or (penalties.type = 'tempban' , from_unixtime(penalties.time_expire) > now()) ) ) p on c.id = p.client_id p.client_id null; order s.uploaded desc limit ?, ?;
when don't have client_id
, query okay:
select s.guid, s.player_name, s.server, s.map, s.created, s.uploaded, s.uploader_id, s.filesize z_screenshots s inner join clients c on s.guid = c.guid c.id not in ( select distinct client_id penalties penalties.inactive = 0 , (penalties.type = 'ban' or (penalties.type = 'tempban' , from_unixtime(penalties.time_expire) > now()) ) ) order s.uploaded desc limit ?, ?;
regarding indexes, it's hard tell. clients
, doesn't sound have distinct values. index better, closer result of formula gets 1.
count(distinct <column>) / count(<column>)
meaning, when have 5 distinct values , 5000000 rows, mysql (assuming because of limit
clause) think it's cheaper read entire table. if case here, might consider having index on z_screenshots.uploaded
instead, mysql @ least doesn't have sort. read explain
know when index chosen , index is.
Comments
Post a Comment