簡體   English   中英

由於查詢,我的網站加載速度太慢

[英]My site is loading so slow because of my query

我需要在我的網站上從數據庫表role_usersuser_idrole_id列中添加一些角色。 我正在使用此代碼:

$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 
"); 





和我的看法:

@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

這是有問題的線:

$temp_soluation['role'] = \App\User::select('id')->where('id', '=', $career_solution->user_id)->first()->role;

沒有它,我的網站將快速正確地加載。 我的網站加載速度如此之慢。我的職業解決方案上有9000多個帖子,我認為這是問題所在。 這個變量$carer_solution_data['role']正在加載所有帖子,這就是我的網站的問題。 如何避免這個問題?

您的問題不一定是您的查詢,您做的事情不會太復雜,問題在於您的瀏覽器被淹沒了。

另一方面,您可以做一些事情來改善您的查詢。 -首先,您可以使用“解釋”語句為您提供有關如何生成查詢以及如何進行改進的更多信息-其次,您可以使用“索引”來改善表的關系和效率

該URL提供了有關使用“ Explain”語句的更多信息: http : //dev.mysql.com/doc/refman/5.0/en/explain.html

就像我之前說的,您的瀏覽器被淹沒了,因為它試圖同時加載9000個(行)元素。 查看實現分頁等功能,以將數據分解為較小的塊。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM