View Single Post
theChaosCoder
Junior Member
Join Date: Oct 2015
Old 10-29-2016 , 06:22   Re: Banlist slow loading query on recent MYSQL versions
Reply With Quote #4

A cache would be indeed great.

So tested the whole query now and even added 45k more rows to sb_bans and its still very fast 0.1 sec.

my query (with a test bans table sb_bans2):
Code:
SELECT bid ban_id, BA.type, BA.ip ban_ip, BA.authid, BA.name player_name, created ban_created, ends ban_ends, length ban_length, reason ban_reason, BA.ureason unban_reason, BA.aid, AD.gid AS gid, adminIp, BA.sid ban_server, country ban_country, RemovedOn, RemovedBy, RemoveType row_type,
			SE.ip server_ip, AD.user admin_name, AD.gid, MO.icon as mod_icon,
			CAST(MID(BA.authid, 9, 1) AS UNSIGNED) + CAST('76561197960265728' AS UNSIGNED) + CAST(MID(BA.authid, 11, 10) * 2 AS UNSIGNED) AS community_id,
			(SELECT count(*) FROM sb_demos as DM WHERE DM.demtype='B' and DM.demid = BA.bid) as demo_count,
			(SELECT count(*) FROM sb_bans2 as BH WHERE (BH.type = BA.type AND BH.type = 0 AND BH.authid = BA.authid AND BH.authid != '' AND BH.authid IS NOT NULL) OR (BH.type = BA.type AND BH.type = 1 AND BH.ip = BA.ip AND BH.ip != '' AND BH.ip IS NOT NULL)) as history_count
	   FROM sb_bans2 AS BA
  LEFT JOIN sb_servers AS SE ON SE.sid = BA.sid
  LEFT JOIN sb_mods AS MO on SE.modid = MO.mid
  LEFT JOIN sb_admins AS AD ON BA.aid = AD.aid
  
   ORDER BY created DESC limit 50
50 rows in set (0.01 sec)

If you run the query with explain, then the slowest queries should be the ones with BH and BA tables. joins have an index:
Code:
+------+--------------------+-------+--------+---------------+---------+---------+-------------------------+-------+--------------------------+
| id   | select_type        | table | type   | possible_keys | key     | key_len | ref                     | rows  | Extra                    |
+------+--------------------+-------+--------+---------------+---------+---------+-------------------------+-------+--------------------------+
|    1 | PRIMARY            | BA    | ALL    | NULL          | NULL    | NULL    | NULL                    | 46136 | Using filesort           |
|    1 | PRIMARY            | SE    | eq_ref | PRIMARY       | PRIMARY | 4       | sourcebans.BA.sid       |     1 |                          |
|    1 | PRIMARY            | MO    | eq_ref | PRIMARY       | PRIMARY | 4       | sourcebans.SE.modid     |     1 | Using where              |
|    1 | PRIMARY            | AD    | eq_ref | PRIMARY       | PRIMARY | 4       | sourcebans.BA.aid       |     1 |                          |
|    3 | DEPENDENT SUBQUERY | BH    | ALL    | authid_2      | NULL    | NULL    | NULL                    | 46136 | Using where              |
|    2 | DEPENDENT SUBQUERY | DM    | eq_ref | PRIMARY       | PRIMARY | 9       | sourcebans.BA.bid,const |     1 | Using where; Using index |
+------+--------------------+-------+--------+---------------+---------+---------+-------------------------+-------+--------------------------+
And a count(*) is very fast, because mysql just reads the table value or the row count value. But also: count(myfield) is much much slower.

Maybe a DB restart can help ^^

You can also check the "advisor-tab" for some performance hints in phpmyadmin. I have the feeling that a var is just too small in your db.

Last edited by theChaosCoder; 10-29-2016 at 06:28.
theChaosCoder is offline