首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对PostgreSQL DB使用一个或多个查询

对PostgreSQL DB使用一个或多个查询
EN

Stack Overflow用户
提问于 2015-12-06 23:46:34
回答 1查看 69关注 0票数 0

我有以下数据库

代码语言:javascript
复制
CREATE TABLE IF NOT EXISTS users (
    user_uid INTEGER PRIMARY KEY, 
    user_name CHAR(64) NOT NULL,
    token_id INTEGER
);

CREATE TABLE IF NOT EXISTS unique_thing (
    id SERIAL PRIMARY KEY,
    unique_thing_id INTEGER NOT NULL, 
    option_id INTEGER NOT NULL
);

CREATE TABLE IF NOT EXISTS example (
    id SERIAL PRIMARY KEY, 
    variable INTEGER NOT NULL,
    variable_2 INTEGER NOT NULL,
    char_var CHAR(64) NOT NULL, 
    char_var2 CHAR(512),
    char_var3 CHAR(256), 
    file_path CHAR(256) NOT NULL
);



CREATE TABLE IF NOT EXISTS different_option_of_things (
    id SERIAL PRIMARY KEY,
    name CHAR(64)
);

CREATE TABLE IF NOT EXISTS commits (
    id SERIAL PRIMARY KEY, 
    user_id INTEGER NOT NULL, 
    unique_thing_id INTEGER NOT NULL, 
    value REAL NOT NULL, 
    var CHAR(512) NOT NULL, 
    example_id INTEGER NOT NULL,
    boolean_var boolean NOT NULL
);

unique_thingdifferent_option_of_thingsexamples将是静态的(数据将很少手动添加)。表commits将会相当大。它将是仅用于插入的表(我将很少删除)。用户将是具有用户标识的表。它不会像unique_thing那么大,但会有相当多的用户。

该表的数据如下:

代码语言:javascript
复制
INSERT INTO users VALUES(1, 'pacefist', 2);
INSERT INTO users VALUES(3, 'motherfucker', 4);
INSERT INTO users VALUES(4, 'cheater', 5);

INSERT INTO different_option_of_things   VALUES(1, 'blablab');
INSERT INTO different_option_of_things   VALUES(2, 'smth different');
INSERT INTO different_option_of_things   VALUES(3, 'unique_thing');
INSERT INTO different_option_of_things   VALUES(4 ,'unique_thing2');

INSERT INTO unique_thing  VALUES(DEFAULT, 1, 1);
INSERT INTO unique_thing  VALUES(DEFAULT, 1, 3);
INSERT INTO unique_thing  VALUES(DEFAULT, 2, 3);
INSERT INTO unique_thing  VALUES(DEFAULT, 2, 2);

INSERT INTO example VALUES(1, 20, 20, 'fsdfsdf', 'fgdfgdfg', 'url', '/home/user/file.txt');
INSERT INTO example VALUES(2, 24, 40, 'sfadfadf', 'dfgdfg', 'url', '/home/user/file2.txt');

INSERT INTO commits VALUES(DEFAULT, 1, 1, 55.43, '1234567', 1, TRUE);
INSERT INTO commits VALUES(DEFAULT, 2, 1, 97.85, '1234573', 2, TRUE);
INSERT INTO commits VALUES(DEFAULT, 3, 1, 0.001, '98766543', 1, TRUE);
INSERT INTO commits VALUES(DEFAULT, 4, 2, 100500.00, 'xxxxxxxx', 1, TRUE);

因此,数据将通过以下方式插入:

代码语言:javascript
复制
1) I have input data of different_option_of_things, e.g.,  [ blablab, unique_thing],  the REAL value (like 8.9999) and the number of example like `fsdfsdf`
2) It's necessary to find this record in the table `unique_thing`
a) if we've found 2 or more values or haven't found anything 
    results false -> the search is over
b) if we've found 1 result then 
    3) we are searching all values (record from unique_thing) in the 'commits' table. 
     a) if it has been found 
        a.1 search of the given example name
          a.1.1 if found -> get first 25 values and check whether the current value is bigger
               a.1.1.1 if yes, we make a commit
               a.1.1.2 if no, do nothing (do not duplicate the value)
          a.1.2 no -> no results
        a.2 if no -> no results

第二个函数几乎相同,但没有插入,我们将只做一个选择而不插入(只是为了获取数据),并将查找表'examples‘中的所有现有值(而不只是一个)。

问题是:创建3个函数比创建一个大查询更好吗?

代码语言:javascript
复制
SELECT count(1) AS counter FROM different_option_of_things 
        WHERE name IN (SELECT * FROM unnest(different_option_of_things));

SELECT * FROM example where id=fsdfsdf;

SELECT TOP 25
    FROM commits
    JOIN unique_thing
    ON commits.id=unique_thing.unique_thing_id where value > 8.9999;

if results-> 0 do a commit

还是写一个庞大的查询更好?我使用的是Postgresql、Tornado和momoko。

EN

回答 1

Stack Overflow用户

发布于 2015-12-07 15:29:44

我更喜欢使用两个存储过程来获取和插入数据。

优点:

  • 所有必需的数据都在db中,所以看起来像是db的一个作业,
  • 每次调用execute时都需要:

代码语言:javascript
复制
1. get/wait for available connection in pool (depending on your app)
2. run query
3. fetch data
4. Release connection

x。在IOLoop上的所有这些操作之间

虽然momoko是非阻塞的,但它不适用于自由

  • db可以是api,而不仅仅是sack of data

缺点:

  • 逻辑在db中意味着你依赖它-更改db引擎(例如到cassandra)将更难
  • 经常在db中的逻辑意味着没有测试。当然,您可以而且应该测试它(例如pgTap)
  • 用于简单任务它看起来像是一个过度杀伤力的

这是数据库和应用程序负载、性能和时间约束的问题-换句话说,运行测试并选择满足您的期望/要求的解决方案。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34119309

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档