search
HomeDatabaseMysql Tutorial 【原创】日志表设计一例分析

关于关系表的设计归根结底有两个方面。第一,就是完全按照范式理论去设计,一般来说达到第三范式就可以了,或者你可以划分的更细到达更上一层次。比如第四,第五

关于关系表的设计归根结底有两个方面。

第一,就是完全按照范式理论去设计,一般来说达到第三范式就可以了,或者你可以划分的更细到达更上一层次。比如第四,第五,第六等等。这种设计有自己的可读性很强,,但是有一点,在检索数据的时候增加了多张关系表来做关联的开销。

第二,就是在范式理论上适当的做些反范式,有的东西还是不要太剥离的好。(窄表以及宽表) 这点和软件设计中的紧耦合松耦合理论一致。


下面我就以常用的LOG表来做下演示,其中有两种表的实际,一种是窄表,一种是稍微宽一点的表。

窄表: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的平均时间。 有以下两种方式:

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What Are the Limitations of Using Views in MySQL?What Are the Limitations of Using Views in MySQL?May 14, 2025 am 12:10 AM

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

Securing Your MySQL Database: Adding Users and Granting PrivilegesSecuring Your MySQL Database: Adding Users and Granting PrivilegesMay 14, 2025 am 12:09 AM

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

What Factors Influence the Number of Triggers I Can Use in MySQL?What Factors Influence the Number of Triggers I Can Use in MySQL?May 14, 2025 am 12:08 AM

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

MySQL: Is it safe to store BLOB?MySQL: Is it safe to store BLOB?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

MySQL: Adding a user through a PHP web interfaceMySQL: Adding a user through a PHP web interfaceMay 14, 2025 am 12:04 AM

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor