ASP. NET Website optimization database optimization measures use master/Slave databases (below)

Source: Internet
Author: User

In the previous article, we configured the Master/Slave database. Now we tryProgramUse the master-slave database.

The master-slave database is a publish-subscribe relationship. The publisher and subscriber are not synchronized in real time. Generally, there is a delay of several minutes, and even several hours. Therefore, we need to avoid the problem of latency through reasonable use.

We hope that the primary database can participate in queries as little as possible to improve the timeliness of writing. At the same time, we also want the slave database to share the pressure of the primary database as much as possible without affecting the accuracy and timeliness of data reading.

The master and slave databases must configure two connection strings, conn_master and conn_slave, in the configuration file. We need to set some rules to determine whether the current query should be queried from the master database or from the slave database. This rule is not formulated and can only be determined based on business needs. Here are a few examples:

1. Taking the detailed page of The Douban book as the assumption scenario, you can click here to see the structure on the next page (I am not a Douban technology, here I just take this page as an example)
We will analyze the data required to present this page and the effectiveness requirements of the data.
1) Timeliness requirements for detailed information of books: Timely requirements
2) effectiveness of common tags of Douban members: No need to timely
3) books that people who like to read this book also like to read are data analysis and do not need to be timely
4) The latest book reviews must be timely
5) the number of users reading this book is not timely
6) The group that people like this book often go to does not need to be timely in analyzing data.
From the above analysis, we can see that only 1), 4) the two data items need to be read from the master database, while 2), 3), 5), 6) the non-timely data can be read from the slave database. Of course, we can cache these ineffective data.

2. taking the forum post list page as the assumption scenario, Forum players like to pin their posts to the first page for more people to pay attention, there are very few people who read the post after 50 pages. Based on this business logic feature, we can decide to read the Post list data from the master database when the user accesses the first 50 pages, when a user accesses data with more than 50 pages, the user queries the data from the slave database.

3. taking orders as an example, orders that have been placed for more than three months will not change any more. If we design the order number as the date format, when you query an order based on the order number, you can determine whether to access the master database or slave database based on the order number.

Taking the third scenario as an example, let's write a simple example.CodeCheck

 
// The format of orderno is 20100528120105000001, that is, yyyymmddhhmmss + Serial Number public orderinfo getorder (string orderno) {string connstring = connstringgetter. getfororder (orderno); Using (sqlconnection conn = new sqlconnection (connstring )){...}} public class connstringgetter {public static string getfororder (string orderno) {int year = int. parse (orderno. substring (0, 4); int money = int. parse (orderno. substring (4, 2); int date = int. parse (orderno. substring (6, 2); datetime ordertime = new datetime (year, money, date); timespan Ts = datetime. now-ordertime; // determines whether to use the master database or slave database if (TS. totaldays> 30) return configurationmanager. connectionstrings ["conn_slave"]. connectionstring; return configurationmanager. connectionstrings ["conn_master"]. connectionstring ;}}

Correct use of the Master/Slave database can greatly improve the system performance. The choice of using the master database or slave database depends on the business logic.

 

Related Articles: ASP. NET Website optimization database optimization measures use master/Slave databases (on)

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.