Home Database Mysql Tutorial 总结MySQL建表、查询优化的一些实用小技巧_MySQL

总结MySQL建表、查询优化的一些实用小技巧_MySQL

Jun 01, 2016 pm 01:25 PM
superior Tips data sheet Update time Record

bitsCN.com

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
Statement of this Website
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Where can I view the records of things I have purchased on Pinduoduo? How to view the records of purchased products? Where can I view the records of things I have purchased on Pinduoduo? How to view the records of purchased products? Mar 12, 2024 pm 07:20 PM

Pinduoduo software provides a lot of good products, you can buy them anytime and anywhere, and the quality of each product is strictly controlled, every product is genuine, and there are many preferential shopping discounts, allowing everyone to shop online Simply can not stop. Enter your mobile phone number to log in online, add multiple delivery addresses and contact information online, and check the latest logistics trends at any time. Product sections of different categories are open, search and swipe up and down to purchase and place orders, and experience convenience without leaving home. With the online shopping service, you can also view all purchase records, including the goods you have purchased, and receive dozens of shopping red envelopes and coupons for free. Now the editor has provided Pinduoduo users with a detailed online way to view purchased product records. method. 1. Open your phone and click on the Pinduoduo icon.

How to view and manage Linux command history How to view and manage Linux command history Aug 01, 2023 pm 09:17 PM

How to View Command History in Linux In Linux, we use the history command to view the list of all previously executed commands. It has a very simple syntax: history Some options for pairing with the history command include: Option description -c clears the command history for the current session -w writes the command history to a file -r reloads the command history from the history file -n Limit the number of output of recent commands Simply run the history command to see a list of all previously executed commands in a Linux terminal: In addition to viewing command history, you can also manage command history and perform modifications to previously executed commands , reverse search command history or even delete history completely

Digital audio output interface on the motherboard-SPDIF OUT Digital audio output interface on the motherboard-SPDIF OUT Jan 14, 2024 pm 04:42 PM

SPDIFOUT connection line sequence on the motherboard. Recently, I encountered a problem regarding the wiring sequence of the wires. I checked online. Some information says that 1, 2, and 4 correspond to out, +5V, and ground; while other information says that 1, 2, and 4 correspond to out, ground, and +5V. The best way is to check your motherboard manual. If you can't find the manual, you can use a multimeter to measure it. Find the ground first, then you can determine the order of the rest of the wiring. How to connect motherboard VDG wiring When connecting the VDG wiring of the motherboard, you need to plug one end of the VGA cable into the VGA interface of the monitor and the other end into the VGA interface of the computer's graphics card. Please be careful not to plug it into the motherboard's VGA port. Once connected, you can

How to check call history in iPhone and export it? How to check call history in iPhone and export it? Jul 05, 2023 pm 12:54 PM

Call recording in iPhone is often underestimated and is one of the most critical features of iPhone. With its simplicity, this feature is of vital importance and can provide important insights about the calls made or received on the device. Whether for work purposes or legal proceedings, the ability to access call records can prove invaluable. In simple terms, call history refers to the entries created on your iPhone whenever you make or receive a call. These logs contain key information, including the contact's name (or number if not saved as a contact), timestamp, duration, and call status (dialed, missed, or not answered). They are a concise record of your communication history. Call history includes call history strips stored on your iPhone

How to view your medication log history in the Health app on iPhone How to view your medication log history in the Health app on iPhone Nov 29, 2023 pm 08:46 PM

iPhone lets you add medications to the Health app to track and manage the medications, vitamins and supplements you take every day. You can then log medications you've taken or skipped when you receive a notification on your device. After you log your medications, you can see how often you took or skipped them to help you track your health. In this post, we will guide you to view the log history of selected medications in the Health app on iPhone. A short guide on how to view your medication log history in the Health App: Go to the Health App>Browse>Medications>Medications>Select a Medication>Options&a

C# Development Advice: Logging and Monitoring Systems C# Development Advice: Logging and Monitoring Systems Nov 22, 2023 pm 08:30 PM

C# Development Suggestions: Logging and Monitoring System Summary: In the software development process, logging and monitoring systems are crucial tools. This article will introduce the role and implementation suggestions of logging and monitoring systems in C# development. Introduction: Logging and monitoring are essential tools in large-scale software development projects. They can help us understand the running status of the program in real time and quickly discover and solve problems. This article will discuss how to use logging and monitoring systems in C# development to improve software quality and development efficiency. The role of logging system

Go language development tips: Alibaba Cloud interface docking practice sharing Go language development tips: Alibaba Cloud interface docking practice sharing Jul 05, 2023 pm 11:49 PM

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

How to log and monitor Java development projects How to log and monitor Java development projects Nov 03, 2023 am 10:09 AM

How to log and monitor Java development projects 1. Background introduction With the rapid development of the Internet, more and more companies have begun to develop Java and build various types of applications. In the development process, logging and monitoring are an important link that cannot be ignored. Through logging and monitoring, developers can discover and solve problems in time to ensure the stability and security of applications. 2. The importance of logging 1. Problem tracking: When an application error occurs, logging can help us quickly locate the problem.

See all articles