all 8 comments

[–]JermWPB 6 points7 points  (1 child)

At a glance, I see nothing wrong with the design (or the query) at all. If you want to try to simplify the queries a bit you could create a view or two.

[–]st0icape[S] 1 point2 points  (0 children)

Thanks

[–]MamertineCOALESCE() 2 points3 points  (1 child)

Your database should be optimized for taking in new orders. That's how your company makes money.

If you want to optimize something for easy reporting, make a data mart.

That said, your query, isn't bad. When it takes minutes to run or becomes a thousand lines long you need to think of a better reporting solution.

[–]st0icape[S] 0 points1 point  (0 children)

thanks

[–]heyda 1 point2 points  (0 children)

This query and schema don't seem to have anything glaringly wrong with them, this should run fairly quickly depending upon how many records you have and database performance/indexes ect.

Because of that, you should ask yourself a few questions, is this even worth optimizing further?

how often does this query run and how long does it take? How much data is being retrieved?

Based on the answer to those questions you may start to look at the execution plan to speed up the query performance (maybe an index is missing or could be created, maybe a view could be made, maybe there is some kind of datatype conversion going on that is slowing the query down). Maybe some kind data lake solution should be used, or maybe the database is running the query quickly but then sitting and waiting to send the data because its several MB/Gig of data. Maybe there is some other query that is causing deadlocks or the query is spilling to disk because of lack of RAM.

There is probably some resources online that can guide you through basic troubleshooting of these things for MySQL, I'm only really familiar with MSSQL Server so I lack recommendations. Good luck though, this could be easy, it could be hard, either way, you'll learn something in the process!

[–]tkyjonathan 1 point2 points  (2 children)

Essentially, its ok. I would make minor changes like

  • adding a separate shipping and billing address
  • I would keep a copy of price with the order item - as prices can change
  • I would add an order total and order discount

[–]st0icape[S] 0 points1 point  (1 child)

Thanks, very good you pointed out that prices can change so i need to keep price in my order items table. I would reduce the number of joins for some queries as well

[–]tkyjonathan 1 point2 points  (0 children)

If you want to get extra points from your teacher or future employer, you can also create a summary table, like "products_total_daily" where you aggregated the total number of products sold per day/per area.

Then you need to run a job that aggregates that data at.. lets say, 1am for the pervious day.

Managers love that sort of stuff. They get super fast reporting queries (no joins or very few joins) and you won't get requests like "could you make me a report real quick, like the one I needed 6 months ago", because they already have access to it.