Image/thumbnail is very slow

Silverstripe Version: 5.2

Question:

Hi,

I built a website with SilverStripe. Everything works well, just I found the image/thumbnail parts are very slow.

Example 1:

class ProductProfile extends DataObject
{
    private static $has_one = [
        'Thumbnail' => Image::class
    ];
}

The template

  <% if $ProductProfiles %>
      <% loop $ProductProfiles %>
                  <div class="product-profile-image">
                      <a href="$Thumbnail.URL" data-lightbox="$Thumbnail.Name">
                          $Thumbnail.ScaleHeight(150)
                      </a>
                  </div>
      <% end_loop %>
  <% end_if %>
</div

This is very slow due to $Thumbnail.ScaleHeight(150). I can see an obvious speedup once I remove this part.

Example Two:
I want to tell if one object has Thumbnail, if not use its parent’s thumbnail


public function ProfileThumbnail()
{
    $productProfile = $this->ProductProfiles()->filter('CountryID', Security::getCurrentUser()->PrimaryCountryID)->first();
    $profileThumbnail = $productProfile ? $productProfile->Thumbnail() : $this->Thumbnail();
    return $profileThumbnail;
}

but this step

$profileThumbnail = $productProfile ? $productProfile->Thumbnail() : $this->Thumbnail();

is really slow.

Any ideas how can I optimize my code?

It turned out that it was because the table “File”, “File_Versions” and “File_Live” are too huge.
I will write a scheduled task to clean the unused record.