AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 3653
Accepted
Spredzy
Spredzy
Asked: 2011-07-06 02:16:21 +0800 CST2011-07-06 02:16:21 +0800 CST 2011-07-06 02:16:21 +0800 CST

当应用程序依赖数据库时如何批量加载数据库?

  • 772

在日常工作中,我需要删除并批量加载数据库。

问题是我有一个依赖该数据库的 WebApp。所以我不能就这样删除数据库。

解决这个问题的好方法是什么?

database-design
  • 6 6 个回答
  • 168 Views

6 个回答

  • Voted
  1. BillThor
    2011-07-06T05:28:29+08:002011-07-06T05:28:29+08:00

    您需要删除并重新加载数据库吗?在像您这样的情况下,我会寻找一种方法将数据库与参考集(您正在加载的数据)同步。

    另一种方法是拥有两个数据库。每天加载一个不同的。配置 webapp,以便您可以即时交换数据库。

    • 6
  2. Best Answer
    Gaius
    2011-07-07T10:45:49+08:002011-07-07T10:45:49+08:00

    好吧,这在大多数数据库中都很简单。当您的应用程序正在运行时,从您的加载作业中:

    1. 开始一个事务(这可能是隐式的或者可能需要一个命令,你不指定你正在使用哪个数据库)
    2. DELETE表中的所有数据(不删除或截断)
    3. INSERT新数据
    4. COMMIT交易

    当步骤 1-3 运行时,应用程序将看到“旧”版本的数据并可以正常继续。即时第 4 步完成后,应用程序将在运行下一个查询时立即看到新数据。

    • 5
  3. mrdenny
    2011-07-06T18:11:05+08:002011-07-06T18:11:05+08:00

    截断表,然后批量加载数据。如果需要,在截断表之前从表中锁定 Web 应用程序。

    • 3
  4. ik_zelf
    2011-07-06T02:32:16+08:002011-07-06T02:32:16+08:00

    这取决于依赖树的情况。如果在加载数据的表 [s] 上有很多依赖对象,最好不要更改对象和表,因为会发生很多失效。

    truncate table 可以非常快速地删除数据,但在存在引用约束时将不起作用。由于锁定问题,它在应用程序运行时也不会工作。

    最适合应用程序的是正常的删除/插入序列。不是最快的选择,但它是可用性最好的,并且锁定问题最少。

    其他选项可以分区交换加载。约束可能会产生问题,并且需要花费大量时间来验证。永远不要为了删除数据而删除表。表被视为应用程序基础设施并且应该始终可用,因为应用程序会向您的用户抛出很多错误。

    • 2
  5. alex
    2011-07-06T04:32:50+08:002011-07-06T04:32:50+08:00
    1. 停止网络应用程序。
    2. 删除/恢复数据库。
    3. 再次启动网络应用程序。
    • 2
  6. maletin
    2012-04-17T05:54:41+08:002012-04-17T05:54:41+08:00

    我将新数据加载到临时表中。这样,我只需要锁定需要更新的行。之后我删除丢失的行并插入新行。

    create table permanent ( key int primary key, field1 int, field2 text );
    create temporary table temporary as select * from permanent where false;
    \copy temporary from 'actual_data.dump'
    alter table temporary add constraint temporary_pkey primary key ( key );
    
    update permanent                              
       set field1 = temporary.field1,
           field2 = temporary.field2
      from temporary
     where permanent.key = temporary.key
       and (     permanent.field1 is distinct from temporary.field1
             or  permanent.field2 is distinct from temporary.field2
           );
    
    delete from permanent where not exists ( select 1
                                               from temporary
                                              where key = permanent.key );
    insert into permanent ( select *
                              from temporary
                             where not exists ( select 1
                                                  from permanent
                                                 where key = temporary.key ) );

    这至少适用于 postgresql。

    对于甲骨文,我使用以下内容:

    update ( select p.field1 p_field1,
                    p.field2 p_field2,
                    t.field1 t_field1,
                    t.field2 t_field2
               from permanent p, temporary t
              where p.key = t.key )
       set p_field1 = t_field1,
           p_field2 = t_field2
     where p_field1 <> t_field1
        or p_field1 is null and t_field1 is not null
        or p_field1 is not null and t_field1 is null
        or p_field2 <> t_field2
        or p_field2 is null and t_field2 is not null
        or p_field2 is not null and t_field2 is null
    当我有不可能的值时,我可以改用 NVL:

     where nvl(p_field1, -1) <> nvl(t_field1, -1)
        or nvl(p_field2, '{null}') <> nvl(t_field2, '{null}')
    • 1

相关问题

  • 过滤索引是否有助于改进基于输入时间的查询,还是应该避免这种情况?

  • MySQL VARCHAR 和 TEXT 数据类型有什么区别?

  • 存储计算值或根据要求重新计算它们更好吗?[复制]

  • 存储与计算聚合值

  • 在数据仓库中实现多对多关系有哪些方法?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    您如何显示在 Oracle 数据库上执行的 SQL?

    • 2 个回答
  • Marko Smith

    如何选择每组的第一行?

    • 6 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    我可以查看在 SQL Server 数据库上运行的历史查询吗?

    • 6 个回答
  • Marko Smith

    如何在 PostgreSQL 中使用 currval() 来获取最后插入的 id?

    • 10 个回答
  • Marko Smith

    如何在 Mac OS X 上运行 psql?

    • 11 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Marko Smith

    将数组参数传递给存储过程

    • 12 个回答
  • Martin Hope
    Manuel Leduc PostgreSQL 多列唯一约束和 NULL 值 2011-12-28 01:10:21 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Stuart Blackler 什么时候应该将主键声明为非聚集的? 2011-11-11 13:31:59 +0800 CST
  • Martin Hope
    pedrosanta 使用 psql 列出数据库权限 2011-08-04 11:01:21 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST
  • Martin Hope
    BrunoLM Guid vs INT - 哪个更好作为主键? 2011-01-05 23:46:34 +0800 CST
  • Martin Hope
    bernd_k 什么时候应该使用唯一约束而不是唯一索引? 2011-01-05 02:32:27 +0800 CST
  • Martin Hope
    Patrick 如何优化大型数据库的 mysqldump? 2011-01-04 13:13:48 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve