最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501
当前位置: 首页 - 科技 - 知识百科 - 正文

日志表设计一例分析

来源:懂视网 责编:小采 时间:2020-11-09 15:06:23
文档

日志表设计一例分析

日志表设计一例分析:关于关系表的设计归根结底有两个方面。 窄表:log_ytt mysql show create table log_ytt; +-------------+-------------------------------------------------------------------------------------------
推荐度:
导读日志表设计一例分析:关于关系表的设计归根结底有两个方面。 窄表:log_ytt mysql show create table log_ytt; +-------------+-------------------------------------------------------------------------------------------

关于关系表的设计归根结底有两个方面。 窄表:log_ytt mysql show create table log_ytt; +-------------+--------------------------------------------------------------------------------------------------------------------------------------------

关于关系表的设计归根结底有两个方面。
窄表:log_ytt
mysql> show create table log_ytt; +-------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | log_ytt | CREATE TABLE `log_ytt` ( `ids` bigint(20) DEFAULT NULL, `log_time` datetime DEFAULT NULL, KEY `idx_u1` (`ids`,`log_time`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 | +-------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)

mysql> select * from log_ytt where ids > '4875000001'; +------------+---------------------+ | ids | log_time | +------------+---------------------+ | 7110000001 | 2014-05-20 21:56:42 | | 6300000001 | 2014-05-20 21:56:42 | | 6750000001 | 2014-05-20 21:56:42 | | 5310000001 | 2014-05-20 21:56:42 | | 7200000001 | 2014-05-20 21:56:42 | | 7380000001 | 2014-05-20 21:56:42 | | 5760000001 | 2014-05-20 21:56:42 | | 6930000001 | 2014-05-20 21:56:42 | | 6660000001 | 2014-05-20 21:56:42 | | 5670000001 | 2014-05-20 21:56:42 | | 6210000001 | 2014-05-20 21:56:42 | | 5850000001 | 2014-05-20 21:56:42 | | 6570000001 | 2014-05-20 21:56:42 | | 5580000001 | 2014-05-20 21:56:42 | | 5130000001 | 2014-05-20 21:56:42 | | 7290000001 | 2014-05-20 21:56:42 | | 6390000001 | 2014-05-20 21:56:42 | | 5490000001 | 2014-05-20 21:56:42 | | 5220000001 | 2014-05-20 21:56:42 | | 7560000001 | 2014-05-20 21:56:42 | | 7470000001 | 2014-05-20 21:56:42 | | 7020000001 | 2014-05-20 21:56:42 | | 6840000001 | 2014-05-20 21:56:42 | | 6030000001 | 2014-05-20 21:56:42 | | 6480000001 | 2014-05-20 21:56:42 | | 7650000001 | 2014-05-20 21:56:42 | | 5940000001 | 2014-05-20 21:56:42 | | 6120000001 | 2014-05-20 21:56:42 | | 7740000001 | 2014-05-20 21:56:42 | | 5400000001 | 2014-05-20 21:56:42 | | 5760000001 | 2014-05-21 03:19:07 | | 6840000001 | 2014-05-21 03:19:17 | | 7020000001 | 2014-05-21 03:19:32 | | 7200000001 | 2014-05-21 03:19:45 | | 7110000001 | 2014-05-21 03:19:46 | | 7380000001 | 2014-05-21 03:19:48 | | 5670000001 | 2014-05-21 03:19:58 | | 6930000001 | 2014-05-21 03:19:59 | | 6030000001 | 2014-05-21 03:20:00 | | 5940000001 | 2014-05-21 03:20:00 | | 7290000001 | 2014-05-21 03:20:02 | | 6120000001 | 2014-05-21 03:20:09 | | 5850000001 | 2014-05-21 03:20:18 | | 5580000001 | 2014-05-21 03:20:24 | | 6480000001 | 2014-05-21 03:25:05 | | 6390000001 | 2014-05-21 03:25:37 | | 6210000001 | 2014-05-21 03:25:45 | | 7470000001 | 2014-05-21 03:26:14 | | 6750000001 | 2014-05-21 03:27:17 | | 5310000001 | 2014-05-21 03:27:33 | | 5130000001 | 2014-05-21 03:27:34 | | 6570000001 | 2014-05-21 03:27:34 | | 7560000001 | 2014-05-21 03:27:45 | | 5220000001 | 2014-05-21 03:27:45 | | 5400000001 | 2014-05-21 03:27:53 | | 5490000001 | 2014-05-21 03:27:55 | | 6660000001 | 2014-05-21 03:28:07 | | 6300000001 | 2014-05-21 03:28:13 | | 7740000001 | 2014-05-21 03:28:26 | | 7650000001 | 2014-05-21 03:28:37 | +------------+---------------------+ 60 rows in set (0.00 sec)
接下来,我们要检索所有IDS的平均时间。 有以下两种方式:

mysql> select sec_to_time(avg(timestampdiff(second,a.times,b.times))) as 'running' -> from -> (select ids,min(log_time) as times from log_ytt where 1 group by ids ) as a, -> (select ids,max(log_time) as times from log_ytt where 1 group by ids) as b where a.ids = b.ids; +---------------+ | running | +---------------+ | 05:27:08.8333 | +---------------+ 1 row in set (0.00 sec)
第二,虽然对表进行了最少的访问,但是也有一次GROUP BY 操作。也没办法,表设计如此。
mysql> SELECT SEC_TO_TIME(AVG(times)) AS 'Running' FROM -> ( -> SELECT TIMESTAMPDIFF(SECOND,MIN(log_time),MAX(log_time)) AS times FROM log_ytt GROUP BY ids -> ) AS T; +---------------+ | Running | +---------------+ | 05:27:08.8333 | +---------------+ 1 row in set (0.00 sec)
宽表:log_ytt_horizontal.
mysql> show create table log_ytt_horizontal; +------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | log_ytt_horizontal | CREATE TABLE `log_ytt_horizontal` ( `ids` bigint(20) NOT NULL, `start_time` datetime DEFAULT NULL, `end_time` datetime DEFAULT NULL, PRIMARY KEY (`ids`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 | +------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
表记录数:
mysql> select * from log_ytt_horizontal; +------------+---------------------+---------------------+ | ids | start_time | end_time | +------------+---------------------+---------------------+ | 5130000001 | 2014-05-20 21:56:42 | 2014-05-21 03:27:34 | | 5220000001 | 2014-05-20 21:56:42 | 2014-05-21 03:27:45 | | 5310000001 | 2014-05-20 21:56:42 | 2014-05-21 03:27:33 | | 5400000001 | 2014-05-20 21:56:42 | 2014-05-21 03:27:53 | | 5490000001 | 2014-05-20 21:56:42 | 2014-05-21 03:27:55 | | 5580000001 | 2014-05-20 21:56:42 | 2014-05-21 03:20:24 | | 5670000001 | 2014-05-20 21:56:42 | 2014-05-21 03:19:58 | | 5760000001 | 2014-05-20 21:56:42 | 2014-05-21 03:19:07 | | 5850000001 | 2014-05-20 21:56:42 | 2014-05-21 03:20:18 | | 5940000001 | 2014-05-20 21:56:42 | 2014-05-21 03:20:00 | | 6030000001 | 2014-05-20 21:56:42 | 2014-05-21 03:20:00 | | 6120000001 | 2014-05-20 21:56:42 | 2014-05-21 03:20:09 | | 6210000001 | 2014-05-20 21:56:42 | 2014-05-21 03:25:45 | | 6300000001 | 2014-05-20 21:56:42 | 2014-05-21 03:28:13 | | 6390000001 | 2014-05-20 21:56:42 | 2014-05-21 03:25:37 | | 6480000001 | 2014-05-20 21:56:42 | 2014-05-21 03:25:05 | | 6570000001 | 2014-05-20 21:56:42 | 2014-05-21 03:27:34 | | 6660000001 | 2014-05-20 21:56:42 | 2014-05-21 03:28:07 | | 6750000001 | 2014-05-20 21:56:42 | 2014-05-21 03:27:17 | | 6840000001 | 2014-05-20 21:56:42 | 2014-05-21 03:19:17 | | 6930000001 | 2014-05-20 21:56:42 | 2014-05-21 03:19:59 | | 7020000001 | 2014-05-20 21:56:42 | 2014-05-21 03:19:32 | | 7110000001 | 2014-05-20 21:56:42 | 2014-05-21 03:19:46 | | 7200000001 | 2014-05-20 21:56:42 | 2014-05-21 03:19:45 | | 7290000001 | 2014-05-20 21:56:42 | 2014-05-21 03:20:02 | | 7380000001 | 2014-05-20 21:56:42 | 2014-05-21 03:19:48 | | 7470000001 | 2014-05-20 21:56:42 | 2014-05-21 03:26:14 | | 7560000001 | 2014-05-20 21:56:42 | 2014-05-21 03:27:45 | | 7650000001 | 2014-05-20 21:56:42 | 2014-05-21 03:28:37 | | 7740000001 | 2014-05-20 21:56:42 | 2014-05-21 03:28:26 | +------------+---------------------+---------------------+ 30 rows in set (0.00 sec)
如果对这种稍微冗余一些的表来进行查询,那么对表的访问以及CPU的资源占用都达到了最低。
mysql> select sec_to_time(avg(timestampdiff(second,start_time,end_time))) as 'Running' from log_ytt_horizontal; +---------------+ | Running | +---------------+ | 05:27:08.8333 | +---------------+ 1 row in set (0.00 sec)

声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文档

日志表设计一例分析

日志表设计一例分析:关于关系表的设计归根结底有两个方面。 窄表:log_ytt mysql show create table log_ytt; +-------------+-------------------------------------------------------------------------------------------
推荐度:
标签: 两个 设计 日志
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top