Сокращение mysql запросов для PHP-Fusion

Сегодня пойдет речь о том как сократить количество mysql запросов в новостях php-fusion. Как многие знают запросов во фьюжине очень много. Тем самым, мы уменьшим их количество на -1 на одну новость..

Файл news.php

Строка 39-48

$result = dbquery(
    "SELECT tn.*, tc.*, tu.user_id, tu.user_name, tu.user_status
    FROM ".DB_NEWS." tn
    LEFT JOIN ".DB_USERS." tu ON tn.news_name=tu.user_id
    LEFT JOIN ".DB_NEWS_CATS." tc ON tn.news_cat=tc.news_cat_id
    WHERE ".groupaccess('news_visibility')." AND (news_start='0'||news_start<=".time().")
        AND (news_end='0'||news_end>=".time().") AND news_draft='0'
    GROUP BY news_id
    ORDER BY news_sticky DESC, news_datestamp DESC LIMIT ".$_GET['rowstart'].",".$items_per_page
);


Заменить на

$result = dbquery(
    "SELECT tn.*, tc.*, COUNT(cc.comment_id) AS com_num, tu.user_id, tu.user_name, tu.user_status
    FROM ".DB_NEWS." tn
    LEFT JOIN ".DB_USERS." tu ON tn.news_name=tu.user_id
    LEFT JOIN ".DB_NEWS_CATS." tc ON tn.news_cat=tc.news_cat_id
    LEFT JOIN ".DB_COMMENTS." cc ON tn.news_id=cc.comment_item_id AND comment_type='N'
    WHERE ".groupaccess('news_visibility')." AND (news_start='0'||news_start<=".time().")
        AND (news_end='0'||news_end>=".time().") AND news_draft='0'
    GROUP BY news_id
    ORDER BY news_sticky DESC, news_datestamp DESC LIMIT ".$_GET['rowstart'].",".$items_per_page
);


Закоментировать строчку 52

//$comments = dbcount("(comment_id)", DB_COMMENTS." WHERE comment_type='N' AND comment_hidden='0' AND comment_item_id='".$data['news_id']."'");


Строку 77

"news_comments" => $comments,


Заменить на

"news_comments" => $data['com_num'],


Строки 94-100

$result = dbquery(
    "SELECT tn.*, tc.*, tu.user_id, tu.user_name, tu.user_status FROM ".DB_NEWS." tn
    LEFT JOIN ".DB_USERS." tu ON tn.news_name=tu.user_id
    LEFT JOIN ".DB_NEWS_CATS." tc ON tn.news_cat=tc.news_cat_id
    WHERE ".groupaccess('news_visibility')." AND news_id='".$_GET['readmore']."' AND news_draft='0'
    LIMIT 1"
);


Заменить на

$result = dbquery(
    "SELECT tn.*, tc.*, COUNT(cc.comment_id) AS com_num, tu.user_id, tu.user_name, tu.user_status FROM ".DB_NEWS." tn
    LEFT JOIN ".DB_USERS." tu ON tn.news_name=tu.user_id
    LEFT JOIN ".DB_NEWS_CATS." tc ON tn.news_cat=tc.news_cat_id
    LEFT JOIN ".DB_COMMENTS." cc ON tn.news_id=cc.comment_item_id AND comment_type='N'
    WHERE ".groupaccess('news_visibility')." AND news_id='".$_GET['readmore']."' AND news_draft='0'
    LIMIT 1"
);


Строку 131

"news_comments" => dbcount("(comment_id)", DB_COMMENTS, "comment_type='N' AND comment_item_id='".$data['news_id']."' AND comment_hidden='0'"),


Заменить на

"news_comments" => $data['com_num'],


Файл articles.php

Строка 64-73

$result = dbquery(
    "SELECT ta.article_subject, ta.article_article, ta.article_breaks, 
    ta.article_datestamp, ta.article_reads, ta.article_allow_comments, ta.article_allow_ratings,
    tac.article_cat_id, tac.article_cat_name,
    tu.user_id, tu.user_name, tu.user_status
    FROM ".DB_ARTICLES." ta
    INNER JOIN ".DB_ARTICLE_CATS." tac ON ta.article_cat=tac.article_cat_id
    LEFT JOIN ".DB_USERS." tu ON ta.article_name=tu.user_id
    WHERE ".groupaccess('article_cat_access')." AND article_id='".$_GET['article_id']."' AND article_draft='0'"
);


Заменить на

$result = dbquery(
    "SELECT ta.article_subject, ta.article_article, ta.article_breaks, 
    ta.article_datestamp, ta.article_reads, ta.article_allow_comments, ta.article_allow_ratings,
    tac.article_cat_id, tac.article_cat_name, COUNT(cc.comment_id) AS com_num,
    tu.user_id, tu.user_name, tu.user_status
    FROM ".DB_ARTICLES." ta
    INNER JOIN ".DB_ARTICLE_CATS." tac ON ta.article_cat=tac.article_cat_id
    LEFT JOIN ".DB_COMMENTS." cc ON ta.article_id=cc.comment_item_id AND comment_type='A'
    LEFT JOIN ".DB_USERS." tu ON ta.article_name=tu.user_id
    WHERE ".groupaccess('article_cat_access')." AND article_id='".$_GET['article_id']."' AND article_draft='0'"
);


Строку 92

"article_comments" => dbcount("(comment_id)", DB_COMMENTS, "comment_type='A' AND comment_item_id='".$_GET['article_id']."'"),


Заменить на

"article_comments" => $data['com_num'],



Просмотров: 1831
20.06.2015, 18:28 -

Категория: Web-скрипты » PHP-Fusion