Skip to content

Commit

Permalink
fix(backend): sql查询性能优化 #7666
Browse files Browse the repository at this point in the history
# Reviewed, transaction id: 22552
  • Loading branch information
WytheLi authored and zhangzhw8 committed Nov 1, 2024
1 parent 7d5e6bf commit dd6def8
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 deletions.
5 changes: 4 additions & 1 deletion dbm-ui/backend/db_meta/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ class DBMeta(AppConfig):
name = "backend.db_meta"

def ready(self):
from backend.db_meta.models import ProxyInstance, StorageInstance
from backend.db_meta.models import AppCache, ProxyInstance, StorageInstance
from backend.db_meta.signals import update_cluster_status
from backend.db_meta.utils import cache_appcache_data

post_save.connect(cache_appcache_data, sender=AppCache)

post_migrate.connect(init_db_meta, sender=self)
# 当实例进行修改或者删除时,更新集群状态
Expand Down
4 changes: 3 additions & 1 deletion dbm-ui/backend/db_meta/models/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"""
import logging

from django.core.cache import cache
from django.db import models
from django.utils.translation import ugettext_lazy as _

Expand Down Expand Up @@ -94,7 +95,8 @@ def batch_get_app_attr(cls, bk_biz_ids, attr_name="db_app_abbr"):
@classmethod
def get_choices(cls):
try:
biz_choices = [(biz.bk_biz_id, f"[{biz.bk_biz_id}]{biz.bk_biz_name}") for biz in cls.objects.all()]
appcache_data = cache.get("appcache_list")
biz_choices = [(app["bk_biz_id"], f"[{app['bk_biz_id']}]{app['bk_biz_name']}") for app in appcache_data]
except Exception: # pylint: disable=broad-except
# 忽略出现的异常,此时可能因为表未初始化
biz_choices = []
Expand Down
8 changes: 6 additions & 2 deletions dbm-ui/backend/db_meta/models/db_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"""
import logging

from django.core.cache import cache
from django.db import models
from django.db.models import Q
from django.utils.translation import ugettext_lazy as _
Expand Down Expand Up @@ -70,11 +71,14 @@ def get_choices_with_filter(cls, cluster_type=None):
for dm in cls.objects.filter(q).all():

try:
app = AppCache.objects.get(bk_biz_id=dm.bk_biz_id)
appcache_dict = cache.get("appcache_dict")
appcache = appcache_dict.get(str(dm.bk_biz_id))
db_module_choices.append(
(
dm.db_module_id,
f"[{dm.db_module_id}]-[{dm.cluster_type}]-[app:{app.db_app_abbr}]-{dm.db_module_name}",
f"[{dm.db_module_id}]-"
f"[{dm.cluster_type}]-[app:{appcache['db_app_abbr']}]-"
f"{dm.db_module_name}",
)
)
except AppCache.DoesNotExist:
Expand Down
10 changes: 10 additions & 0 deletions dbm-ui/backend/db_meta/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
from collections import defaultdict

from django.conf import settings
from django.core.cache import cache
from django.utils.translation import ugettext as _

from backend import env
from backend.components import CCApi, JobApi
from backend.configuration.constants import DBType
from backend.db_meta.enums import ClusterPhase, ClusterType
from backend.db_meta.models import (
AppCache,
Cluster,
ClusterEntry,
Machine,
Expand Down Expand Up @@ -201,3 +203,11 @@ def clean_cc_topo(bk_biz_id=env.DBA_APP_BK_BIZ_ID):
"bk_module_id": bk_module["bk_module_id"],
}
)


def cache_appcache_data(sender, **kwargs):
data = AppCache.objects.all().values()
appcache_list = list(data) if data else []
appcache_dict = {app["bk_biz_id"]: app for app in data}
cache.set("appcache_list", appcache_list)
cache.set("appcache_dict", appcache_dict)
7 changes: 5 additions & 2 deletions dbm-ui/backend/db_monitor/views/notice_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,11 @@ def get_action_permission_map(self):

def get_serializer_context(self):
context = super().get_serializer_context()
notify_groups = MonitorPolicy.objects.exclude(notify_groups=[]).values_list("notify_groups", flat=True)
context["group_used"] = dict(Counter([item for group in notify_groups for item in group]))
context["group_used"] = {}
if "X-Requested-With" in self.request.META:
# 仅在实际API调用时执行查库逻辑。/swagger/文档API忽略查库
notify_groups = MonitorPolicy.objects.exclude(notify_groups=[]).values_list("notify_groups", flat=True)
context["group_used"] = dict(Counter([item for group in notify_groups for item in group]))
return context

def get_queryset(self):
Expand Down

0 comments on commit dd6def8

Please sign in to comment.