总结MySQL建表、查询优化实用小技巧_MySQL
bitsCN.com
总结MySQL建表、查询优化实用小技巧
MySQL建表阶段是非常重要的一个环节,表结构的好坏、优劣直接影响着后续的管理维护,赶在明天上班前分享总结个人MySQL建表、MySQL查询优化积累的一些实用小技巧。
技巧一、数据表冗余记录添加时间与更新时间
我们用到的很多数据表大多情况下都会有表记录的“添加时间(add_time)”,我建议大家再新增一个记录“更新时间(update_time)”字段,在我的工作里需要为市场部、运营部等建立各种报表,而很多报表里的数据都是需要到大记录表里去查询的,如果直接查询大表的话,查询速度相当慢。那么,我们的解决办法就是建立各种数据快照,数据快照都是通过脚本定时跑的。假如,哪一天原始数据表出现了问题,那么快照数据也会有问题,这个时候我们的快照脚本就得重新跑一份修复好了的数据,这样有了update_time更新时间这个字段,我们就能很快定位记录更新的时间。
技巧二、不要抛弃insert ignore into与replace into
大家肯定写过一些脚本循环将记录插入到数据表,例如使用PHP脚本。如果期间的一条SQL插入失败,那么后续的插入将终止,你可以在脚本里绕过这个插入错误,一个好的办法是使用ignore关键字来屏蔽插入错误。如果,你的数据表里已经存在有将要插入的相同记录,那么这个时候就会产生插入报错,使用insert ignore into将绕开这个错误,继续下一条记录的插入。
如果你的表结构设计了唯一索引的话,那么使用replace into来更新你的数据表记录再好不过了,尤其是你在写一个脚本的时候,你就不需要在脚本里先查询数据表是否存在该条记录,如果存在,则更新;如果不存在则插入。你可以直接使用replace into,它会自动到数据表检测,如果在唯一索引字段存在该值,则会先删除该条记录,然后再插入新记录;如果唯一索引字段不存在该值,则直接插入数据表,简单且非常实用。
技巧三、给你的表建立唯一索引
很多情况下建立表唯一索引,能给我们省下很多麻烦事,想上面说的replace into就必须是有唯一索引,当然,很多人喜欢用到的自增主键ID尽管也是唯一索引,我建议是能在其他经常出现在where条件屁股后面的字段,并且值是唯一的话可以加个索引甚至唯一索引,查询速度将会提高很多。
说在最后:当然,insert ignore into与replace into大家要根据自己的需求来确定,不一定就适合你现在的需求;我只是提示大家还有这么些个实用的小知识大家可以灵活运用到项目里边。
bitsCN.com
Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



When trying to open a disk image in VirtualBox, you may encounter an error indicating that the hard drive cannot be registered. This usually happens when the VM disk image file you are trying to open has the same UUID as another virtual disk image file. In this case, VirtualBox displays error code VBOX_E_OBJECT_NOT_FOUND(0x80bb0001). If you encounter this error, don’t worry, there are some solutions you can try. First, you can try using VirtualBox's command line tools to change the UUID of the disk image file, which will avoid conflicts. You can run the command `VBoxManageinternal

What happens when someone calls in airplane mode? Mobile phones have become one of the indispensable tools in people's lives. It is not only a communication tool, but also a collection of entertainment, learning, work and other functions. With the continuous upgrading and improvement of mobile phone functions, people are becoming more and more dependent on mobile phones. With the advent of airplane mode, people can use their phones more conveniently during flights. However, some people are worried about what impact other people's calls in airplane mode will have on the mobile phone or the user? This article will analyze and discuss from several aspects. first

Java is a commonly used programming language used to develop various applications. However, just like other programming languages, Java has security vulnerabilities and risks. One of the common vulnerabilities is the file inclusion vulnerability (FileInclusionVulnerability). This article will explore the principle, impact and how to prevent this vulnerability. File inclusion vulnerabilities refer to the dynamic introduction or inclusion of other files in the program, but the introduced files are not fully verified and protected, thus

On the Douyin platform, users can not only share their life moments, but also interact with other users. Sometimes the comment function may cause some unpleasant experiences, such as online violence, malicious comments, etc. So, how to turn off the comment function of TikTok? 1. How to turn off the comment function of Douyin? 1. Log in to Douyin APP and enter your personal homepage. 2. Click "I" in the lower right corner to enter the settings menu. 3. In the settings menu, find "Privacy Settings". 4. Click "Privacy Settings" to enter the privacy settings interface. 5. In the privacy settings interface, find "Comment Settings". 6. Click "Comment Settings" to enter the comment setting interface. 7. In the comment settings interface, find the "Close Comments" option. 8. Click the "Close Comments" option to confirm closing comments.

The impact of data scarcity on model training requires specific code examples. In the fields of machine learning and artificial intelligence, data is one of the core elements for training models. However, a problem we often face in reality is data scarcity. Data scarcity refers to the insufficient amount of training data or the lack of annotated data. In this case, it will have a certain impact on model training. The problem of data scarcity is mainly reflected in the following aspects: Overfitting: When the amount of training data is insufficient, the model is prone to overfitting. Overfitting refers to the model over-adapting to the training data.

Bad sectors on a hard disk refer to a physical failure of the hard disk, that is, the storage unit on the hard disk cannot read or write data normally. The impact of bad sectors on the hard drive is very significant, and it may lead to data loss, system crash, and reduced hard drive performance. This article will introduce in detail the impact of hard drive bad sectors and related solutions. First, bad sectors on the hard drive may lead to data loss. When a sector in a hard disk has bad sectors, the data on that sector cannot be read, causing the file to become corrupted or inaccessible. This situation is especially serious if important files are stored in the sector where the bad sectors are located.

Go language development tips: Alibaba Cloud interface docking practice sharing Preface: Nowadays, cloud computing has become one of the core technologies for enterprise information construction, and Alibaba Cloud, as a well-known cloud computing service provider in China, has a wealth of cloud products and service. This article will share some of the author's practical experience in using Go language to connect to Alibaba Cloud interfaces, and explain it in the form of code examples. 1. Introduction of Alibaba Cloud GoSDK Before using the Go language to connect to the Alibaba Cloud interface, we first need to introduce the corresponding Alibaba Cloud GoSDK so that

What should I do if I get an error when installing PyQt5? These little tricks will help you get twice the result with half the effort! PyQt5 is a widely used GUI (graphical user interface) toolkit for the Python programming language. It provides many powerful features and tools to help developers create interactive and engaging applications. However, when installing PyQt5, you sometimes encounter some errors. This article will introduce some common PyQt5 installation errors and solutions to help you install and use PyQt5 smoothly. Common error 1: pip command not found
