I am trying to optimize a query in Mysql, now this query needs 0,5 seconds and the table only have 2500 rows. I have 2 tables one table is the tickets and the other the tickets that are grouped.
Tickets:
- ID : Int (Primary Key)
- Name of the ticket: text
GroupTickets:
- ID : Int (Primary Key)
- ID_relation: Int -> the id of the group of the tickets
- ID_Ticket: Int -> the id of the ticket
My query is:
Select T.id, T.name, Count(GP.id_relation)
FROM Tickets as T, GroupTickets as GP
WHERE GP.id_relation IN (
Select id_relation from GroupTickets
Where id_ticket=T.id
)
This select in the where clause make that mysql will do a select by every row so in the future where the table have millions of rows this query will be hard to process.
Am i wrong? Someone know a better way to take this info? I need to know if a ticket is grouped with other tickets in the query.
Best Regards.
INtoEXISTSorINNER JOIN, maybe create an index onidis also nesesarry.