Skip to content

Commit 732f029

Browse files
committed
検索count削減
1 parent 956458a commit 732f029

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

isuumo/webapp/nodejs/app.js

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -241,24 +241,21 @@ app.get("/api/chair/search", async (req, res, next) => {
241241
const pageNum = parseInt(page, 10);
242242
const perPageNum = parseInt(perPage, 10);
243243

244-
const sqlprefix = "SELECT * FROM chair WHERE ";
244+
const sqlprefix = "SELECT SQL_CALC_FOUND_ROWS * FROM chair WHERE ";
245245
const searchCondition = searchQueries.join(" AND ");
246246
const limitOffset = " ORDER BY popularity DESC, id ASC LIMIT ? OFFSET ?";
247-
const countprefix = "SELECT COUNT(*) as count FROM chair WHERE ";
247+
const coundSql = "SELECT FOUND_ROWS() as count";
248248

249249
const getConnection = promisify(db.getConnection.bind(db));
250250
const connection = await getConnection();
251251
const query = promisify(connection.query.bind(connection));
252252
try {
253-
const [{ count }] = await query(
254-
`${countprefix}${searchCondition}`,
255-
queryParams
256-
);
257253
queryParams.push(perPageNum, perPageNum * pageNum);
258254
const chairs = await query(
259255
`${sqlprefix}${searchCondition}${limitOffset}`,
260256
queryParams
261257
);
258+
const [{ count }] = await query(coundSql);
262259
res.json({
263260
count,
264261
chairs: camelcaseKeys(chairs),
@@ -400,7 +397,7 @@ app.get("/api/estate/search", async (req, res, next) => {
400397
const featureConditions = features.split(",");
401398

402399
for (const featureCondition of featureConditions) {
403-
searchQueries.push("FIND_IN_SET(?, features)");
400+
searchQueries.push("FIND_IN_SET(?, features) > 0");
404401
queryParams.push(featureCondition);
405402
}
406403
}
@@ -423,24 +420,21 @@ app.get("/api/estate/search", async (req, res, next) => {
423420
const pageNum = parseInt(page, 10);
424421
const perPageNum = parseInt(perPage, 10);
425422

426-
const sqlprefix = "SELECT * FROM estate WHERE ";
423+
const sqlprefix = "SELECT SQL_CALC_FOUND_ROWS * FROM estate WHERE ";
427424
const searchCondition = searchQueries.join(" AND ");
428425
const limitOffset = " ORDER BY popularity DESC, id ASC LIMIT ? OFFSET ?";
429-
const countprefix = "SELECT COUNT(*) as count FROM estate WHERE ";
426+
const coundSql = "SELECT FOUND_ROWS() as count";
430427

431428
const getConnection = promisify(db.getConnection.bind(db));
432429
const connection = await getConnection();
433430
const query = promisify(connection.query.bind(connection));
434431
try {
435-
const [{ count }] = await query(
436-
`${countprefix}${searchCondition}`,
437-
queryParams
438-
);
439432
queryParams.push(perPageNum, perPageNum * pageNum);
440433
const estates = await query(
441434
`${sqlprefix}${searchCondition}${limitOffset}`,
442435
queryParams
443436
);
437+
const [{ count }] = await query(coundSql);
444438
res.json({
445439
count,
446440
estates: camelcaseKeys(estates),

0 commit comments

Comments
 (0)