IPB php功能有效,但速度很慢。 SQL需要更快

时间:2016-01-24 18:12:22

标签: php sql performance function ipb

我实际上只使用了这个函数的一小部分,但我想发布它以提供全局图。此函数中有一部分查询可查找用户已发布到论坛的最新附件。该块位于用户配置文件中。它有效,但问题是......它非常慢!核心附件锁定30秒以上,使我的网站无法使用。

任何能够提供帮助的人都会非常感激。

   private function getAttImages($limit, $forumIds = 0, $fidsReverse = false, $topicIds = 0, $membersIds = 0, $order = 'attach_date', $sort = 'desc', $group = null)
        {
            $fids = '';

            if ($forumIds)
            {
                $r = '';
                if ($fidsReverse)
                {
                    $r = ' NOT ';
                }

                if (is_array($forumIds))
                {
                    $forumIds = implode(',', $forumIds);
                }

                $fids = ' AND forums_topics.forum_id ' . $r . ' IN (' . $forumIds .  ')';
            }

            $tids = '';
            if ($topicIds)
            {
                $tids = ' AND forums_topics.tid IN (' . $topicIds . ')';
            }

            $mids = '';
            if ($membersIds)
            {
                $mids = ' AND core_attachments.attach_member_id IN (' . $membersIds . ')';
            }


            $whereT = array();
            $joinsT = array();

            $findInPosts = ' AND ' . \IPS\Db::i()->findInSet('queued', array('0'));
            $joinsT[] = array(
                                'select'    => 'forums_posts.*',
                                'from'      => 'forums_posts',
                                'where'     => array("forums_posts.pid=core_attachments_map.id2" . $findInPosts),
                        );

            $findInTopics = ' AND ' . \IPS\Db::i()->findInSet('approved', array('1'));
            $joinsT[] = array(
                                'select'    => 'forums_topics.*',
                                'from'      => 'forums_topics',
                                'where'     => array("forums_topics.tid=forums_posts.topic_id" . $findInTopics . $fids . $tids),
                        );

            $select = 'core_attachments.attach_id AS custom_data, core_attachments.*';
            if ($group)
            {
                $select = 'core_attachments.attach_id AS custom_data, COUNT(attach_is_image) as cnt_images, SUM(attach_hits) as summ_attach_hits, core_attachments.*';
            }

            $joinsT[] = array(
                                'select'    => $select,
                                'from'      => 'core_attachments',
                                'where'     => array('core_attachments.attach_is_image=1 AND core_attachments.attach_is_archived=0 AND core_attachments.attach_id=core_attachments_map.attachment_id' . $mids),
                        );              

            $joinsT[] = array(  'select'    => 'core_members.member_id, core_members.member_group_id, core_members.mgroup_others, core_members.name, core_members.members_seo_name',
                                'from'      => 'core_members',
                                'where'     => array('core_attachments.attach_member_id=core_members.member_id' . $mids),
                        );

            $joinsT[] = array(  'select'    => 'core_permission_index.perm_id',
                                'from'      => 'core_permission_index',
                                'where'     => array("core_permission_index.app='forums' AND core_permission_index.perm_type='forum' AND core_permission_index.perm_type_id=forums_topics.forum_id"),
                        );

            $groupT = $group;

            $whereT[] = array(
                "core_attachments_map.location_key='forums_Forums' AND " . 
                \IPS\Db::i()->findInSet('perm_view', array_merge(array(\IPS\Member::loggedIn()->member_group_id), array_filter(explode(',', \IPS\Member::loggedIn()->mgroup_others)))) . " OR perm_view='*'" .
                $fids . $tids . $mids
            );

            $table = new \IPS\Helpers\Table\Db(
                'core_attachments_map',
                \IPS\Http\Url::internal('app=core&module=system&controller=nbattachpictures', 'front', 'nbattachpictures'),
                $whereT,
                $groupT
            );

            $table->joins = $joinsT;
            $table->limit = $limit;
            $table->sortBy = $order;
            $table->sortDirection = $sort;

            $table->rowsTemplate = array(\IPS\Theme::i()->getTemplate('plugins', 'core', 'global'), 'nbAttachmentsBlocksRows');

            $table->parsers = array(
                'custom_data' => function( $val, $row )
                {
                    return array(
                        'topic_data' => \IPS\Http\Url::internal("app=forums&module=forums&controller=topic&id={$row['tid']}", 'front', 'forums_topic', array($row['title_seo'])),
                        'summ_attach_hits' => $row['summ_attach_hits'],
                        'jewel' => $this->attachJewel($row['summ_attach_hits']),
                    );
                },
            );

            return $table;
        }

0 个答案:

没有答案