Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveStorage checking if file exists is slow

When creating a variant, I am seeing slow times just to check if the file exists. The strange thing is that I do not see the slow times when the file gets uploaded. How can we speed this up?

It happens when you call something like this.

@object.artwork.variant(resize: "100X100")

Notice the slow time (over 1 second!) just to check if the file exists.

S3 Storage (1149.3ms) Checked if file exists at key: variants/K6XGSR9uuyEG17Vd6HXSLyew/4c1922cb9e0ee90688549c6a10906cc59c45f90ab173febdd7ae9dfc72c0378b

You can recreate in the console with

ActiveStorage::Blob.service.exist?("variants/K6XGSR9uuyEG17Vd6HXSLyew/4c1922cb9e0ee90688549c6a10906cc59c45f90ab173febdd7ae9dfc72c0378b")

The above is an actual example from production, but normally we see average times in the 300ms range.

like image 782
John Pollard Avatar asked Mar 21 '18 20:03

John Pollard


1 Answers

I think this is just the way it works. I'd suggest storing your variants as separate attachments instead.

There's an issue on the Rails GitHub about it:

https://github.com/rails/rails/issues/32548

February 2020 update: storing variant records in the database is coming in Rails 6.1

like image 156
Tim Down Avatar answered Oct 21 '22 11:10

Tim Down