2016-03-20 5 views
4

Ich habe folgende MySQL-Abfrage:MySQL: ORDER BY-Klausel verlangsamt MATCH AGAINST Suche nach unten

$sql = "SELECT (SELECT COUNT(share_id) FROM pd_shares WHERE section = 'news' AND item_id = news.article_id) AS count_shares, article_id, link, title, publish_date, description, source FROM pd_news AS news WHERE (MATCH (title_ascii, description_ascii) AGAINST ('".match_against($_GET["sn"])."' IN BOOLEAN MODE)) AND article_id > " . $last_index . " ORDER BY article_id ASC LIMIT 0,$limit"; 

Wenn ich eine Suche, die Abfrage Lasten 513,24 ms mit der ORDER BY-Klausel. Wenn ich es entferne, läuft es 77,12 ms.

Sowohl title_ascii als auch description_ascii sind FULLTEXT.

Wie kann ich diese Abfrage neu schreiben, so dass die Laufgeschwindigkeit viel schneller lädt, als es derzeit ist?

Ausgabe von EXPLAIN:

id select_type  table type possible_keys key  key_len  ref  rows Extra 
1 PRIMARY  news fulltext PRIMARY,news_search  news_search  0 NULL 1 Using where; Using filesort 
2 DEPENDENT SUBQUERY pd_shares ref  section  section  19 const,my_database.news.article_id 2 Using index condition 
+2

Können Sie Ihre EXPLAIN-Ausgabe posten? –

+0

EXPLAIN Posted. –

+0

Ihre EXPLAIN-Ausgabe sagte uns, dass die Abfrage ziemlich schnell sein sollte, da sie nur wenige Zeilen verarbeitet. Rufen Sie EXPLAIN für den aktuellen Datensatz auf? –

Antwort

0

Es wäre hilfreich, um das Schema für die referenzierten Tabellen (pd_shares und pd_news) zu kennen. Ich habe die Unterauswahl in einen normalen Join verschoben und eine group by-Klausel hinzugefügt:

$sql = "SELECT 
      article_id 
     , link 
     , title 
     , publish_date 
     , description 
     , source 
     , COUNT(shares.share_id) AS count_shares 
     FROM pd_news AS news 
     LEFT JOIN pd_shares shares 
      ON shares.section = 'news' AND shares.item_id = news.article_id 
     WHERE (MATCH (title_ascii, description_ascii) AGAINST ('".match_against($_GET["sn"])."' IN BOOLEAN MODE)) 
      AND article_id > " . $last_index . " 
     GROUP BY article_id 
     ORDER BY article_id ASC LIMIT 0, $limit"; 
Verwandte Themen