1
0
Code Issues Pull Requests Projects Releases Wiki Activity GitHub Gitee

避免重复查库;DISTICT查出来后通过代码去重

This commit is contained in:
程序员小墨 2022-10-20 00:45:12 +08:00
parent 0862db5db9
commit e521c139e2
5 changed files with 23 additions and 21 deletions

View File

@ -38,7 +38,9 @@ async function fetchAll({ args = {}, isUpdate = false }) {
args.max ? `album_id <= ${args.max}` : '1=1',
].join(' AND ');
var sql = `
SELECT DISTINCT album_id FROM song_album_relation WHERE ${whereClause} AND album_id NOT IN ( SELECT album_id FROM album )
-- 查出来通过代码去重提高速度
-- SELECT DISTINCT album_id FROM song_album_relation WHERE ${whereClause} AND album_id NOT IN ( SELECT album_id FROM album )
SELECT album_id FROM song_album_relation WHERE ${whereClause} AND album_id NOT IN ( SELECT album_id FROM album )
${args.limit ? `LIMIT ${args.limit}` : ''}
`;
console.log(sql);
@ -46,6 +48,7 @@ async function fetchAll({ args = {}, isUpdate = false }) {
var albumIds = await dbUtils.query(sql, []);
albumIds = albumIds.map(item => item.album_id);
albumIds = Array.from(new Set(albumIds));
for (let i = 0; i < albumIds.length; i++) {
await global.checkIsExit();
const albumId = albumIds[i];
@ -155,15 +158,12 @@ async function fetch({ albumId, debug = false, update = false }) {
songIds: songIds,
};
// console.log("albumInfo", albumInfo);
songIds.forEach(function (songId) {
if (isNaN(Number(songId)) || Number(songId) === 0 || isNaN(Number(albumId)) || Number(songId) === 0)
return;
dbUtils.query('INSERT IGNORE INTO song_album_relation SET ?', {
song_id: songId,
album_id: albumId,
});
});
dbUtils.query(update ? `UPDATE album SET ? WHERE album_id = ${albumId}` : 'INSERT IGNORE INTO album SET ?', {
if (albumId > 0) {
await dbUtils.query(`
INSERT IGNORE INTO song_album_relation (song_id, album_id) VALUES ?
`, [songIds.map(songId => [Number(songId), albumId])]);
}
await dbUtils.query(update ? `UPDATE album SET ? WHERE album_id = ${albumId}` : 'INSERT IGNORE INTO album SET ?', {
album_id: albumInfo.albumId,
title: albumInfo.title,
description: albumInfo.description,

View File

@ -30,13 +30,16 @@ async function fetchAll({ args = {} }) {
args.max ? `artist_id <= ${args.max}` : '1=1',
].join(' AND ');
var sql = `
SELECT DISTINCT artist_id FROM song_artist_relation WHERE ${whereClause} AND artist_id NOT IN ( SELECT artist_id FROM artist )
-- 查出来通过代码去重提高速度
-- SELECT DISTINCT artist_id FROM song_artist_relation WHERE ${whereClause} AND artist_id NOT IN ( SELECT artist_id FROM artist )
SELECT artist_id FROM song_artist_relation WHERE ${whereClause} AND artist_id NOT IN ( SELECT artist_id FROM artist )
${args.limit ? `LIMIT ${args.limit}` : ''}
`;
console.log(sql);
var artistIds = await dbUtils.query(sql, []);
artistIds = artistIds.map(item => item.artist_id);
artistIds = Array.from(new Set(artistIds));
for (let i = 0; i < artistIds.length; i++) {
await global.checkIsExit();
const artistId = artistIds[i];
@ -107,14 +110,11 @@ async function fetch({ artistId, debug = false }) {
songIds: songIds,
};
// console.log("artistInfo", artistInfo);
songIds.forEach(function (songId) {
if (isNaN(Number(songId)) || Number(songId) === 0 || isNaN(Number(artistId)) || Number(artistId) === 0)
return;
dbUtils.query('INSERT IGNORE INTO song_artist_relation SET ?', {
song_id: songId,
artist_id: artistId,
});
});
if (artistId > 0) {
await dbUtils.query(`
INSERT IGNORE INTO song_artist_relation (song_id, artist_id) VALUES ?
`, [songIds.map(songId => [Number(songId), artistId])]);
}
dbUtils.query('INSERT IGNORE INTO artist SET ?', {
artist_id: artistInfo.artistId,
title: artistInfo.title,

View File

@ -63,6 +63,7 @@ async function fetch({ songId, debug = false }) {
if (typeof lyric == "undefined") {
// 这首歌爬song的时候还在但是现在不在了
// 这里数据丢了不要紧所以不加await
dbUtils.query('INSERT IGNORE INTO lyric SET ?', {
song_id: songId,
lyric: '',
@ -77,6 +78,7 @@ async function fetch({ songId, debug = false }) {
version: lyric.version,
};
// console.log("lyricInfo", lyricInfo);
// 这里数据丢了不要紧所以不加await
dbUtils.query('INSERT IGNORE INTO lyric SET ?', {
song_id: lyricInfo.songId,
lyric: lyricInfo.lyric,

View File

@ -15,9 +15,9 @@ async function fetchAll({ args = {} }) {
args.max ? `song_id <= ${args.max}` : '1=1',
].join(' AND ');
var sql = `
SELECT DISTINCT song_id FROM song_artist_relation WHERE ${whereClause} AND song_id NOT IN ( SELECT song_id FROM song )
SELECT song_id FROM song_artist_relation WHERE ${whereClause} AND song_id NOT IN ( SELECT song_id FROM song )
UNION
SELECT DISTINCT song_id FROM song_album_relation WHERE ${whereClause} AND song_id NOT IN ( SELECT song_id FROM song )
SELECT song_id FROM song_album_relation WHERE ${whereClause} AND song_id NOT IN ( SELECT song_id FROM song )
${args.order ? `ORDER BY song_id ${args.order}` : ''}
${args.limit ? `LIMIT ${args.limit}` : ''}
`;