Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
mysql> explain SELECT menu_name FROM menu_links WHERE expanded != 0 GROUP BY menu_name;
+----+-------------+------------+-------+---------------+------------------------+---------+------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+-------+---------------+------------------------+---------+------+------+--------------------------+
| 1 | SIMPLE | menu_links | index | NULL | menu_plid_expand_child | 106 | NULL | 5214 | Using where; Using index |
+----+-------------+------------+-------+---------------+------------------------+---------+------+------+--------------------------+
1 row in set (0.00 sec)
mysql> alter table menu_links add index expanded (expanded);
Query OK, 5069 rows affected (0.74 sec)
Records: 5069 Duplicates: 0 Warnings: 0
mysql> explain SELECT menu_name FROM menu_links WHERE expanded != 0 GROUP BY menu_name;
+----+-------------+------------+-------+---------------+----------+---------+------+------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+-------+---------------+----------+---------+------+------+----------------------------------------------+
| 1 | SIMPLE | menu_links | range | expanded | expanded | 2 | NULL | 2 | Using where; Using temporary; Using filesort |
+----+-------------+------------+-------+---------------+----------+---------+------+------+----------------------------------------------+
Comments
Comment #1
robertDouglass CreditAttribution: robertDouglass commentedsubscribbe
Comment #2
irakli CreditAttribution: irakli commentedUsing good-old DISTINCT instead of Group By can further optimize by removing the "filesort":
Comment #3
David StraussThe index should be on (expanded, menu_name), and the query should be rewritten to use greater than, less than, or equality instead of "!=". Agreed on the use of DISTINCT. If we do all that, we should eliminate the temp table, too.
Comment #4
multiplextor CreditAttribution: multiplextor commentedClosed. The reason: expired.
Comment #4.0
multiplextor CreditAttribution: multiplextor commentedadded code tags so you can read this