-- 更新统计数据
INSERT INTO analysis (`key`, `value`) VALUES ('songCount', (SELECT count(*) as count FROM song) ) ON DUPLICATE KEY UPDATE `value` = VALUES(`value`);
INSERT INTO analysis (`key`, `value`) VALUES ('songWaiting', (SELECT count(DISTINCT song_id) AS count
                FROM ( SELECT song_id FROM song_artist_relation UNION SELECT song_id FROM song_album_relation ) t_tmp
                WHERE song_id NOT IN ( SELECT song_id FROM song )) ) ON DUPLICATE KEY UPDATE `value` = VALUES(`value`);
INSERT INTO analysis (`key`, `value`) VALUES ('playlistCount', (SELECT count(*) AS count FROM playlist) ) ON DUPLICATE KEY UPDATE `value` = VALUES(`value`);
INSERT INTO analysis (`key`, `value`) VALUES ('albumCount', (SELECT count(*) as count FROM album) ) ON DUPLICATE KEY UPDATE `value` = VALUES(`value`);
INSERT INTO analysis (`key`, `value`) VALUES ('albumWaiting', (SELECT count( DISTINCT album_id ) as count FROM song_album_relation WHERE album_id NOT IN ( SELECT album_id FROM album )) ) ON DUPLICATE KEY UPDATE `value` = VALUES(`value`);
INSERT INTO analysis (`key`, `value`) VALUES ('artistCount', (SELECT count(*) AS count FROM artist) ) ON DUPLICATE KEY UPDATE `value` = VALUES(`value`);
INSERT INTO analysis (`key`, `value`) VALUES ('artistWaiting', (SELECT count( DISTINCT artist_id ) as count FROM song_artist_relation WHERE artist_id NOT IN ( SELECT artist_id FROM artist )) ) ON DUPLICATE KEY UPDATE `value` = VALUES(`value`);
INSERT INTO analysis (`key`, `value`) VALUES ('lyricCount', (SELECT count(*) AS count FROM lyric) ) ON DUPLICATE KEY UPDATE `value` = VALUES(`value`);
INSERT INTO analysis (`key`, `value`) VALUES ('commentCount', (SELECT count( DISTINCT song_id ) AS count FROM comment) ) ON DUPLICATE KEY UPDATE `value` = VALUES(`value`);
INSERT INTO analysis (`key`, `value`) VALUES ('commentTotalCount', (SELECT count(*) AS count FROM comment) ) ON DUPLICATE KEY UPDATE `value` = VALUES(`value`);
INSERT INTO analysis (`key`, `value`) VALUES ('userCount', (SELECT count(*) AS count FROM user) ) ON DUPLICATE KEY UPDATE `value` = VALUES(`value`);
INSERT INTO analysis (`key`, `value`) VALUES ('songPlaylistCount', (SELECT count(*) AS count FROM song_playlist_relation) ) ON DUPLICATE KEY UPDATE `value` = VALUES(`value`);
INSERT INTO analysis (`key`, `value`) VALUES ('songAlbumCount', (SELECT count(*) AS count FROM song_album_relation) ) ON DUPLICATE KEY UPDATE `value` = VALUES(`value`);
INSERT INTO analysis (`key`, `value`) VALUES ('songArtistCount', (SELECT count(*) AS count FROM song_artist_relation) ) ON DUPLICATE KEY UPDATE `value` = VALUES(`value`);



-- 更新后初次全表扫描
INSERT IGNORE INTO wait_song (song_id) SELECT song_id FROM song_artist_relation   WHERE song_id NOT IN ( SELECT song_id FROM song )
INSERT IGNORE INTO wait_song (song_id) SELECT song_id FROM song_album_relation    WHERE song_id NOT IN ( SELECT song_id FROM song )
INSERT IGNORE INTO wait_song (song_id) SELECT song_id FROM song_playlist_relation WHERE song_id NOT IN ( SELECT song_id FROM song )

-- 后续只需要扫描 wait_check 表
INSERT IGNORE INTO wait_song (song_id) SELECT song_id FROM wait_check_song WHERE song_id NOT IN ( SELECT song_id FROM song )



-- 查看需要爬取的 song 的分布
SELECT cast( format( t_tmp.song_id / 10000000, 0) * 10000000 as UNSIGNED ) as s, count(*) as count
FROM (
    SELECT DISTINCT song_id FROM song_album_relation
    UNION
    SELECT DISTINCT song_id FROM song_artist_relation
) as t_tmp
WHERE song_id NOT IN ( SELECT song_id FROM song )
GROUP BY s
ORDER BY s DESC

-- 查看需要爬取的 album 的分布
SELECT cast( format( album_id / 1000000, 0) * 1000000 as UNSIGNED ) as s, count(*) as count
FROM song_album_relation
WHERE album_id NOT IN ( SELECT album_id FROM album )
GROUP BY s
ORDER BY s DESC

-- 查看需要爬取的 artist 的分布
SELECT cast( format( artist_id / 2000000, 0) * 2000000 as UNSIGNED ) as s, count(*) as count
FROM song_artist_relation
WHERE artist_id NOT IN ( SELECT artist_id FROM artist )
GROUP BY s
ORDER BY s DESC

-- 查看需要爬取的 comment 的分布
SELECT cast( format( song_id / 10000000, 0) * 10000000 as UNSIGNED ) as s, count(*) as count
FROM comment_progress
WHERE current_status != 2
GROUP BY s
ORDER BY s DESC

-- 查看需要爬取的 lyric 的分布
SELECT cast( format( song_id / 10000000, 0) * 10000000 as UNSIGNED ) as s, count(*) as count
FROM song
WHERE song_id NOT IN ( SELECT song_id FROM lyric )
GROUP BY s
ORDER BY s DESC




-- 查看本地已有 song 的分布
SELECT cast( format( song_id / 10000000, 0) * 10000000 as UNSIGNED ) as s, count(*) as count
FROM song
GROUP BY s
ORDER BY s DESC

-- 查看本地已有 user 的分布
SELECT cast( format( user_id / 10000000, 0) * 10000000 as UNSIGNED ) as s, count(*) as count
FROM user
GROUP BY s
ORDER BY s DESC

-- 查看本地已有 album 的分布
SELECT cast( format( album_id / 1000000, 0) * 1000000 as UNSIGNED ) as s, count(*) as count
FROM album
GROUP BY s
ORDER BY s DESC

-- 查看本地已有 artist 的分布
SELECT cast( format( artist_id / 2000000, 0) * 2000000 as UNSIGNED ) as s, count(*) as count
FROM artist
GROUP BY s
ORDER BY s DESC

-- 查看本地已有 playlist 的分布
SELECT cast( format( playlist_id / 2000000, 0) * 2000000 as UNSIGNED ) as s, count(*) as count
FROM playlist
GROUP BY s
ORDER BY s DESC



-- optimize table
optimize table analysis;
optimize table album;
optimize table artist;
optimize table category;
optimize table comment;
optimize table comment_progress;
optimize table log;
optimize table lyric;
optimize table playlist;
optimize table song;
optimize table song_album_relation;
optimize table song_artist_relation;
optimize table song_playlist_relation;
optimize table user;
optimize table wait_check_album;
optimize table wait_check_artist;
optimize table wait_check_lyric;
optimize table wait_check_song;
optimize table wait_fetch_album;
optimize table wait_fetch_artist;
optimize table wait_fetch_lyric;
optimize table wait_fetch_song;



-- 移动数据库
RENAME TABLE neteasemusic.analysis TO neteasemusic_develop.analysis;
RENAME TABLE neteasemusic.album TO neteasemusic_develop.album;
RENAME TABLE neteasemusic.artist TO neteasemusic_develop.artist;
RENAME TABLE neteasemusic.category TO neteasemusic_develop.category;
RENAME TABLE neteasemusic.comment TO neteasemusic_develop.comment;
RENAME TABLE neteasemusic.comment_progress TO neteasemusic_develop.comment_progress;
RENAME TABLE neteasemusic.log TO neteasemusic_develop.log;
RENAME TABLE neteasemusic.lyric TO neteasemusic_develop.lyric;
RENAME TABLE neteasemusic.playlist TO neteasemusic_develop.playlist;
RENAME TABLE neteasemusic.song TO neteasemusic_develop.song;
RENAME TABLE neteasemusic.song_album_relation TO neteasemusic_develop.song_album_relation;
RENAME TABLE neteasemusic.song_artist_relation TO neteasemusic_develop.song_artist_relation;
RENAME TABLE neteasemusic.song_playlist_relation TO neteasemusic_develop.song_playlist_relation;
RENAME TABLE neteasemusic.user TO neteasemusic_develop.user;
RENAME TABLE neteasemusic.wait_check_album TO neteasemusic_develop.wait_check_album;
RENAME TABLE neteasemusic.wait_check_artist TO neteasemusic_develop.wait_check_artist;
RENAME TABLE neteasemusic.wait_check_lyric TO neteasemusic_develop.wait_check_lyric;
RENAME TABLE neteasemusic.wait_check_song TO neteasemusic_develop.wait_check_song;
RENAME TABLE neteasemusic.wait_fetch_album TO neteasemusic_develop.wait_fetch_album;
RENAME TABLE neteasemusic.wait_fetch_artist TO neteasemusic_develop.wait_fetch_artist;
RENAME TABLE neteasemusic.wait_fetch_lyric TO neteasemusic_develop.wait_fetch_lyric;
RENAME TABLE neteasemusic.wait_fetch_song TO neteasemusic_develop.wait_fetch_song;




-- 查询单个数据库里面各个表所占磁盘空间大小包括其索引的大小
SELECT
    table_schema AS '数据库',
    table_name AS '表名',
    table_rows AS '记录数',
    TRUNCATE (data_length / 1024 / 1024, 2) AS '数据容量(MB)',
    TRUNCATE (index_length / 1024 / 1024, 2) AS '索引容量(MB)',
    TRUNCATE ((data_length + index_length) / 1024 / 1024 / 1024, 2) AS '总容量(GB)'
FROM
    information_schema.TABLES
WHERE
    table_schema = 'neteasemusic'
ORDER BY
    table_rows DESC;



-- analysis
-- album
-- artist
-- category
-- comment
-- comment_progress
-- log
-- lyric
-- playlist
-- song
-- song_album_relation
-- song_artist_relation
-- song_playlist_relation
-- user
-- wait_check_album
-- wait_check_artist
-- wait_check_lyric
-- wait_check_song
-- wait_fetch_album
-- wait_fetch_artist
-- wait_fetch_lyric
-- wait_fetch_song
