使用JOIN与多种方法的特定SQL查询

时间:2015-12-14 10:06:44

标签: mysql node.js

我有一个NodeJS应用程序和一个mysql数据库。我使用mysql npm package来访问它。

我的SQL模式中经常有这样的表之间的关系:

<link rel="stylesheet" href="{% static 'bootstrap/css/bootstrap.css' %}">
    <link rel="stylesheet" href="{% static 'css/main.css' %}">

<div class="container-fluid">

    <div class="page-header"><h2>Hero Configurer</h2></div>

    <div class="row">
        <div class="col-md-6 col-sm-offset-3">
            <form method="post" action="" enctype="multipart/form-data">
                {% csrf_token %}  
                {{ form.as_p }}
            </form>
        </div>
    </div>
</div>

现在,在我的用户模型中,我希望有一个函数可以为用户,他们的组和用户的组属性提供。

现在,我使用一些JOINS做一个大的自定义SQL请求。它有效,但它带来了许多“自定义”函数,如 getUsersWithGroupAndConstraints 。 由于我在代码的许多不同位置都有这种设计,因此会导致很难维护。

我希望我的代码更通用,并且有一个User / Group / Constraint模型,然后我会以这种方式查询:

Table User :
SERIAL id
...

Table Group
SERIAL id
...

Table Group_Constraints
SERIAL ConstraintId
...

Table UserToGroup
BIGINT UserId
BIGINT GroupId

Table GroupToConstraint
BIGINT GroupId
BIGINT ConstraintId

但它会从1个SQL查询引导到User.length * Group.length SQL查询。

我找不到实现 clean 设计的方法,没有大量的SQL查询,因此,我猜测,性能非常差。

我该怎么做?

0 个答案:

没有答案
相关问题