-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tec: Replace Link.url_lookup by url_hash
This column is using a more performant index type (hash index) and is generated with sha256, which should generate more efficient indexes. btree index is used on the idx_links_user_id_url_hash index because hash index cannot be used with multiple columns.
- Loading branch information
1 parent
c45d8bd
commit fd1fe71
Showing
16 changed files
with
99 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace App\migrations; | ||
|
||
class Migration202406130002AddLinksUrlHash | ||
{ | ||
public function migrate(): bool | ||
{ | ||
$database = \Minz\Database::get(); | ||
|
||
$database->exec(<<<'SQL' | ||
CREATE EXTENSION IF NOT EXISTS pgcrypto; | ||
ALTER TABLE links | ||
ADD COLUMN url_hash TEXT GENERATED ALWAYS AS (encode(digest(url, 'sha256'), 'hex')) STORED; | ||
DROP INDEX idx_links_user_id_url; | ||
DROP INDEX idx_links_url; | ||
ALTER TABLE links | ||
DROP COLUMN url_lookup; | ||
CREATE INDEX idx_links_user_id_url_hash ON links USING btree(user_id, url_hash); | ||
CREATE INDEX idx_links_url_hash ON links USING hash(url_hash); | ||
DROP FUNCTION simplify_url; | ||
SQL); | ||
|
||
return true; | ||
} | ||
|
||
public function rollback(): bool | ||
{ | ||
$database = \Minz\Database::get(); | ||
|
||
$database->exec(<<<'SQL' | ||
CREATE FUNCTION simplify_url(url TEXT) | ||
RETURNS TEXT | ||
IMMUTABLE | ||
RETURNS NULL ON NULL INPUT | ||
AS $$ SELECT substring(url from 'https?://(.+)') $$ | ||
LANGUAGE SQL; | ||
ALTER TABLE links | ||
ADD COLUMN url_lookup TEXT GENERATED ALWAYS AS (simplify_url(url)) STORED; | ||
DROP INDEX idx_links_user_id_url_hash; | ||
DROP INDEX idx_links_url_hash; | ||
ALTER TABLE links | ||
DROP COLUMN url_hash; | ||
CREATE INDEX idx_links_user_id_url ON links(user_id, url_lookup); | ||
CREATE INDEX idx_links_url ON links(url_lookup); | ||
DROP EXTENSION IF EXISTS pgcrypto; | ||
SQL); | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.