這不是一個解決方案,但是一個數據的建模工作周圍
- 分手的URL轉換成{協議,主機名,路徑名}組件。
- 現在,您可以使用完全匹配來加入主機名部分,避免了正則表達式匹配中的前導%。
- 該視圖旨在證明full_url可以根據需要進行重構。
更新可能需要幾分鐘的時間。
SET search_path='tmp';
DROP TABLE urls CASCADE;
CREATE TABLE urls
(id SERIAL NOT NULL PRIMARY KEY
, full_url varchar
, proto varchar
, hostname varchar
, pathname varchar
);
INSERT INTO urls(full_url) VALUES
('ftp://www.myhost.com/secret.tgz')
,('http://www.myhost.com/robots.txt')
,('http://www.myhost.com/index.php')
,('https://www.myhost.com/index.php')
,('http://www.myhost.com/subdir/index.php')
,('https://www.myhost.com/subdir/index.php')
,('http://www.hishost.com/index.php')
,('https://www.hishost.com/index.php')
,('http://www.herhost.com/index.php')
,('https://www.herhost.com/index.php')
;
UPDATE urls
SET proto = split_part(full_url, '://' , 1)
, hostname = split_part(full_url, '://' , 2)
;
UPDATE urls
SET pathname = substr(hostname, 1+strpos(hostname, '/'))
, hostname = split_part(hostname, '/' , 1)
;
-- the full_url field is now redundant: we can drop it
ALTER TABLE urls
DROP column full_url
;
-- and we could always reconstruct the full_url from its components.
CREATE VIEW vurls AS (
SELECT id
, proto || '://' || hostname || '/' || pathname AS full_url
, proto
, hostname
, pathname
FROM urls
);
SELECT * FROM urls;
;
SELECT * FROM vurls;
;
OUTPUT:
INSERT 0 10
UPDATE 10
UPDATE 10
ALTER TABLE
CREATE VIEW
id | proto | hostname | pathname
----+-------+-----------------+------------------
1 | ftp | www.myhost.com | secret.tgz
2 | http | www.myhost.com | robots.txt
3 | http | www.myhost.com | index.php
4 | https | www.myhost.com | index.php
5 | http | www.myhost.com | subdir/index.php
6 | https | www.myhost.com | subdir/index.php
7 | http | www.hishost.com | index.php
8 | https | www.hishost.com | index.php
9 | http | www.herhost.com | index.php
10 | https | www.herhost.com | index.php
(10 rows)
id | full_url | proto | hostname | pathname
----+-----------------------------------------+-------+-----------------+------------------
1 | ftp://www.myhost.com/secret.tgz | ftp | www.myhost.com | secret.tgz
2 | http://www.myhost.com/robots.txt | http | www.myhost.com | robots.txt
3 | http://www.myhost.com/index.php | http | www.myhost.com | index.php
4 | https://www.myhost.com/index.php | https | www.myhost.com | index.php
5 | http://www.myhost.com/subdir/index.php | http | www.myhost.com | subdir/index.php
6 | https://www.myhost.com/subdir/index.php | https | www.myhost.com | subdir/index.php
7 | http://www.hishost.com/index.php | http | www.hishost.com | index.php
8 | https://www.hishost.com/index.php | https | www.hishost.com | index.php
9 | http://www.herhost.com/index.php | http | www.herhost.com | index.php
10 | https://www.herhost.com/index.php | https | www.herhost.com | index.php
(10 rows)
嘗試使用MERGE代替。它可以更快地鏈接表格。 – Samson 2012-07-08 09:15:33
你真的需要〜操作符嗎? stxt1,stxt2字段中有什麼,以及它們的類型是什麼? – wildplasser 2012-07-08 10:15:50
@wildplasser〜運算符幾乎等同於'stxt2',如'%'|| stxt1 ||'%''。兩個字段stxt都是字符變化的。 @radashk我試過這個[link](http://petereisentraut.blogspot.com/2010/05/merge-syntax.html),但是Postgres總是告訴我錯誤:語法錯誤處於或接近「MERGE」。我該如何嘗試「合併」? – datatanger 2012-07-08 10:27:36