由于JOIN,Mysql ORDER BY主键减慢

时间:2013-07-15 13:24:43

标签: sql join view

我在级联患者中有3个表(250 000个回忆者) - 患者_属性(250 000个回忆者) - 检查(1 000 000个回忆者) 患者:patient_attributes为1:N patient_attributes:考试是1:N

query1非常快(0.000秒):

SELECT examination.ID AS Eid 
FROM examination  
ORDER BY eid LIMIT 10000,50

query2仍然很快(0.015秒):

SELECT examination.ID AS Eid 
FROM examination
JOIN `patient_attributes` 
  ON `examination`.`PATIENT_ATTRIBUTES_ID` = `patient_attributes`.`ID`
ORDER BY eid LIMIT 10000,50

query3非常慢(5.5秒):

SELECT examination.ID AS Eid 
FROM `examination`         
JOIN `patient_attributes` 
  ON `examination`.`PATIENT_ATTRIBUTES_ID` = `patient_attributes`.`ID`      
JOIN `patient` ON  `patient_attributes`.`PATIENT_ID` = `patient`.`ID`
ORDER BY eid LIMIT 10000,50

为简单起见,在这种情况下,连接是无用的。后来我想从这个查询创建视图,除了DB之外,我会忽略视图中未使用的列并省略未使用的连接,因为这些连接是通过外键连接的,并且总共有一条记录。

我应该在db结构中设置一些东西来加速查询3,省略查询中无用的连接吗?

examination.ID is primary key, 
examination.PATIENT_ATTRIBUTES_ID is foreign key, 
patient_attributes.ID is primary key, 
patient_attributes.PATIENT_ID is foreign key, 
patient.ID is primary key

show index from patient

Table   Non_uniq Key_name    Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type
patient 0        PRIMARY     1            ID                 A    230077    NULL    NULL         BTREE  
patient 1        RefMEMO1001 1            ANAMNESIS_MEMO_ID  A    80        NULL    NULL    YES  BTREE  
patient 1        REF17       1            RACE_ID            A    4         NULL    NULL    YES  BTREE  
patient 1        REF5        1            CARD_INDEX_ID      A    6         NULL    NULL         BTREE

show index from patient_attributes

Table         Non_unique Key_name Seq_in_idx Col.name Collation Cardinality Sub_part Packed Null Index_type
patient_attributes   0   PRIMARY         1   ID               A    1857048     NULL    NULL       BTREE  
patient_attributes   1   RefPATIENT1021  1   PATIENT_ID       A    619016      NULL    NULL       BTREE  
patient_attributes   1   RefMEMO1081     1   SYMPTOMS_MEMO_ID A    2           NULL    NULL  YES  BTREE

show index from examination

Table   Non_unique Key_name      Seq_in_index  Column_name   Collation Cardinality Sub_part Packed Null Index_type
examination   0    PRIMARY                   1 ID                    A 905353      NULL      NULL       BTREE  
examination   0    I_EXAMINATION_GUID        1 UNIQUE_GUID           A 905353      NULL      NULL       BTREE  
examination   0    I_PATIENT_GUID            1 UNIQUE_GUID           A 905353      NULL      NULL       BTREE  
examination   1    RefSTATION621             1 STATION_ID            A 2           NULL      NULL  YES  BTREE  
examination   1    I_EXAMINATION_STATUS      1 STATUS                A 6           NULL      NULL       BTREE  
examination   1    I_EXAMINATION_CREATED     1 CREATED               A 181070      NULL      NULL       BTREE  
examination   1    I_EXAMINATION_STARTED     1 STARTED               A 181070      NULL      NULL       BTREE  
examination   1    RefPATIENT_ATTRIBUTES1031 1 PATIENT_ATTRIBUTES_ID A 905353      NULL      NULL       BTREE  

0 个答案:

没有答案