レポートの大規模なデータベース (300K 行、1.6 GB) を備えた Web サイトがあります。また、検索エンジンは結果を表示するのに非常に時間がかかります。
検索は 3 つのテーブルを介して行われます。
- メイン テーブルには、レポートのタイトル、説明、目次があります。
- 2 番目の表には、各レポートのビジネス セクター (自動車、鉱業、ヘルスケアなど) があります。
- 3 番目の表には、各レポートの国コードがあります。
次のようになります。
以前は約 50 秒でしたが、検索パフォーマンスを向上させるためにデータベースを 2 つに分割しましたが、現在は約 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; ?>§or=<? echo $id_cat_s; ?>&cr=<? echo $cr; ?><? echo $cr_parent; ?>&cr_parent=§or_parent=&org_id=<? echo $org_id; ?>&keyword=<? echo $keyword; ?>" style="color:#000000"><? echo $name_cat_s;
if ($sector=='' AND $cr_parent=='') { print (" ($ile_s)"); } else if ($cr_parent!='') { print (" "); } else { print (" ($liczba_rekordow)"); }
?></a> </div>
<? } ?>
</div>
<? // } ?>
<? //if ($sector_parent=='') { ?>
<div style="margin-bottom:5px;margin-top:20px;"><strong>| 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; ?>§or=<? echo $sector; ?>&cr=&cr_parent=§or_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;
}