SELECT u.id_users,name,surname,IFNULL(reserve,0),IFNULL(lends,0) FROM users u LEFT JOIN (SELECT id_users,COUNT(*) AS lends FROM lend GROUP BY id_users) AS l ON u.id_users = l.id_users LEFT JOIN (SELECT id_users,COUNT(*) AS reserve FROM reservation GROUP BY id_users) AS r ON r.id_users = u.id_users WHERE name LIKE '%kamil%' LIMIT 600,15 ;
W tabeli lend mam 60 tys. rekordów, a w users ok. 40 tys.,tabela reserve jest póki co pusta. Tabela lend ma pola id_users oraz is_books, nie ma klucza podstawowego ani indeksów, a te dwa pola są kluczami obcymi. Próbowałem dodać index na pole id_users, ale nic to nie zmieniło. Całość wykonuje się obecnie w 1.13 sec, a przydało by się z 6 razy szybciej....