Improve Mysql query performance

Asked

Viewed 75 times

2

I have this query where I do left join to three tables and returns the data from the 3 tables:

SELECT Carro, Produto, Sim, DataP, Stock, Obsevacao, Colaborador, DATE(`Data`) AS `Data`,
DATE_FORMAT(`Data`,'%H:%i:%S') AS Hora
FROM centrodb.Registolistagem AS C LEFT OUTER JOIN centrodb.Registolistagem1 AS D
ON D.IdList = C.Id
LEFT OUTER JOIN centrodb.Registolistagem2 AS E
ON E.IdLista = C.Id 

So far so good, but now this query I intend that in each line returned return me the collaborator who made the previous registration and I do it this way (making a subquery to create the column with the previous contributor):

SELECT Carro, Produto, Sim, DataP, Stock, Obsevacao, Colaborador, DATE(`Data`) AS `Data`,
DATE_FORMAT(`Data`,'%H:%i:%S') AS Hora,
(SELECT Colaborador FROM centrodb.Registolistagem AS A LEFT OUTER JOIN centrodb.Registolistagem2 AS B
ON B.IdLista = A.Id
WHERE A.Carro = C.Carro ORDER BY A.Id DESC LIMIT 1,1) AS `Colaborador Anterior`
FROM centrodb.Registolistagem AS C LEFT OUTER JOIN centrodb.Registolistagem1 AS D
ON D.IdList = C.Id
LEFT OUTER JOIN centrodb.Registolistagem2 AS E    
ON E.IdLista = C.Id

When do I run the query, if made WHERE and indicate that I intend to return a week, returns the data, but if ask to return a month is very time consuming and is not functional.

How can I improve the performance of query?

Tables

Registolistagem

Id, Car, Selected
1, G3 Ward A, X
2, G3 Ward A, X

Registolistagem1

Id, Idlist, Product, Yes, Datap, Stock, Observation
1, 1, 1 Liquid Soap, Ok,,,,
2, 1, 1 Body Lotion, Okay,,,,
3, 2, 1 Liquid Soap, Ok,,,,
4, 2, 1 Body Lotion, Okay,,,,

Registolistagem2

Id, Idlista, Contributor, Data
1, 1, Mário Oliveira, 2018-12-06 06:29:00
2, 2, Isabel Barge, 2018-12-06 06:58:00

Example of a line with the result:

Car, Product, Missing, Date on Product, In Stock, Remark, Contributor, Date, Time, Contributor Previous
G3 Wing A, 1 Soap Liquid, Ok,,, Isabel Barge, 2018-12-06,06:58:00,Mário Oliveira

The previous collaborator has to be always according to the name of the car, if I’m returning the collaborator who registered the car G3 Ala A then it has to be the collaborator who registered before this carroe so successively

  • Can provide an example of the data in each table and an example that would be generated with this data?

  • @Sorack, I added to the question the information, I hope that the way I put the information

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.