I have two tables in MySQL, table1 has 1,013,347 entities and 38 attributes, and table2 has 7,343,905 entities and 10 attributes. In the following query (which is supposed to get the number of rows for pagination), table1.ID is a PK, table2.ID is its FK (both are indexed), and the HAVING clause gets the value if it's over a certain percentage, in this case 50%
SELECT SQL_CALC_FOUND_ROWS *
FROM table1 INNER JOIN table2 ON table1.ID = table2.ID
WHERE table1.attribute1 LIKE 'D%'
GROUP BY table2.ID
HAVING (COUNT(table2.ID) * (100/18)) >= '50'
Even in the simplified state which I posted here, this query takes no less than 5 minutes to run via command line. I know there must be changes I can make to the query, the PHP code (the values '50' and 'D' are assigned via PHP variables), and/or to my MySQL configuration to speed things up (I'm using the latest XAMPP with default configurations). Any help would be greatly appreciated.
EDIT1: All attributes are TINYTEXT except for the ID attributes which are VARCHAR(9).
EDIT2: EXPLAIN SELECT... returns:
+----+-------------+--------+------+---------------+-------------+---------+------+---------+---------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------+------+---------------+-------------+---------+------+---------+---------------------------------+
| 1 | SIMPLE | table2 | ALL | NULL | NULL | NULL | NULL | 7343905 | Using temporary; Using filesort |
| 1 | SIMPLE | table1 | ref | ID | ID | 29 | func | 1 | Using where |
+----+-------------+--------+------+---------------+-------------+---------+------+---------+---------------------------------+
2 rows in set (0.00 sec)