-2

我有一个包含大型报告数据库(300K 行,1.6 GB)的网站。搜索引擎需要很长时间才能显示结果。

通过 3 个表进行搜索:

  • 主表有报告的标题、描述和目录。
  • 第二个表有每个报告的业务部门(汽车、采矿、医疗保健等)
  • 第三个表格有每个报告的国家代码。

它是这样出现的:

在此处输入图像描述

之前大约需要 50 秒,现在我将数据库一分为二以获得更快的搜索性能,现在大约需要 30 秒,这仍然很多。

我认为问题应该出在其中一个查询中,但我无法识别错误在哪里。

你能帮我看看如何优化查询吗?这真的很重要。我尝试了不同的解决方案,但都没有奏效。

主表结构:

Field            Type         Null Key Default Extra
id               int(12)      NO   PRI NULL    auto_increment
name             varchar(220) NO   MUL NULL  
data             date         NO       NULL  
publication_date date         NO   MUL NULL  
no_pages         int(12)      NO   MUL NULL  
description      text         NO       NULL  
table_content    text         NO       NULL  
type             varchar(120) NO   MUL NULL  
price            float        NO   MUL NULL  
currency         varchar(12)  NO   MUL NULL  
status           int(12)      NO   MUL NULL  
organizer_id     int(12)      NO   MUL NULL  
pic              varchar(80)  NO       NULL  
main_sec         varchar(80)  NO       NULL  
main_reg         varchar(80)  NO       NULL

索引

Table   Non_unique Key_name         Seq_in_index Column_name      Collation Cardinality Sub_part    Packed  Null    Index_type  Comment
reports 0          PRIMARY          1            id               A         129700      NULL        NULL            BTREE    
reports 1          publication_date 1            publication_date A         712         NULL        NULL            BTREE    
reports 1          price            1            price            A         584         NULL        NULL            BTREE    
reports 1          name_2           1            name             A         129700      NULL        NULL            BTREE    
reports 1          organizer_id     1            organizer_id     A         48          NULL        NULL            BTREE    
reports 1          type             1            type             A         162         NULL        NULL            BTREE    
reports 1          status           1            status           A         4           NULL        NULL            BTREE    
reports 1          currency         1            currency         A         4           NULL        NULL            BTREE    
reports 1          no_pages         1            no_pages         A         831         NULL        NULL            BTREE    
reports 1          name             1            name             NULL      1           NULL        NULL            FULLTEXT     

代码

<?
if (($cr=='' AND $cr_parent=='') OR ($cr!='' AND $sector!='')) {
$zapytanie_s = "SELECT count(DISTINCT u.id) AS ile_s, w.name_cat AS name_cat_s, t.id_cat AS id_cat_s FROM reports as u, sector_index_reports as t, sectors as w WHERE u.id = t.id_obiekt AND w.id = t.id_cat AND u.status!='0' $q $warunek2 $warunek3 $warunek4 $warunek5 $warunek6 $warunek7 GROUP BY w.name_cat ORDER BY ile_s DESC";
} else if ($cr!='' AND $sector_parent!='') {
$zapytanie_s = "SELECT count(DISTINCT u.id) AS ile_s, w.name_cat AS name_cat_s, t.id_cat AS id_cat_s FROM reports as u, sector_index_reports as t, sectors as w WHERE u.id = t.id_obiekt AND w.id = t.id_cat AND u.status!='0' $q $warunek2 $warunek3 $warunek4 $warunek5 $warunek6 $warunek7 GROUP BY w.name_cat ORDER BY ile_s DESC"; 
//echo $zapytanie_s;
} else {
$zapytanie_s = "SELECT count(DISTINCT u.id) AS ile_s, w.name_cat AS name_cat_s, u.main_sec AS id_cat_s FROM reports as u, country_index_reports as t, sectors as w WHERE u.id = t.id_obiekt AND w.id = u.main_sec AND u.status!='0' $q $warunek2 $warunek3 $warunek4 $warunek5 $warunek6 $warunek7 GROUP BY w.name_cat ORDER BY ile_s DESC";
}
$wynik_s = mysql_query($zapytanie_s);
//echo $zapytanie_s;
WHILE ($row_s = @mysql_fetch_array($wynik_s)) {
extract($row_s);  ?>
<div style="margin-top:5px; margin-bottom:10px; margin-left:25px;"><a href="./results.php?s=<? echo $s; ?>&ls=<? echo $ls; ?>&sortuj=<? echo $sortuj; ?>&typ=<? echo $typ; ?>&publish_date=<? echo $publish_date; ?>&sector=<? echo $id_cat_s; ?>&cr=<? echo $cr; ?><? echo $cr_parent; ?>&cr_parent=&sector_parent=&org_id=<? echo $org_id; ?>&keyword=<? echo $keyword; ?>" style="color:#000000"><? echo $name_cat_s; 
if ($sector=='' AND $cr_parent=='') { print ("&nbsp;($ile_s)"); } else if ($cr_parent!='') { print ("&nbsp;"); }  else { print ("&nbsp;($liczba_rekordow)"); } 
?></a> </div> 
<? } ?>
</div>
<? // } ?>
<? //if ($sector_parent=='') { ?>
<div style="margin-bottom:5px;margin-top:20px;"><strong>|&nbsp;Countries/Regions: </strong> 
<? if ($cr!='') { ?>
<span class="dopaginacji" style="float:right; margin-top:-8px;"><a href="./results.php?s=<? echo $s; ?>&ls=<? echo $ls; ?>&sortuj=<? echo $sortuj; ?>&typ=<? echo $typ; ?>&publish_date=<? echo $publish_date; ?>&sector=<? echo $sector; ?>&cr=&cr_parent=&sector_parent=<? echo $sector_parent; ?>&org_id=<? echo $org_id; ?>&keyword=<? echo $keyword; ?>" style="color:#000000"><b>x</b> clear</span></a>
<? } ?>
</div>
<div style="width:100%; max-height:300px; overflow:auto; ">
<?
if ($sector=='' AND $sector_parent=='') {
$zapytanie_s = "SELECT count(DISTINCT u.id) AS ile_s, w.name_cat AS name_cat_s, t.id_cat AS id_cat_s FROM reports as u, country_index_reports as t, countries as w WHERE u.id = t.id_obiekt AND w.id = t.id_cat AND u.status!='0' $q $warunek2 $warunek3 $warunek4 $warunek5 $warunek6 $warunek7 GROUP BY w.name_cat ORDER BY ile_s DESC";
} else if ($cr!='' AND $sector!='') {
$zapytanie_s = "SELECT count(DISTINCT u.id) AS ile_s, nn.name_cat AS name_cat_s, u.main_reg AS id_cat_s FROM reports as u, sector_index_reports as t, countries as nn WHERE u.id = t.id_obiekt AND nn.id = u.main_reg AND u.status!='0' $q $warunek2 $warunek3 $warunek4 $warunek5 $warunek6 $warunek7 GROUP BY nn.name_cat ORDER BY ile_s DESC";
} else {
$zapytanie_s = "SELECT count(DISTINCT u.id) AS ile_s, w.name_cat AS name_cat_s, u.main_reg AS id_cat_s FROM reports as u, sector_index_reports as t, countries as w WHERE u.id = t.id_obiekt AND w.id = u.main_reg AND u.status!='0' $q $warunek2 $warunek3 $warunek4 $warunek5 $warunek6 $warunek7 GROUP BY w.name_cat ORDER BY ile_s DESC";
//echo $zapytanie_s;
}
4

1 回答 1

0

我建议您获取所有可疑查询,然后explain select在 MySQL Workbench 之类的数据库管理工作室中进行操作。

这样,您将准确地看到每个查询在做什么,并且您将能够相应地调整它。

您可能可以通过对 where 子句中的项目使用索引来提高性能(当然不是全部,因为这会占用太多空间)。也不是做一个

from table1, table2

试一试join。如果您在数据库中执行查询时提供查询(没有 php 变量),这将非常有帮助,因为这可能是瓶颈。

于 2013-09-10T14:35:05.017 回答