Use Optimized Functions

Osclass 8.3 brings a lot of new functions including optimized data loader functions those avoid excessive and repetitive database queries and store them into session for possible repetitive use, sort or filter.

Optimized Functions

How it works? Let's analyze it:

  • Problem: get list of available countries from Osclass and then US country. 
  • Function$data = Country::newInstance()->listAll(); $row = Country::newInstance()->findByCode('US');
  • In above scenario, if we repeat using these functions 50 times, we get 100 database queries/calls all together. That's quite a lot!

This was pretty fast, but what if we need that on 50 different files? And what if we need just one country? Or we have 20 queries getting country-level data - country by country? ... this leads to many repetitive database calls. Some can be optimized by using database cache like memcached, but some of them not.

What can we do about it? Use optimized way to access data:

  • Solution: Store data into session and for specific row-level queries reuse these data.
  • Function$data = osc_get_countries(); $row = osc_get_country_row('US');
  • In this case, if we use just 1 database call. Why one? because at first, all country data are loaded from database and stored into session. When we try to get US data next, we first check if this value was already received from DB and stored into session. If it's there - use it!

Support and limitations

We have such functions for most of data - country, region, city, category, user, ... In many cases, it's not possible to load everything into session as it's too much data and it would be slow.

For categories, there is also available constant OPTIMIZE_CATEGORIES (true/false) and OPTIMIZE_CATEGORIES_LIMIT (int) to enable/disable preloads into session. Default value is between 1000-2000 categories. If you use more, it's not going to preload everything.

Anyway, for objects like user, item... these are still going to be stored under their IDs, as when we received data for one item or user from DB, why not to store it for possible reuse? Especially for premium items or logged user record it's going to save a lot of queries.

Summary

Use optimized functions anywhere it's possible to reduce database queries, review helper files for list of supported functions in your current Osclass installation.