Skip to content

Commit 63ee4e1

Browse files
fix: 优化首页查询效率
1 parent d84e821 commit 63ee4e1

File tree

2 files changed

+43
-42
lines changed

2 files changed

+43
-42
lines changed

blogvue/vue.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ module.exports = {
6363
devServer: {
6464
proxy: {
6565
'/api': { // 以/api开头的接口都代理到target指定的域名下
66-
target: 'https://immortalboy.cn'
66+
target: 'http://127.0.0.1:7001'
6767
}
6868
}
6969
}

service/app/controller/blog/index.js

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,28 @@ class IndexController extends controller {
1010

1111
async getUserInfoById() {
1212
const id = this.ctx.params.id;
13+
console.log('id', id);
1314
const selectSql = `SELECT username as username, portrait as portrait, bg_img as bgImg,
1415
qq_account as qqAccount, wechat_account as weChatAccount, github_url as githubUrl,
1516
logo_name as logoName, logo_sub as logoSub
1617
FROM admin_user WHERE id = ?`;
1718
const userInfoPromise = this.app.mysql.query(selectSql, [ id ]);
18-
const articleCountPromise = this.app.mysql.query('SELECT count(*) as count from article');
19-
const talkCountPromise = this.app.mysql.query('SELECT count(*) as count from talk');
20-
const novelCountPromise = this.app.mysql.query('SELECT count(*) as count from novel');
21-
Promise.all([userInfoPromise, articleCountPromise, talkCountPromise, novelCountPromise])
22-
.then(([userInfoResult, articleCountResult, talkCountResult, novelCountResult]) => {
23-
if (userInfoResult.length > 0) {
24-
const dataObj = userInfoResult[0];
25-
dataObj.articleCount = articleCountResult[0].count;
26-
dataObj.talkCount = talkCountResult[0].count;
27-
dataObj.novelCount = novelCountResult[0].count;
28-
this.ctx.body = { success: true, data: dataObj };
29-
} else {
30-
this.ctx.body = { success: false, message: '获取个人信息失败' };
31-
}
32-
})
19+
const articleCountPromise = this.app.mysql.query('SELECT count(id) as count from article');
20+
const talkCountPromise = this.app.mysql.query('SELECT count(id) as count from talk');
21+
const novelCountPromise = this.app.mysql.query('SELECT (max(id)-min(id)+1) as count from novel where is_deleted = 0');
22+
const [userInfoResult, articleCountResult, talkCountResult, novelCountResult] = await Promise.all([userInfoPromise, articleCountPromise, talkCountPromise, novelCountPromise])
23+
if (userInfoResult.length > 0) {
24+
const dataObj = userInfoResult[0];
25+
dataObj.articleCount = articleCountResult[0].count;
26+
dataObj.talkCount = talkCountResult[0].count;
27+
dataObj.novelCount = novelCountResult[0].count;
28+
console.log('dataObj', dataObj);
29+
this.ctx.body = { success: true, data: dataObj };
30+
console.log(this.ctx.body);
31+
} else {
32+
this.ctx.body = { success: false, message: '获取个人信息失败' };
33+
}
34+
3335
}
3436

3537
async getAdverList() {
@@ -57,27 +59,26 @@ class IndexController extends controller {
5759
FROM article LEFT JOIN article_type
5860
ON article.type_id = article_type.id WHERE article.is_publish = 1 ORDER BY article.publish_time DESC LIMIT ?,?`;
5961
const articleListPromise = await this.app.mysql.query(sql, [ request.offset, request.limit ]);
60-
const countPromise = await this.app.mysql.query('SELECT count(*) as total FROM article');
61-
Promise.all([articleListPromise, countPromise])
62-
.then(([articleListResult, countResult]) => {
63-
if (articleListResult.length > 0) {
64-
for (const item of articleListResult) {
65-
item.listType = 'article';
66-
}
67-
this.ctx.body = {
68-
success: true,
69-
data: {
70-
total: countResult[0].total,
71-
list: articleListResult,
72-
},
73-
};
74-
} else {
75-
this.ctx.body = {
76-
success: false,
77-
message: '获取文章列表失败',
78-
};
62+
const countPromise = await this.app.mysql.query('SELECT (max(id)-min(id)+1) as count from novel where is_deleted = 0');
63+
const [articleListResult, countResult] = await Promise.all([articleListPromise, countPromise])
64+
if (articleListResult.length > 0) {
65+
for (const item of articleListResult) {
66+
item.listType = 'article';
7967
}
80-
})
68+
this.ctx.body = {
69+
success: true,
70+
data: {
71+
total: countResult[0].total,
72+
list: articleListResult,
73+
},
74+
};
75+
console.log(this.ctx.body);
76+
} else {
77+
this.ctx.body = {
78+
success: false,
79+
message: '获取文章列表失败',
80+
};
81+
}
8182
}
8283

8384
async getTalkList() {
@@ -88,7 +89,7 @@ class IndexController extends controller {
8889
LEFT JOIN admin_user AS user ON talk.user_id = user.id
8990
ORDER BY talk.publish_time DESC LIMIT ?,?`;
9091
const result = await this.app.mysql.query(sql, [ request.offset, request.limit ]);
91-
const countResult = await this.app.mysql.query('SELECT count(*) as total FROM talk');
92+
const countResult = await this.app.mysql.query('SELECT count(id) as total FROM talk');
9293
if (result.length > 0) {
9394
const commentsql = 'SELECT count(*) as count FROM visitor_comment as comment WHERE comment.talk_id = ?';
9495
for (const item of result) {
@@ -124,7 +125,7 @@ class IndexController extends controller {
124125
LEFT JOIN novel ON chapter.novel_id = novel.id
125126
ORDER BY chapter.updatetime DESC LIMIT ?,?`;
126127
const sqlResult = await this.app.mysql.query(sql, [ request.offset, request.limit ]);
127-
const countResult = await this.app.mysql.query('SELECT count(*) as total FROM novel_chapter');
128+
const countResult = await this.app.mysql.query('SELECT (max(id)-min(id)+1) as count from novel_chapter where is_deleted = 0');
128129
if (sqlResult.length > 0) {
129130
for (const item of sqlResult) {
130131
item.listType = 'novel';
@@ -162,8 +163,8 @@ class IndexController extends controller {
162163

163164
async getIndexListApp() {
164165
const request = this.ctx.request.body;
165-
const chapterCountResult = await this.app.mysql.query('SELECT count(*) AS total FROM novel_chapter');
166-
const articleCountResult = await this.app.mysql.query('SELECT count(*) AS total FROM article');
166+
const chapterCountResult = await this.app.mysql.query('SELECT (max(id)-min(id)+1) AS total FROM novel_chapter where is_deleted = 0');
167+
const articleCountResult = await this.app.mysql.query('SELECT (max(id)-min(id)+1) AS total FROM article where is_deleted = 0');
167168
const chapterSql = 'SELECT id AS chapterId, updatetime AS updateTime FROM novel_chapter';
168169
const articleSql = 'SELECT id AS articleId, publish_time AS updateTime FROM article';
169170
const chapterList = await this.app.mysql.query(chapterSql);
@@ -230,7 +231,7 @@ class IndexController extends controller {
230231
introduce_img AS introduceImg, view_count AS viewCount
231232
FROM article where title like '%${request.searchValue}%' AND is_publish = 1 ORDER BY publish_time DESC LIMIT ?,?`;
232233
const sqlResult = await this.app.mysql.query(sql, [ request.offset, request.limit ]);
233-
const countResult = await this.app.mysql.query('SELECT count(*) as total FROM article');
234+
const countResult = await this.app.mysql.query('SELECT (max(id)-min(id)+1) as total FROM article where is_deleted = 0');
234235
if (sqlResult.length > 0) {
235236
for (const item of sqlResult) {
236237
item.listType = 'article';
@@ -257,7 +258,7 @@ class IndexController extends controller {
257258
LEFT JOIN novel ON chapter.novel_id = novel.id
258259
WHERE chapter.name like '%${request.searchValue}%' ORDER BY updatetime DESC LIMIT ?,?`;
259260
const sqlResult = await this.app.mysql.query(sql, [ request.offset, request.limit ]);
260-
const countResult = await this.app.mysql.query('SELECT count(*) as total FROM novel_chapter');
261+
const countResult = await this.app.mysql.query('SELECT (max(id)-min(id)+1) as total FROM novel_chapter where is_deleted = 0');
261262
if (sqlResult.length > 0) {
262263
for (const item of sqlResult) {
263264
item.listType = 'novel';

0 commit comments

Comments
 (0)