MySQL Common script

Source: Internet
Author: User

Exporting to a test environment

Large companies often have a production environment and test environment, the test environment can not synchronize the production environment in real-time data, which requires us to manually synchronize.

Back up to File

We do not need to export the full amount of data from the production environment to the test environment, only the data that meets some of the criteria, such as the last 1 weeks of data, or the last 100 pieces of data

-u-p-h--where"查询语句"--skip-lock-tables  --default-character-set=--no-create-info--> 数据表.sql
Importing to a test environment
-u-p-h< Page_Stats.sql
Exists then is updated, does not exist insert

We need to update the offline database regularly, and when it is updated, there may be duplicate data, that is, data that meets the unique index already exists. At this point, if you delete the data in the database directly, you may get some bugs. There is a way to insert a database in MySQL, insert data if the data does not exist, and update the data if there is data that satisfies the unique index

INSERT INTO table (column_1, column_2, column_3) VALUES (‘column_1_value‘, ‘column_1_value‘, ‘column_3_value‘) ON DUPLICATE KEY UPDATE column_1=‘column_1_value‘, column_2=‘column_2_value‘, column_3=‘column_3_value‘;
Storage optimization

If the online database only records the latest bid information for each advertiser, we want to look at the historical bid information for each advertiser. Then the amount of data will be very large when all advertisers ' bids are recorded every minute, and we need to optimize the storage: If the advertiser's bid information doesn't change, we don't need to record

Database tables

Table: Coupon_bid_realtime
Field: Coupon Bid Record_time
Unique index: Coupon record_time

Check the latest bid information for all advertisers
SELECT A.coupon, A.bid, A.record_time FROM coupon_bid_realtime AS A JOIN (SELECT coupon, MAX(record_time) AS record_time FROM coupon_bid_realtime GROUP BY coupon) AS B ON A.coupon = B.coupon AND A.record_time = B.record_time;
Filter data that doesn't change

Based on The Advertiser's bid information from the previous query, if the advertiser does not change the bid information, it is not inserted

Search Artifact

If you want to see if your query statement makes the best use of the database index, you can use EXPLAIN theEXPLAIN querySQL

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

MySQL Common script

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.