First of all, this question concerns MySQL 3.23.58, so keep in mind.
I have 2 tables with the following definition:
Table A: id INT (primary), customer_id INT, offlineid INT
Table B: id INT (primary), name VARCHAR(255)
Now table A contains a range of 65k + records, and table B contains ~ 40 records. In addition to the two primary key indexes in table A, there is an index in the offlineid field. Each table has more fields, but they are not relevant (as I see, ask if necessary) for this query.
The first request was submitted to me for the first time (request time: ~ 22 seconds):
SELECT b.name, COUNT(*) AS orders, COUNT(DISTINCT(a.kundeid)) AS leads
FROM katalogbestilling_katalog a, medie b
WHERE a.offlineid = b.id
GROUP BY b.name
Now each id in medie is associated with a different name, which means that you can group by both id and name. A little testing back and forth helped me with this (request time: ~ 6 seconds):
SELECT a.name, COUNT(*) AS orders, COUNT(DISTINCT(b.kundeid)) AS leads
FROM medie a
INNER JOIN katalogbestilling_katalog b ON a.id = b.offline
GROUP BY b.offline;
"" ( 1 )? offlineid, , , , . EXPLAIN , fileshort ( ). !