Skip to content

Commit

Permalink
feat: MySQL bool 条件查询优化 (closed TencentBlueKing#2151)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhuoZhuoCrayon committed Mar 27, 2024
1 parent 213ca9c commit b0dfdf8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
4 changes: 2 additions & 2 deletions apps/backend/subscription/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from django.conf import settings
from django.core.cache import cache
from django.db.models import Max, Q, QuerySet
from django.db.models import Max, Q, QuerySet, Value
from django.utils.translation import get_language
from django.utils.translation import ugettext as _

Expand Down Expand Up @@ -579,7 +579,7 @@ def instance_status(subscription_id_list: List[int], show_task_detail: bool) ->
subscription_instance_record: Dict[int, Dict[str, models.SubscriptionInstanceRecord]] = defaultdict(dict)
instance_records = []
for instance_record in models.SubscriptionInstanceRecord.objects.filter(
subscription_id__in=subscription_id_list, is_latest=True
subscription_id__in=subscription_id_list, is_latest=Value(1)
):
subscription_instance_record[instance_record.subscription_id][instance_record.instance_id] = instance_record
instance_records.append(instance_record)
Expand Down
12 changes: 9 additions & 3 deletions apps/backend/subscription/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from django.core.cache import caches
from django.db import transaction
from django.db.models import Q
from django.db.models import Q, Value
from django.utils.translation import get_language
from django.utils.translation import gettext_lazy as _
from drf_yasg.utils import swagger_auto_schema
Expand Down Expand Up @@ -779,8 +779,14 @@ def query_host_policy(self, request):
host = models.Host.objects.get(bk_host_id=bk_host_id)

subscription_records = models.SubscriptionInstanceRecord.objects.filter(
instance_id__endswith="host|{}".format(host.bk_host_id),
is_latest=True,
instance_id=tools.create_node_id(
{
"node_type": models.Subscription.NodeType.INSTANCE,
"object_type": models.Subscription.ObjectType.HOST,
"bk_host_id": host.bk_host_id,
}
),
is_latest=Value(1),
).values("subscription_id", "update_time", "instance_id", "task_id", "status")

sub_id__sub_record_map = {
Expand Down

0 comments on commit b0dfdf8

Please sign in to comment.