首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >由于我的查询,我的站点加载得太慢了。

由于我的查询,我的站点加载得太慢了。
EN

Stack Overflow用户
提问于 2019-08-28 08:52:31
回答 1查看 76关注 0票数 0

我需要在我的网站上添加一些来自数据库表role_users、列user_idrole_id的角色。我把它和这个代码一起使用:

代码语言:javascript
复制
$career_solutions_data = DB::select(" 
SELECT 
career_solutions.id,
career_solutions.user_id,  
career_solutions.subject, 
career_solutions.date, 
career_solutions.public, 
career_solutions.views, 
career_solutions.optional, 
career_solutions.on_offer, 
users.username, 
users.profile_picture, 
categories.category, 
categories.category_url, 
categories.color, 
career_solutions_categories.category as sub_category,
career_solutions_format.category as event_format,
career_solutions_certification.category as certification

FROM career_solutions 

INNER JOIN categories 
ON categories.id = career_solutions.topic_category_id 

INNER JOIN career_solutions_format
ON career_solutions_format.id = career_solutions.topic_format_id

INNER JOIN career_solutions_certification
ON career_solutions_certification.id = career_solutions.topic_certification_id

INNER JOIN career_solutions_categories 
ON career_solutions_categories.id = career_solutions.topic_subcategory_id 

INNER JOIN users 
ON users.id = career_solutions.user_id 


INNER JOIN privacy_settings 
ON privacy_settings.user_id = users.id 

WHERE users.deleted_at IS NULL 
AND ( 
(privacy_settings.career_solutions = 0 AND public = 1 ) 
OR (users.id IN ( 

SELECT contacts.contact_id 
FROM contacts 
WHERE contacts.user_id = $id 
) 
) 
) 

OR users.id = $id 

ORDER BY date desc limit 5000 
"); 

我的看法是:

代码语言:javascript
复制
@if($carer_solution_data['role'][0]['pivot']['role_id'] == 1 )
                                            <i style="font-size: 11px" class="icon-user"></i> 
                                            @else <i style="font-size: 11px" class="icon-hotel-restaurant-172 u-line-icon-pro fa- fa-lg"></i>

                                            @endif

以下是问题所在:

代码语言:javascript
复制
$temp_soluation['role'] = \App\User::select('id')->where('id', '=', $career_solution->user_id)->first()->role;

没有它,我的网站是快速和正确的加载。我的网站现在加载得太慢了,我在我的职业解决方案上有关于9000+的帖子,我认为这就是问题所在。这个变量$carer_solution_data['role']正在加载所有的帖子,这就是我的网站的问题。我怎样才能避免这个问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-28 09:54:52

你的问题不一定是你的查询,你没有做任何太复杂的事情,问题是你的浏览器被淹没了。

另一方面,您可以做一些事情来改进您的查询。-首先,您可以使用“解释”语句向您提供有关如何生成查询以及如何改进查询的更多信息;其次,您可以使用“索引”来改进表的关系和效率。

此URL提供有关使用“解释”语句的更多信息:http://dev.mysql.com/doc/refman/5.0/en/explain.html

如前所述,您的浏览器正被淹没,因为它试图同时加载9000 (行)元素。查看实现功能,如分页,以便将数据分解为较小的块。

代码语言:javascript
复制
Example of Pagination: (9000 Rows)

Chunk: 0-99 - Page 1
Chunk: 100-199 - Page 2
Chunk: 200-299 - Page 3
...

您的系统应该能够根据一个事件向服务器发送请求,比如滚动到页面底部(无限滚动),选择下一个页面按钮,提供下一个块。

您还可以启用laravel的分析器来查看代码中的任何瓶颈:https://packagist.org/packages/jkocik/laravel-profiler

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57688491

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档