PERF: Add fast path to find uploads before resorting to `LIKE` query.

For a normal upload url

Before

```
Warming up --------------------------------------
                       264.000  i/100ms
Calculating -------------------------------------
                          2.754k (± 8.4%) i/s -     13.728k in   5.022066s
```

After

```
Warming up --------------------------------------
                       341.000  i/100ms
Calculating -------------------------------------
                          3.435k (±11.6%) i/s -     17.050k in   5.045676s
```
This commit is contained in:
Guo Xiang Tan 2018-09-06 14:29:45 +08:00
parent d4b05d7bc5
commit 1f636c445b
2 changed files with 9 additions and 8 deletions

View File

@ -175,12 +175,12 @@ class Upload < ActiveRecord::Base
end
return if uri&.path.blank?
path = uri.path[/(\/original\/\dX\/[\/\.\w]+)/, 1]
return if path.blank?
Upload.find_by("url LIKE ?", "%#{path}")
data = uri.path.match(/(\/original\/\dX\/[\/\.\w]+\/([a-zA-Z0-9]+)[\.\w]+)/)
return if data.blank?
sha1 = data[2]
upload = nil
upload = Upload.find_by(sha1: sha1) if sha1
upload || Upload.find_by("url LIKE ?", "%#{data[1]}")
end
def self.migrate_to_new_scheme(limit = nil)

View File

@ -74,8 +74,9 @@ describe Upload do
end
context ".get_from_url" do
let(:url) { "/uploads/default/original/3X/1/0/10f73034616a796dfd70177dc54b6def44c4ba6f.png" }
let(:upload) { Fabricate(:upload, url: url) }
let(:sha1) { "10f73034616a796dfd70177dc54b6def44c4ba6f" }
let(:url) { "/uploads/default/original/3X/1/0/#{sha1}.png" }
let(:upload) { Fabricate(:upload, url: url, sha1: sha1) }
it "works when the file has been uploaded" do
expect(Upload.get_from_url(upload.url)).to eq(upload)