mysql - 多个表 JOIN 的 SQL SELECT DISTINCT 性能

标签 mysql performance indexing distinct inner-join

我正在建立一个包含大学类(class)信息的数据库。 每个类(class)都可以关联

  • 一位或多位作者
  • 一个或多个学科
  • 一个或多个机构
  • 一个或多个级别

我的数据库包含以下表格:

  • 类(class)(cou_id、cou_name、cou_number、cou_year、cou_term)
  • 作者(aut_id、aut_last)
  • 纪律(dis_id、dis_name)
  • 机构(ins_id、ins_name、ins_classification)
  • 级别(lev_id、lev_name)
  • 作者类(class)(链接表)
  • 类(class)学科(链接表)
  • 类(class)机构(链接表)
  • 类(class)级别(链接表)

为了从数据库中检索所有类(class)(以及相应的作者、学科、机构和级别信息),我使用以下查询:

SELECT DISTINCT aut_last, c.cou_id, cou_name, cou_number, cou_year, cou_term, dis_name, ins_name, ins_classification, lev_name
    FROM authorcourse ac1
    INNER JOIN authorcourse ac2        
    ON ac1.cou_id = ac2.cou_id        
    INNER JOIN author a
    ON ac2.aut_id=a.aut_id
    INNER JOIN course c
    ON ac2.cou_id = c.cou_id
    INNER JOIN coursediscipline cd1
    ON ac2.cou_id = cd1.cou_id
    INNER JOIN coursediscipline cd2
    ON cd1.cou_id = cd2.cou_id
    INNER JOIN discipline d
    ON cd2.dis_id = d.dis_id
    INNER JOIN courseinstitution ci1
    ON ac2.cou_id = ci1.cou_id
    INNER JOIN courseinstitution ci2
    ON ci1.cou_id = ci2.cou_id
    INNER JOIN institution i
    ON ci2.ins_id = i.ins_id
    INNER JOIN courselevel cl1
    ON ac2.cou_id = cl1.cou_id
    INNER JOIN courselevel cl2
    ON cl1.cou_id = cl2.cou_id
    INNER JOIN level l
    ON cl2.lev_id = l.lev_id

当数据库中有 15 门具有“简单”关系的类(class)时,此查询效果很好。例如:

 cou_name = 'course1', cou_number = 'C1', cou_year = '1999', cou_term = 'summer'
 aut_last = 'Doe1'
 dis_name = 'discipline1'
 ins_name = 'institution1', ins_classification = 'classification1'
 lev_name = 'level1'

-->显示第 0 - 14 行(总共 15 行,查询耗时 0.0118 秒) EXPLAIN 生成下表:

id select_type table type   possible_keys          key     key_len ref             rows Extra
1  SIMPLE      ac1   index  cou_id                 aut_id  2       NULL            15   Using index; Using  temporary
1  SIMPLE      ac2   ref    PRIMARY,aut_id,cou_id  cou_id  2       ccdb.ac1.cou_id 1    Using index
1  SIMPLE      a     eq_ref PRIMARY                PRIMARY 2       ccdb.ac2.aut_id 1    
1  SIMPLE      c     eq_ref PRIMARY                PRIMARY 2       ccdb.ac2.cou_id 1    Using where
1  SIMPLE      cd1   ref    PRIMARY,cou_id         PRIMARY 2       ccdb.ac1.cou_id 1    Using index
1  SIMPLE      cd2   ref    PRIMARY,cou_id,dis_id  PRIMARY 2       ccdb.ac2.cou_id 1    Using where; Using index
1  SIMPLE      d     eq_ref PRIMARY                PRIMARY 2       ccdb.cd2.dis_id 1    
1  SIMPLE      ci1   ref    PRIMARY,cou_id         PRIMARY 2       ccdb.ac2.cou_id 1    Using where; Using index
1  SIMPLE      ci2   ref    PRIMARY,cou_id,ins_id  PRIMARY 2       ccdb.ac2.cou_id 1    Using where; Using index
1  SIMPLE      i     eq_ref PRIMARY                PRIMARY 2       ccdb.ci2.ins_id 1    
1  SIMPLE      cl1   ref    PRIMARY,cou_id         PRIMARY 2       ccdb.cd1.cou_id 1    Using where; Using index
1  SIMPLE      cl2   ref    PRIMARY,cou_id,lev_id  PRIMARY 2       ccdb.cl1.cou_id 1    Using where; Using index
1  SIMPLE      l     eq_ref PRIMARY                PRIMARY 2       ccdb.cl2.lev_id 1    

问题:当有 15 个类(class)具有多种关系时,性能会急剧下降。示例类(class):

cou_name = 'course1', cou_number = 'C1', cou_year = '1999', cou_term = 'summer'
aut_last = 'Doe1', 'Doe', 'Doe3', 'Doe4'
dis_name = 'discipline1', 'discipline2', 'discipline3', 'discipline4'
ins_name = 'institution1'(ins_classification = 'classification1'),     'institution2'(ins_classification = 'classification2'), 'institution3'(ins_classification =  'classification3'), 'institution4' (ins_classification = 'classification4')
lev_name = 'level1', 'level2', 'level3', 'level4'

-->显示第 0 - 29 行(总共 3,840 行,查询耗时 14.7039 秒) EXPLAIN 生成下表:

 id select_type table type   possible_keys         key     key_len ref             rows Extra
 1  SIMPLE      c     ALL    PRIMARY               NULL    NULL    NULL            15   Using temporary
 1  SIMPLE      ac1   ref    PRIMARY,aut_id,cou_id cou_id  2       ccdb.c.cou_id   2    Using index
 1  SIMPLE      a     eq_ref PRIMARY               PRIMARY 2       ccdb.ac1.aut_id 1    
 1  SIMPLE      ac2   ref    cou_id                cou_id  2       ccdb.c.cou_id   2    Using index
 1  SIMPLE      cd1   ref    PRIMARY,cou_id        cou_id  2       ccdb.ac1.cou_id 2    Using where; Using index
 1  SIMPLE      cd2   ref    PRIMARY,cou_id,dis_id cou_id  2       ccdb.c.cou_id   2    Using index
 1  SIMPLE      d     eq_ref PRIMARY               PRIMARY 2       ccdb.cd2.dis_id 1    
 1  SIMPLE      ci1   ref    PRIMARY,cou_id        cou_id  2       ccdb.ac1.cou_id 2    Using where; Using index
 1  SIMPLE      ci2   ref    PRIMARY,cou_id,ins_id cou_id  2       ccdb.ac2.cou_id 2    Using where; Using index
 1  SIMPLE      i     eq_ref PRIMARY               PRIMARY 2       ccdb.ci2.ins_id 1    
 1  SIMPLE      cl1   ref    PRIMARY,cou_id        cou_id  2       ccdb.c.cou_id   2    Using index
 1  SIMPLE      cl2   ref    PRIMARY,cou_id,lev_id cou_id  2       ccdb.ci2.cou_id 2    Using where; Using index
 1  SIMPLE      l     eq_ref PRIMARY               PRIMARY 2       ccdb.cl2.lev_id 1    

当运行我的 PHP 网站时,我收到以下错误“ fatal error :...超出了最大执行时间 30 秒”

问题:如何加快此查询速度? 我尝试了几种不同的连接组合 (正如您在解释结果中看到的)我对我认为可能相关的所有列建立了索引。

任何帮助将不胜感激。

最佳答案

在我看来,好像您为“类(class) View ”类型详细信息页面提取了所有这些数据?

如果是这样,我会说,一旦数据库中开设了一门类(class),作者、学科、机构和级别的数量多久会改变一次?

如果从设置的时间起永远不会改变,那么当它设置时,也将其设置在完全非规范化的表中,如下所示:

courseView(cou_id、cou_name、cou_number、cou_year、cou_term、数据)

.. 在“数据”中,您只需放入所有数据的序列化数组。丑陋,但它会很快。

然后,当你通过类(class)id搜索拉取类(class)时,你可以只搜索一行,一个索引,并立即拉取所有数据。

..

此外,如果您要让人们按作者进行搜索,那么您仍然可以像平常一样使用简单的查询来使用规范化表来执行此操作。

关于mysql - 多个表 JOIN 的 SQL SELECT DISTINCT 性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14590139/

相关文章:

java - 使用依赖注入(inject)时会对运行时性能产生影响吗?

c# - Com 调用 64 位服务器上的 32 位应用程序很慢

PHP 搜索与 mySQL 程序错误

mysql - 使用触发器获取mysql中插入行的值

performance - CouchDB:性能动态和静态内容

object - 按对象 ID 从 SQL Server 删除索引

mysql - 加快 MySQL 中的行计数

MYSQL:数据变大后查询速度变慢

MySQL MATCH() AGAINST() 与相反的参数

java - 组合框选择和模型绑定(bind)