添加“where”子句会产生超时

时间:2016-04-27 07:54:10

标签: mysql select

我正在尝试在我的架构中执行选择查询,但是当我添加“where”子句时,性能会下降,最终会出现超时错误。

以下查询显然有效(尽管速度很慢)

select
    count(*),
    avg(ifac),max(ifac),min(ifac),std(ifac),
    year(pubmed_article.publication) as pubYear
from test.pubmedjournal,test.pubmed_article,test.impactfactor
where (pubmed_article.title like '%parameter%' or pubmed_article.abstract like '%parameter%') 
    and pubmed_article.pubmedjournal_idpubmedjournal = pubmedjournal.idpubmedjournal 
    and impactfactor.pubmedjournal_idpubmedjournal=pubmedjournal.idpubmedjournal
    and impactfactor.date = year(pubmed_article.publication)
group by pubYear;

尽管如此,如果我添加一个额外的where子句和impactfactor.ifac> 1.0 ,查询将停止工作

select
    count(*),
    avg(ifac),max(ifac),min(ifac),std(ifac),
    year(pubmed_article.publication) as pubYear
from test.pubmedjournal,test.pubmed_article,test.impactfactor
where (pubmed_article.title like '%parameter%' or pubmed_article.abstract like '%parameter%') 
    and pubmed_article.pubmedjournal_idpubmedjournal = pubmedjournal.idpubmedjournal 
    and impactfactor.pubmedjournal_idpubmedjournal=pubmedjournal.idpubmedjournal
    and impactfactor.date = year(pubmed_article.publication)
    and impactfactor.ifac >1.0
group by pubYear;

在stackoverflow中搜索了一下后,我发现可能我可以使用索引来解决这个问题,但我不确定。关于为什么这不起作用的任何线索?

这些是我的表结构(以防有用):

+-------------------------------+----------------+------+-----+---------+----------------+
| Field                         | Type           | Null | Key | Default | Extra          |
+-------------------------------+----------------+------+-----+---------+----------------+
| idpubmed_article              | int(11)        | NO   | PRI | NULL    | auto_increment |
| title                         | varchar(1000)  | YES  |     | NULL    |                |
| abstract                      | varchar(20000) | YES  |     | NULL    |                |
| publication                   | date           | YES  |     | NULL    |                |
| pubmedjournal_idpubmedjournal | int(11)        | NO   | MUL | NULL    |                |
+-------------------------------+----------------+------+-----+---------+----------------+
5 rows in set (0.01 sec)

mysql> DESCRIBE test.pubmedjournal;
+-------------------+--------------+------+-----+---------+----------------+
| Field             | Type         | Null | Key | Default | Extra          |
+-------------------+--------------+------+-----+---------+----------------+
| idpubmedjournal   | int(11)      | NO   | PRI | NULL    | auto_increment |
| title             | varchar(400) | YES  |     | NULL    |                |
| abbreviated_title | varchar(400) | YES  |     | NULL    |                |
+-------------------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

mysql> DESCRIBE test.impactfactor;
+-------------------------------+---------------+------+-----+---------+----------------+
| Field                         | Type          | Null | Key | Default | Extra          |
+-------------------------------+---------------+------+-----+---------+----------------+
| idimpactfactor                | int(11)       | NO   | PRI | NULL    | auto_increment |
| date                          | varchar(45)   | YES  |     | NULL    |                |
| ifac                          | decimal(10,2) | YES  | MUL | NULL    |                |
| pubmedjournal_idpubmedjournal | int(11)       | NO   | MUL | NULL    |                |
+-------------------------------+---------------+------+-----+---------+----------------+

0 个答案:

没有答案