Log table design example analysis
In the final analysis, there are two aspects to the design of a relational table.
First, it is designed completely according to the paradigm Theory. generally, it is enough to reach the third paradigm, or you can divide it to a further level. For example, fourth, fifth, and sixth. This design has its own strong readability, but it adds the overhead of associating multiple relational tables when retrieving data.
Second, we should do some anti-paradigm in the paradigm Theory. some things should not be too stripped away. (Narrow tables and wide tables) this is consistent with the tightly coupled loose coupling theory in software design.
In the following example, we will use a common LOG table. There are two types of tables: narrow table and slightly wider table.
Narrow table: 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)
Number of table records
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)
Next, we need to retrieve the average time of all IDS. There are two methods:
First, the table is accessed twice and has a group by operation, which is not available.
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)
Second, although the table has the least access, there is also a group by operation. No way. The table design is like this.
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)
Wide Table: 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)
Number of table records:
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)
If you want to query a slightly redundant table, the table access and CPU resource usage are the lowest.
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)