db优化not in改为left join

This commit is contained in:
aozhiwei 2020-07-04 07:40:43 +08:00
parent b570f177a9
commit d512300daf
5 changed files with 106 additions and 42 deletions

View File

@ -201,15 +201,12 @@ void DBHelper::ShuaOfflineUsers(Player* hum)
++shua_users_offline_times;
std::set<std::string>& exclude_account_ids = hum->GetExcludeAccountIds();
if (cache_users_hash.size() < 500 && !exclude_account_ids.empty()) {
std::string fmtstr = "'',";
std::string fmtstr = "SELECT '' AS account_id";
std::vector<a8::XValue> sql_params;
for (auto& account_id : exclude_account_ids) {
fmtstr += "'%s',";
fmtstr += " UNION SELECT '%s'";
sql_params.push_back(a8::XValue(account_id));
}
if (!fmtstr.empty()) {
fmtstr = fmtstr.substr(0, fmtstr.size() - 1);
}
auto on_ok =
[] (a8::XParams& param, const f8::DataSet* data_set)
{
@ -241,10 +238,12 @@ void DBHelper::ShuaOfflineUsers(Player* hum)
(
conn_info,
(
"SELECT idx, account_id, nickname, avatar_url, sex, data_version1, user_value1, "
" user_value2, user_value3, last_logintime "
"FROM `user` WHERE idx > (SELECT 9999 + FLOOR(RAND() * (MAX(idx) - 10000)) FROM `user`)"
" AND account_id NOT IN(" + fmtstr + ") LIMIT 1, 10;"
"SELECT A.idx, A.account_id, A.nickname, A.avatar_url, A.sex, A.data_version1, A.user_value1, "
" A.user_value2, A.user_value3, A.last_logintime "
"FROM `user` A "
" LEFT JOIN (" + fmtstr + ") AS B ON B.account_id = A.account_id "
"WHERE A.idx > (SELECT 9999 + FLOOR(RAND() * (MAX(idx) - 10000)) FROM `user`)"
" B.account_id IS NULL LIMIT 1, 10;"
).c_str(),
sql_params,
a8::XParams()

View File

@ -259,29 +259,27 @@ void Player::_CMFriendApplyList(f8::MsgHdr& hdr, const cs::CMFriendApplyList& ms
paging_copy->set_page_size(20);
#endif
a8::XObject conn_info = DBEngine::Instance()->GetConnInfo(myself.crc32_code);
std::string fmtstr = "'',";
std::string fmtstr = "SELECT '' AS account_id";
std::vector<a8::XValue> sql_params;
{
sql_params.push_back(a8::XValue(last_apply_idx_));
sql_params.push_back(a8::XValue(myself.base_data.account_id));
for (auto& pair : black_hash_) {
fmtstr += "'%s',";
fmtstr += " UNION SELECT '%s'";
sql_params.push_back(a8::XValue(pair.second->base_data.account_id));
}
if (!fmtstr.empty()) {
fmtstr = fmtstr.substr(0, fmtstr.size() - 1);
}
sql_params.push_back(a8::XValue(last_apply_idx_));
sql_params.push_back(a8::XValue(myself.base_data.account_id));
}
DBEngine::Instance()->ExecAsyncQuery
(
conn_info,
(
"SELECT idx, applyid, target_id, sender_id, sender_nickname, "
" sender_avatar_url, sender_sex, sender_data_version1, "
" sender_user_value1, sender_user_value2, sender_user_value3, status "
"FROM friend_apply WHERE idx > %d AND target_id='%s' AND status=0 "
" AND sender_id NOT IN(" + fmtstr + ");"
"SELECT A.idx, A.applyid, A.target_id, A.sender_id, A.sender_nickname, "
" A.sender_avatar_url, A.sender_sex, A.sender_data_version1, "
" A.sender_user_value1, A.sender_user_value2, A.sender_user_value3, A.status "
"FROM friend_apply A "
" LEFT JOIN (" + fmtstr + ") AS B ON B.account_id = A.sender_id "
"WHERE A.idx > %d AND A.target_id='%s' AND A.status=0 AND "
" B.account_id IS NULL;"
).c_str(),
sql_params,
a8::XParams()
@ -1295,27 +1293,25 @@ void Player::RecalcRedPoint()
};
a8::XObject conn_info = DBEngine::Instance()->GetConnInfo(myself.crc32_code);
std::string fmtstr = "'',";
std::string fmtstr = "SELECT '' AS account_id";
std::vector<a8::XValue> sql_params;
{
sql_params.push_back(a8::XValue(last_apply_idx_));
sql_params.push_back(a8::XValue(myself.base_data.account_id));
for (auto& pair : black_hash_) {
fmtstr += "'%s',";
fmtstr += " UNION SELECT '%s'";
sql_params.push_back(a8::XValue(pair.second->base_data.account_id));
}
if (!fmtstr.empty()) {
fmtstr = fmtstr.substr(0, fmtstr.size() - 1);
}
sql_params.push_back(a8::XValue(last_apply_idx_));
sql_params.push_back(a8::XValue(myself.base_data.account_id));
}
DBEngine::Instance()->ExecAsyncQuery
(
conn_info,
(
"SELECT idx, applyid "
"FROM friend_apply WHERE idx > %d AND target_id='%s' AND status=0 "
" AND sender_id NOT IN(" + fmtstr + ") LIMIT 1;"
"SELECT A.idx, A.applyid "
"FROM friend_apply A "
" LEFT JOIN (" + fmtstr + ") AS B ON B.account_id = A.sender_id "
"WHERE A.idx > %d AND A.target_id='%s' AND A.status=0 AND "
" B.account_id IS NULL LIMIT 1;"
).c_str(),
sql_params,
a8::XParams()

View File

@ -0,0 +1,60 @@
#include "precompile.h"
#include <a8/timer.h>
#include <a8/ioloop.h>
#include "perfmonitor.h"
#include "app.h"
static void SavePerfLog()
{
a8::UdpLog::Instance()->Info
("max_run_delay_time:%d max_timer_idle:%d "
"in_data_size:%d out_data_size:%d msgnode_size:%d read_count:%d ",
{
PerfMonitor::Instance()->max_run_delay_time,
PerfMonitor::Instance()->max_timer_idle,
PerfMonitor::Instance()->in_data_size,
PerfMonitor::Instance()->out_data_size,
App::Instance()->msgnode_size_,
PerfMonitor::Instance()->read_count
});
a8::UdpLog::Instance()->Info
("run_times:%d timer_times:%d event_times:%d free_times:%d "
"shutdown_times:%d connect_times:%d close_times:%d "
"send_times:%d recv_times:%d error_times:%d immsg_times:%d",
{
(long long)a8::IoLoop::Instance()->run_times,
(long long)a8::IoLoop::Instance()->timer_times,
(long long)a8::IoLoop::Instance()->event_times,
(long long)a8::IoLoop::Instance()->free_times,
(long long)a8::IoLoop::Instance()->shutdown_times,
(long long)a8::IoLoop::Instance()->connect_times,
(long long)a8::IoLoop::Instance()->close_times,
(long long)a8::IoLoop::Instance()->send_times,
(long long)a8::IoLoop::Instance()->recv_times,
(long long)a8::IoLoop::Instance()->error_times,
(long long)a8::IoLoop::Instance()->immsg_times
});
PerfMonitor::Instance()->max_run_delay_time = 0;
PerfMonitor::Instance()->max_timer_idle = 0;
PerfMonitor::Instance()->max_login_time = 0;
}
void PerfMonitor::Init()
{
{
int perf_log_time = 1000 * 60 * 5;
a8::Timer::Instance()->AddRepeatTimer(perf_log_time,
a8::XParams(),
[] (const a8::XParams& param)
{
SavePerfLog();
});
}
}
void PerfMonitor::UnInit()
{
}

View File

@ -0,0 +1,18 @@
#pragma once
class PerfMonitor : public a8::Singleton<PerfMonitor>
{
private:
PerfMonitor() {};
friend class a8::Singleton<PerfMonitor>;
public:
int max_run_delay_time = 0;
int max_dispatchmsg_time = 0;
long long out_data_size = 0;
long long in_data_size = 0;
long long read_count = 0;
void Init();
void UnInit();
};

View File

@ -1,14 +1,5 @@
#pragma once
struct PerfMonitor
{
int max_run_delay_time = 0;
int max_timer_idle = 0;
long long out_data_size = 0;
long long in_data_size = 0;
long long read_count = 0;
};
struct BaseUserData
{
std::string account_id;