Skip to content

Loading speed very slow with large DB - Part 1 - Items #2409

Closed

Description

@dtomlins

Background information

IMPORTANT: If you choose to ignore this issue report template, your issue will be closed as we cannot help without the requested information.

Please make sure you tick (add an x between the square brackets with no spaces) the following check boxes:

  • Reporting an issue of an unmodified OSPOS installation
  • Checked open and closed issues and no similar issue was already reported (please make sure you searched!)
  • Read README, WHATS_NEW, INSTALL.md and UPGRADE
  • Read the FAQ for any known install and/or upgrade gotchas (in specific PHP extensions installed)
  • Read the wiki
  • Executed any database upgrade scripts if an upgrade pre 3.0.0 (e.g. database/2.4_to_3.0.sql)
  • Aware the installation code is in bintray (see README), and GitHub master is for developers only and therefore not complete nor stable

Installation information

  • OSPOS version is: 3.2.3
  • OSPOS git commit hash is: 4f5ad57
  • PHP version is: 7.3.1
  • MySQL or MariaDB version is: 10.1.37-MariaDB
  • OS and version is: Ubuntu 18.10
  • WebServer is: Apache 2.4
  • Selected language is: en_US
  • (If applicable) Docker installation:
  • (If applicable) Installation package for the LAMP/LEMP stack is: xampp

Issue / Bug / Question / New Feature

Severe performance issue with large number of items (60,000) and inventory history 1m+
Main Menu - Items takes upwards of 2 minutes to "load". Page load timing was 117 seconds for some instances, but never below 90 seconds.
The ladies (operators) don't have that kind of patience, and neither do I. I've been running an OLD version and have improved some slower areas, now upgrading this has to be fixed!
Someone would have to search for my body if I installed right now to production 😁

A few days has made some huge improvements, but I don't want to break others' code either.

A couple of BUGS/design deficiencies have been found also that are being corrected while in this section. I'll note them here also.

This version of changes is "draft - for review only", and is not for release/integration.

As I was going through this a number of questions / comments are in my notes. I'll summarize here and multiple "bug" reports, but full is available should someone want to see it.

I'm picky about some areas. Speed is one huge one - a 1/10 of a second improvement is huge on response times. With "staff costs" at a $22.50/hour ($15 pay plus overhead) that is $0.375 per minute! Do you want to pay them for waiting "several seconds" every time they interact with the computer? Especially when they will do some of those queries a hundred times a day!

First on the "hit-list" is Items - list query speed.
From debug logs - initial query times for this list were:
Execution Time:50.268499851227 PLUS Execution Time:31.159582138062
Rendering time is ignored.
Why?
First - the actual listing "query" (models/Item.php search) is done twice. Once to do the query and return data, the second time to find how many records could have been returned. There MUST be a way to eliminate the second one at least! (There is on most DB software)
A search query for a particular item_number is much faster, at 0.79 + 0.33 - but that is still MUCH slower than it should be. Then remember - your operator wanted to find ONE item to find the stock level - they had to live through the first query, then do the search. That one "request" cost you as an employer at least a Dollar! Not to mention the frustration.

"explain" shows temporary tables being created (28,000 records), and filesort being done.
Where being used to match/eliminate from several tables, and joins done sub-optimally.
The inventory table is being hit on every record, data converted for comparison, then where. Ugly!

BUG FOUND!! Because of the dependency on the inventory table, an item that has no transactions can never be found! RESULTS ERROR. IF this query code is used elsewhere, a new product could never be located to be received, sold, or edited. I’m sure this must show as a bug somewhere! I’m sure this isn’t the intent either. Still can’t figure why there is a selection of inventory transactions on the list anyway!

Aggregates are a big slow-down, and costing about 10 seconds on this query. I understand why they were added (to cover-up for the possible duplication fault caused by the join), but there is a better way. Fix the joins!

Inventory table has no index on the field for the matching where clause (BETWEEN). Costly.

*** create index trans_timestamp_index on ospos_inventory (trans_date);

Cleanup part 1 - Eliminate the inventory table from the join. It is causing bad results, and is certainly an ugly join. I'll re-integrate that later once the use is found.
Execution Time:0.86312890052795 PLUS Execution Time:0.23467803001404
Lots better, but still slower.

The second query (count) is .6 of a second faster - most due to the GROUP BY which shouldn't be needed. Each item has ONE supplier and ONE quantities record that is being considered!
Quantities join matches item_id only, then duplicates eliminated using a WHERE. Hmm
PROBABLE FUTURE BUG? "Stock Location" feature breakage. Where item is created "for me" in location 1, a stock quantities record is created. Someone who only sees location 2 cannot query or see this product. IS THIS INTENTIONAL? If so, HOW do they add a product with the same SKU for the second location? Just another duplicate? OR is each "Item" only intended to every be only in one location? If so, why is this a second table instead of remaining as a "property" field and keeping the quantity in the main table?
For now - I'll do this as an OUTER join. and properly put the LOCATION into the JOIN syntax. Faster anyway!

Removing the MAX(xx) from each select helped a small bit (a couple seconds off the large time), but every second counts.

QUESTION: Is "Field Order" used anywhere in the application? Or are all table references "associative" to the name? A * SELECT shows slightly slower for some reason, but is more flexible for customizing things. I'll leave the field names. There are only 2 "tiny" fields not returned by the query. There'll be a lot more with "MY" needed additions. There are no BLOBS.

Eliminate the "redundant" GROUP BY clause. The joined tables are (now) a 1:1 relationship via KEY, and LEFT further enforces that.
Execution Time:0.64102482795715 PLUS Execution Time:0.27455306053162
Better!

MOST SQL engines have a way to return the rows returned ignoring the LIMIT clause. For mysql, mysqli, Maria, Oracle and SQL server it is SQL_CALC_FOUND_ROWS as part of the initial query, followed by a second query "SELECT FOUND_ROWS()". That can eliminate the second query time.

AFTER all these changes:
List With no sort:
Execution Time:0.35885500907898 PLUS Execution Time:0.00020813941955566

List With a sort:
Execution Time:0.6206910610199 PLUS Execution Time:0.00011992454528809

for a QUERY (with a sort):
Execution Time:0.26358985900879 PLUS Execution Time:0.0001060962677002
without sort:
Execution Time:0.25322484970093

NEXT will be to re-integrate the "Transactions within Date Range" functionality.

Dan

Activity

  1. odiea commented on Mar 21, 2019

    @odiea
    Collaborator

    Removing the MAX(xx) from each select helped a small bit (a couple seconds off the large time), but every second counts. Max had to be put in so the tables would show in php versions 7 and higher.
    Have you tried changing to mysql instead of Maria db. I had load problems with Mariadb.

  2. dtomlins commented on Mar 21, 2019

    @dtomlins
    Author

    Removing the MAX(xx) from each select helped a small bit (a couple seconds off the large time), but every second counts. Max had to be put in so the tables would show in php versions 7 and higher.
    Have you tried changing to mysql instead of Maria db. I had load problems with Mariadb.

    I'm not understanding MAX(xx) being needed. Do you mean so the fields would appear? Or was this part of the getting a consistently unique set of results?

  3. odiea commented on Mar 21, 2019

    @odiea
    Collaborator

    The tables would not even show using php 7 or higher

  4. dtomlins commented on Mar 21, 2019

    @dtomlins
    Author

    Question: As far as I can find, the models/Item.php search function is ONLY used by the "Items" top menu item. Is it referenced from elsewhere that I can't see easily?

    To that, WHY is it even querying the inventory table? None of the data appears to be used or even referenced. As well, as it is only looking at the last transaction, not even by type, it isn't to determine if it has sold or anything. There certainly could be uses, but I'm not seeing that in this usage.

  5. daN4cat commented on Mar 21, 2019

    @daN4cat
    Collaborator

    MAX is required for MySQL 5.7 support.
    @SteveIreland can explain as he did all the work

  6. odiea commented on Mar 21, 2019

    @odiea
    Collaborator

    @daN4cat thanks. I could not remember for sure why the max was added.

  7. daN4cat commented on Mar 21, 2019

    @daN4cat
    Collaborator

    @dtomlins the inventory table is queried to extract items changed within a period of time. It was a request made 1 or 2 years ago.
    I implemented it. It's quite handy.

  8. dtomlins commented on Mar 21, 2019

    @dtomlins
    Author
  9. daN4cat commented on Mar 21, 2019

    @daN4cat
    Collaborator

    @dtomlins I now read all your script because I didn't have time to do that earlier.
    It looks like you knocked down 50s from the query if I understand correctly all the log.
    Quite impressive and well done!!!

    Looking forward to seeing your contributions.

    Thanks for all the help and feedback.

  10. dtomlins commented on Mar 21, 2019

    @dtomlins
    Author
  11. SteveIreland commented on Mar 21, 2019

    @SteveIreland
    Collaborator

    To be able to render table paging the way users are accustomed to it is necessary to know the number of records that could be paged through. Then it is necessary to retrieve one page of the data. It's ugly but people are spoiled and it requires two separate SQL statements. The hope is always that the SQL engine maintains an internal cache and optimizes the engine's behavior.

    In an SQL heavy system such as OSPOS all of that information is retrieved using SQL. Not much data is "denormalized" and not a lot of tracking information exists in the database. I've noticed that at least one site made the effort to denormalize critical values so that reporting was simpler, but they didn't contribute their changes.

    Since I was tasked to "explain myself" I'll touch on the MAX function. As you know, the way we are heavily using the MAX function is to insert columns of data that should only have a single value of data. But we still want the value included in the grouped table. You might be able to get away with omitting the MAX function but technically it's not valid SQL and some variation of the database isn't going to support it. A generic software package should conform to the standard.

    I hate having to do it because it's ugly and looks amateurish, but that's the way the SQL was structured prior to me being involved. I'm sure no one noticed, but for the summary payments report I changed the way the SQL was structured so that I didn't need to use MAX for that purpose. (Hmmm ... I reviewed the code to insure I wasn't lying to you and it appears that I cloned a bit of code that had a MAX function. Hopefully it's not necessary but I'll revisit that at a later time).

    Performance can be significantly improved by denormalizing some information. This would especially be a big boost in performance in the area of sales reporting with summary data captured in the appropriate places, but the cost is that the code becomes more complex because any operational denormalized data needs to be maintained. But if you need optimization that is the most direct path forward (in my opinion).

  12. added this to the 3.4.0 milestone on Mar 22, 2019
  13. 17 remaining items

  14. dtomlins commented on Jun 9, 2019

    @dtomlins
    Author

    Interesting @daN4cat that you see it not working in 5.5, as I have it used a lot in 5.5.16. But see above note on deprecation in 8.0.17

  15. daN4cat commented on Jun 9, 2019

    @daN4cat
    Collaborator

    5.5 was a mistake on my side, I removed the comment.
    OK, since it's not supported in 8.0.x it's not worth changing the code.

    Therefore I close your PR #2416

  16. daN4cat commented on Jun 9, 2019

    @daN4cat
    Collaborator

    Maybe the next I should add is: create index trans_timestamp_index on ospos_inventory (trans_date);

  17. daN4cat commented on Jun 15, 2019

    @daN4cat
    Collaborator

    With #2461 the trans_date index is added, as other date based indexes.

  18. daN4cat commented on Jun 15, 2019

    @daN4cat
    Collaborator

    @dtomlins if you can check the latest code and suggest other improvements, or branch off and work on some that would form a great contribution.

  19. added a commit that references this issue on Jun 15, 2019
  20. objecttothis commented on Jun 15, 2020

    @objecttothis
    Member

    @dtomlins I have submitted the #2821 PR. If you can, please test it against a copy of your database. There are many database optimizations.

  21. linked a pull request that will close this issueCLCdesq API #2754on Jun 15, 2020
  22. dtomlins commented on Jun 15, 2020

    @dtomlins
    Author

    I'll run another full-set over the next short while. Thanks, Dan

  23. objecttothis commented on Apr 22, 2021

    @objecttothis
    Member

    @dtomlins I've just submitted PR #3167 which should help your problem. If you are up for testing it out on a copy of your production database, that would confirm and help find problems if there are any.

  24. linked a pull request that will close this issueDatabase Optimizations #3167on Apr 22, 2021
  25. removed a link to a pull requestCLCdesq API #2754on Apr 22, 2021
  26. dtomlins commented on Apr 22, 2021

    @dtomlins
    Author
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions