简体   繁体   English

PostgreSQL和hstore以便进行两个键值存储

[英]PostgreSQL and hstore in order to make two key value stores

I would like to create two key value stores. 我想创建两个键值存储。 One that has a key of a url and a value of a url and the other has a url (this is the value of the other tables) and a score (integer). 一个具有url键和url值的键,另一个具有url(这是其他表的值)和一个分数(整数)。

I've tried to read the hstore documentation but I cant really find how to create the tables. 我试图阅读hstore文档,但是我真的找不到如何创建表。

I want to represent the urls as strings. 我想将网址表示为字符串。 I am mapping a url to its "parent url". 我正在将网址映射到其“父网址”。 The idea of a score should be more like an index the higher the index the better the website (calculated based with the idea of connectivity to other pages and time to access). 分数的想法应该更像是索引,索引越高,网站越好(根据与其他页面的连接性和访问时间的想法进行计算)。 Basically some sample data might look like this: 基本上,一些示例数据可能如下所示:

key url:    https://maps.google.com/maps?hl=en&tab=wl
parent url: https://www.google.com/

parent url: https://www.google.com/
score: 100

key url: http://www.cracked.com/blog/5-things-you-should-know-before-making-indie-movie/
parent url: http://www.cracked.com/

parent url: http://www.cracked.com/
score: 125

I don't see any reason you'd want to use hstore here. 我看不到您要在这里使用hstore任何原因。

You could model this with a simple pair of tables. 您可以使用一对简单的表对此建模。 Here's a simple translation of your data: 这是您的数据的简单翻译:

CREATE TABLE url_scores(
    parent_url text primary key,
    score integer not null
);

CREATE TABLE url_mappings(
    key_url text not null,
    parent_url text not null references url_scores(parent_url)
);

INSERT INTO url_scores(parent_url, score) VALUES ('https://www.google.com/', 100);
INSERT INTO url_scores(parent_url, score) VALUES ('http://www.cracked.com/', 125);

INSERT INTO url_mappings(key_url, parent_url)
VALUES ('https://maps.google.com/maps?hl=en&tab=wl', 'https://www.google.com/');

INSERT INTO url_mappings(key_url, parent_url)
VALUES ('http://www.cracked.com/blog/5-things-you-should-know-before-making-indie-movie/
', 'http://www.cracked.com/');

... however, you're likely to want to normalize it some more, splitting http/https and the domain out into parts, possibly using a surrogate key into url_scores instead of storing parent_url twice, etc. ...但是,您可能希望对其进行标准化,将http / https和域拆分为多个部分,可能使用替代键替换为url_scores而不是两次存储parent_url ,等等。

It's entirely possible that a relational DB isn't the right storage for your data, by the way. 顺便说一句,关系数据库很可能不是您数据的正确存储。 It's hard to say without knowing what you're doing with it. 不知道自己在做什么,很难说。

Anyway, read the PostgreSQL tutorial and some general information on database design. 无论如何,请阅读PostgreSQL教程和有关数据库设计的一些常规信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM