Select the redundant data and multi-table query

Select data redundancy and multi-table query

Recent bored at home, and the front end of a small partner in jointly developing a blog site, experiencing performance on a mysql optimization problem in data processing, query and linked table data redundant operation, it should be how to choose, this would involve the database and counter-paradigm paradigm

Problem Description

Get blog posts comments when the need to obtain reviewers nickname and avatar picture links, so this time is to enable multi-table query, or whether it uses data redundancy, some may think about it directly linking table query, it may Some people would say that data redundancy, space for time

analysis

But when we encounter this problem, we need to think about other problems that may occur in the

When we encounter such a situation I now comment when using the method of data redundancy is not desirable, First we analyze the performance of even-table query

# 建表语句
CREATE TABLE `comment` (
  `id` int(11) NOT NULL,
  `user_id` int(11) NOT NULL,
  `article_id` int(11) NOT NULL,
  `detail` varchar(100) NOT NULL,
  `publish_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `article_user_id` (`article_id`,`user_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8

Here Insert Picture Description

From the analysis of multi-table queries, we can see the first pass the query (and get comments of an article), using the index query, the second query (for user information), is based on the primary key query. In fact, this multi-table queries, performance is not really weak

Data redundancy

If we use the data redundantly stored in the back of each reviewer's comments nickname and avatar links, so when we query, you can save time for the second query, but resulting in a waste of space, which also not the most important, imagine, if you alter a nickname or a username when their every need to get a comment from the comment and modify tables inside, so very low performance.

So when will it uses data redundancy

When the associated data rarely changes and the amount of data, you can use data redundancy, like students of professional, contingency table if the query words, every time the student information form and related professional information table, but when we simply query the students and their professional time, have to go to the table with the query, when a large amount of big data, it will cause performance degradation, if this time the use of data redundancy, the professional name of redundant information in time to the student , sacrifice a little space, in exchange for query performance greatly improved

Published 17 original articles · won praise 1 · views 645

Guess you like

Origin blog.csdn.net/c_c_y_CC/article/details/104675707
Recommended