What my query is doing:I have two SQL tables and I want to copy datarows from the one table to the other table. I am doing this for statistics. The source table includes all current member-sessions/logins and with my statistics table I also want to see member-logins of the past. Thus I want to store these data into my bot_sessions_statistics table as well to make sure they are not being updated anymore:The original query:INSERT INTO bot_sessions_statistics (member_id, session_token, username, ip_address)SELECT sessions.member_id, sessions.session_token, sessions.username, sessions.ip_addressFROM bot_sessions AS sessionsLEFT JOIN(    SELECT stats.member_id, stats.session_token, stats.username, stats.ip_address    FROM bot_sessions_statistics as stats    WHERE date_active >= date(NOW())) AS statsON sessions.member_id = stats.member_idWHERE latest_renewal >= date(NOW())AND stats.member_id IS NULLThe performance of the original query:Query_time: 86.364613  Lock_time: 0.000085 Rows_sent: 0  Rows_examined: 1088312551The table structures:Table bot_sessions: Table bot_sessions_statistics: The performance of single queries:SELECT sessions.member_id, sessions.session_token, sessions.username, sessions.ip_addressFROM bot_sessions AS sessionsWHERE latest_renewal >= date(NOW())Returns 44.2k rows (Duration: 0.078s / Fetch: 1.607s)SELECT stats.member_id, stats.session_token, stats.username, stats.ip_addressFROM bot_sessions_statistics as statsWHERE date_active >= date(NOW())Returns 23.3k rows (Duration: 0.047s / Fetch 0.920s)
Archiving an SQL table of sessions and statistics
performance;sql;mysql;join