Hi gentlemens,
I suppose you will kill me for question regarding denormalized DB, but only you can help me.
I have table where I store Car log book:
etc
Now, as is probably evident, I store odometer values as absolute values. I decided to do so because it is more error-resistant and insertions are very simple (add absolute value and difference is calculated automatically in query, see below).
I generate report from this table (LogBook) where I use subquery to calculate difference between adjacent records (SELECT current.Odometer - Max(previous.Odometer) FROM … WHERE previous.Car_id = current.Car_id AND prevoius.Odometer < current.Odometer). This works fine as I have this query as saved query. However when I try to perform aggregations with this saved query it is very slow and errorneous.
so I decided that maybe one solution would be to add redundant field where I would simply calculate and save difference between adjacent records as it will increase performance significantly.
My question: do you have experience with this issue so that I can save a lot of time thinking about it?
Many thanks!
I suppose you will kill me for question regarding denormalized DB, but only you can help me.
I have table where I store Car log book:
id | Date | car_id | Odometer (absolute value) |
1 | 12.10.2022 | 1 | 10 000 |
etc
Now, as is probably evident, I store odometer values as absolute values. I decided to do so because it is more error-resistant and insertions are very simple (add absolute value and difference is calculated automatically in query, see below).
I generate report from this table (LogBook) where I use subquery to calculate difference between adjacent records (SELECT current.Odometer - Max(previous.Odometer) FROM … WHERE previous.Car_id = current.Car_id AND prevoius.Odometer < current.Odometer). This works fine as I have this query as saved query. However when I try to perform aggregations with this saved query it is very slow and errorneous.
so I decided that maybe one solution would be to add redundant field where I would simply calculate and save difference between adjacent records as it will increase performance significantly.
My question: do you have experience with this issue so that I can save a lot of time thinking about it?
Many thanks!
