项目中想记录一下文章的访问时长,准备采用心跳的方法来记录。
在设计数据库表时,有两种方案:
- 每次心跳在数据库插入一条记录;
- 每次心跳根据id更新数据。
方案1特点,每次心跳都会有一条数据记录,比较详细,但时查询时可能存在冗余数据
方案2特点,一篇文章只有一条记录,查询时比较简单,但是担心更新数据库会比较慢
1准备数据
准备在数据库测试一下,千万级别数据时的插入和更新性能
-- 建表及序列
create table t_test
(
id number(10) not null,
content VARCHAR2(30),
create_time date,
audit_time date
);
alter table t_test add constraint PK_test primary key (id);
create sequence s_test__id;
存储过程,插入1 千万数据
create or replace procedure pro_test_insert_update is
i int := 0;
begin
dbms_output.put_line('start!');
while i < 10000000 loop
insert into t_test
values
(s_test_insert__id.nextval, i, sysdate, sysdate);
if mod(i, 1000) = 0 then
commit;
end i