mysql查询花费比预期更长的时间

时间:2018-09-19 05:56:49

标签: mysql

这是我的查询,用于比较两个表并获取另一个表中不存在的人的数据

def article_detail(request, pk):
    article = get_object_or_404(Article, pk=pk)
    # sections and current_section

    if article.block.name == "Action" and request.user.username not in  administors:
        raise  Http404("You are not authorized to read Section Action,I prefert to communicate on the topic action in persion.")

但是,由于我们有大约8000个人数据,因此该查询需要执行近1分钟的时间,对于每一行,它应该与其他表数据进行检查。

我对此有其他选择吗?

编辑: BRvaja的评论有效,创建索引后,它可以快速加载数据

SELECT 
    id,
    registrationnumber,
    CONCAT(name, ' ', surname) name,
    admissionclass,
    section
FROM
    studentsdata
WHERE
    registrationnumber NOT IN (SELECT 
            reg_number
        FROM
            attendance);

2 个答案:

答案 0 :(得分:0)

SELECT 
    id,
    registrationnumber,
    CONCAT(name, ' ', surname) name,
    admissionclass,
    section
FROM
    studentsdata
        LEFT JOIN
    attendance ON studentsdata.registrationnumber = attendance.reg_number
WHERE
    attendance.reg_number IS NULL

答案 1 :(得分:0)

尝试这个

SELECT 
    st.id,
    st.registrationnumber,
    CONCAT(st.name, ' ', st.surname) name,
    st.admissionclass,
    st.section
FROM
    studentsdata as st
        INNER JOIN
    attendance as at ON st.registrationnumber != at.reg_number