Good Day guys, I need to improve this query:
SELECT `glpi_tickets`.`id`,`glpi_tickets`.`date`,
`glpi_tickets`.`date_mod`,`glpi_tickets`.`status`,
`glpi_tickets`.`users_id_recipient`,`glpi_tickets`.`slas_id`,
`glpi_slalevels`.`execution_time`,`glpi_tickets`.`due_date`,
`glpi_tickets`.`name`,`glpi_tickets`.`content`
FROM `glpi_tickets`
JOIN `glpi_slas` on (`glpi_slas`.`id` = `glpi_tickets`.`slas_id`)
JOIN `glpi_slalevels` on (`glpi_slas`.`id` = `glpi_slalevels`.`slas_id`)
LEFT JOIN `mail_sent` ON (`mail_sent`.`ticket_id` = `glpi_tickets`.`id`)
WHERE (`glpi_tickets`.`closedate` IS NULL
OR `glpi_tickets`.`solvedate` IS NULL )
AND `glpi_tickets`.`is_deleted` = 0
AND `mail_sent`.`ticket_id` IS NULL
The problem is that when the DB is big as in a lot of rows then it takes to much memory. Basically i have this tables: glpi_tickets with some fields as you can see on the query. I have glpi_slas and glpi_slalevels, from those two the only field that I am really interested in is glpi_slalevels.execution_time, then glpi_tickets is related to glpi_slalevels through glpi_slas. I need to find if this and know if it has been sent an e-mail already from mail_sent.ticket_id. That is it. But I can't get it done using a memory economical way.
As an additional detail once that mail_sent.ticket_id is already in the table mail_sent, I do not even need to use/read it any more... I can easily forget about it's existence it's a one time task.
Any Idea? Thank you
Edit: Is there a way that I can spit this query in two or three to get the same result in a less memory consuming solution with the same result?
abel_mail_sentand you don't use it at all, you might as well remove it. The only effect it has now, is duplicating rows if you have more than one mail for a ticket. -edit- Oh wait, I see you are referring tomail_sentin the where clause. That seems to be an error, sincemail_sentis not an alias. This query would fail. Anyway, you might try to change it to a 'not exist` to check if it doesn't have mail.glpi_tickets.slas_id" indexed ? could you try adding an index to that ? also the fieldabel_mail_sent.ticket_idmail_sent.ticket_idit's actually important.