Board index needs rewrite
The board index code needs a rewrite. Firstly the way it tries to grab board moderators is just asking for trouble in terms of multiplying out the number of rows pulled when there are moderators, and this should really be a separate query. Perhaps for each board, indicate whether there are moderators or not and query them only if there are.
More importantly, it's been an issue for some time that InnoDB got the order wrong because InnoDB stores by PK order and SMF has since forever been trying to coerce the boards table into display order. While some workarounds were documented for InnoDB, with more recent changes to MySQL (5.6+), it seems that this predication of ordering won't consistently work even in MyISAM.
Any place we query the boards table, we need to explicitly ensure that we get the ordering correct and don't rely on the old semantics to do it for us.
Hmm, doing a simple query over boards with sorting by board_order and child_level still produces a file sort, although I guess it'll be much faster since it's a much smaller data set?
The idea I had was to create a new function called getTreeOrder which stores the entire category and board tree order in a long term cache and another function called sortBoards which takes an array of boards and sorts it with respect to the order defined. This way we avoid direct order by in heavy queries and still get properly sorted
Also, as far as moderators go, wouldn't a separate single query to fetch the moderators and their names be much faster?
EDIT: Just realised you said the same thing
Considering that you're pulling from two sets of moderator tables (member as moderator, group as moderator, each of those with respective table joins), hiving that off to separate queries would be useful. This, of course, assumes you actually want to display all the moderators on the board index, if you didn't want to display such, you could cut all that stuff out.
This issue was mostly about trying to reorganise the board index to avoid all the mess of board/group moderators to make it faster; #887 covers the actual order-by issue, including discussing all the issues around filesorts and changing the TEXT columns to VARCHARs which should limit the chance of filesorts (filesorts primarily occur when the size of memory that would need to be allocated is too large, and when you deal with such, MySQL will allocate the maximum row size for every row for sorting, so you end up dealing with rows > 64KB per row and filesorts are guaranteed at that point)
It'll still hit filesort if you only fetch smf_boards table (id_board and id_cat), because of the two column order which is not using any indexes. A separate query should be the faster solution here, but even then I can't seem to avoid filesort but this is a lot faster due to the much smaller data set.
Is that before or after you change the TEXT columns not to be TEXT columns?
When I'm not selecting the TEXT columns, does it make a difference?
I'm confused, then. What exactly are you doing?
SELECT id_board, child_level, id_cat
FROM smf_boards
ORDER BY child_level, board_order
Why are you ordering by child_level?
Because that's how the whole tree is ordered, board index doesn't bother with it because it restricts the child_level to 1. The idea being that this query is run once every long time and it caches the index, then uses PHP sorting to sort the fetched boards.
Um, no, the tree is not ordered by child_level. The tree is ordered by board_order and ONLY board_order, seeing how it runs from 1 to n where n is the number of boards you have, meaning that you will be guaranteed to get the correct order if you order by board_order. PostgreSQL and SQLite explicitly already do this, in fact, as per queries with boardindex_fetch_boards, messageindex_fetch_boards, and order_by_board_order; alter_table_boards explicitly tries to force order the table by board_order based on that detail.
Building a long term cache is fine bearing in mind the challenges that have to be faced: there are a number of places that all rely on board ordering, not just the board index; board indexes, message indexes with child boards, jump to, several dropdowns that get the board list (e.g. the dropdown for selecting recycle board), search dialogue, ignore boards dialogue.
Ah, you're right, getBoardTree() confused me. And you're right that there are many areas which rely on board ordering, but since it's a major revision I guess it's about time this gets fixed properly.
This should hopefully fix the issue properly
At a glance it looks good. Do note that you should now remove the definitions of alter_table_boards in the PGSQL/SQLite files since these don't need to be checked as the query is removed.
I would actually question the validity of displaying moderators on the board index/message index (for child boards) and consider actually removing that entirely anyway to simplify things, but if it is to be kept, this looks like a good way to go about it (though I'd wonder whether you'd keep track of whether a given board has moderators and only perform the query if at least one board has moderators to be loaded)
Hmm, board index doesn't display childboard's moderators so I guess they can be left out of the passed list. As far as keeping track goes, I don't know. I guess one can add a counter for moderators on board's table and then skip the query, but considering that the forums which would benefit from this optimisation probably will have moderators I don't think it'll be very beneficial.
Also, thanks for the alter_table_boards tip, removed them
Well, I was looking at it from the other perspective: if you don't have moderators, you don't make that extra query, which for a lot of sites is actually better than the enforcing joining that existed before.
But for my stuff, I just removed the moderators entirely, I don't really see the benefit. It makes everything so cluttered, IMO.