Skip to content

Add a url index for faster SQL lookups with large tables#4013

Merged
dgw merged 7 commits intoYOURLS:masterfrom
matt-h:fix/3793-index-url-for-lookup
Dec 22, 2025
Merged

Add a url index for faster SQL lookups with large tables#4013
dgw merged 7 commits intoYOURLS:masterfrom
matt-h:fix/3793-index-url-for-lookup

Conversation

@matt-h
Copy link
Copy Markdown
Contributor

@matt-h matt-h commented Nov 2, 2025

This is a replacement for the hash index I added in #4006

I found that just adding a basic index to the urls column limited to 30 chars has the same performance boost as the url hash. This handles it all in mysql without adding any new complexity to the codebase or queries.

To note, when doing a lookup by the index it will match on those first 30 characters and to filter it down to matching ones and then do a WHERE on just those results which is significantly faster than doing a WHERE on the entire table. The one drawback vs the hash would be the case where every url in the database is the same domain name that is longer than 30 characters (not a likely case in my opinion) it would just work the same as it does now.

For data size difference, my test DB with over 4 million urls was 1.4 GB with the current version of YOURLS, 1.6 GB with the hash version and also 1.6 GB with this new index version.

Also, the upgrade time was about 1/5 the time of adding the hash in my testing.

Fixes #3793

@ozh
Copy link
Copy Markdown
Member

ozh commented Nov 2, 2025

Smart, thanks, indeed much better

@ozh ozh self-assigned this Nov 23, 2025
@dgw dgw mentioned this pull request Dec 17, 2025
4 tasks
@dgw dgw self-assigned this Dec 17, 2025
Copy link
Copy Markdown
Member

@ozh ozh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Copy Markdown
Member

@dgw dgw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@dgw dgw enabled auto-merge (squash) December 22, 2025 17:39
@dgw dgw merged commit 60a9110 into YOURLS:master Dec 22, 2025
6 checks passed
Copy link
Copy Markdown
Member

@ozh ozh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Damn, not sure how I missed this :)

@dgw
Copy link
Copy Markdown
Member

dgw commented Dec 22, 2025

It happens :)
That's why we have multiple people look at these things!

@joesenova
Copy link
Copy Markdown

joesenova commented Jan 12, 2026

Glad this is sorted 4 years after I recommended the fix:

#2585

@ozh
Copy link
Copy Markdown
Member

ozh commented Jan 12, 2026

For the record this is NOT the fix your recommended @joesenova . To be accurate, this is more the suggestion I made to you 4 years ago, but to which you didn't reply.

@joesenova
Copy link
Copy Markdown

Each to their own @ozh .

Glad to see that this issue is fixed, thanks for that.

@2ndK16
Copy link
Copy Markdown

2ndK16 commented Feb 17, 2026

I'm not sure how urgent this is, but there are different index lengths in function-install.php (30) and function-upgrade.php` (50).

@ozh
Copy link
Copy Markdown
Member

ozh commented Feb 17, 2026

Oops, nice catch @2ndK16
I propose we update the functions-install.php to 50 and leave the functions-upgrade.php as is, to avoid a database upgrade for everyone

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

YOURLS_UNIQUE_URLS slows down large instances

5 participants