我有一個網站頁面,在加載時,針對具有150,000,000行的表激發10個不同的查詢。避免對頁面重新加載多個SQL查詢
通常,在不到2秒鐘的網頁加載 - 但如果我刷新過於頻繁,它通過長達10秒創造了很多的查詢,其緩慢的頁面加載時間。
如何避免觸發所有這些查詢,因爲它會殺死我的數據庫?
我還沒有緩存。該網站的工作方式如下。我有一個表是所有的URI都存儲。如果用戶輸入一個URL,我將URI從被調用的URL中抓取出來,並檢查表中是否存儲了URI。如果URI存儲在表中,我將從關係數據庫中的其他表中提取相應的數據。
從拉動其它表中的信息的PHP文件中的一個示例代碼是這樣
<?php
set_time_limit(2);
define('MODX_CORE_PATH', '/path/to/modx/core/');
define('MODX_CONFIG_KEY','config');
require_once MODX_CORE_PATH . 'model/modx/modx.class.php';
// Criteria for foreign Database
$host = 'hostname';
$username = 'user';
$password = 'password';
$dbname = 'database';
$port = 3306;
$charset = 'utf8mb4';
$dsn = "mysql:host=$host;dbname=$dbname;port=$port;charset=$charset";
$xpdo = new xPDO($dsn, $username, $password);
// Catch the URI that is called
$pageURI = $_SERVER["REQUEST_URI"];
// Get the language token saved as TV "area" in parent and remove it
if (!isset($modx)) return '';
$top = isset($top) && intval($top) ? $top : 0;
$id= isset($id) && intval($id) ? intval($id) : $modx->resource->get('id');
$topLevel= isset($topLevel) && intval($topLevel) ? intval($topLevel) : 0;
if ($id && $id != $top) {
$pid = $id;
$pids = $modx->getParentIds($id);
if (!$topLevel || count($pids) >= $topLevel) {
while ($parentIds= $modx->getParentIds($id, 1)) {
$pid = array_pop($parentIds);
if ($pid == $top) {
break;
}
$id = $pid;
$parentIds = $modx->getParentIds($id);
if ($topLevel && count($parentIds) < $topLevel) {
break;
}
}
}
}
$parentid = $modx->getObject('modResource', $id);
$area = "/".$parentid->getTVValue('area');
$URL = str_replace($area, '', $pageURI);
$lang= $parentid->getTVValue('lang');
// Issue queries against the foreign database:
$output = '';
$sql = "SELECT epf_application_detail.description FROM epf_application_detail INNER JOIN app_uri ON epf_application_detail.application_id=app_uri.application_id WHERE app_uri.uri = '$URL' AND epf_application_detail.language_code = '$lang'";
foreach ($xpdo->query($sql) as $row) {
$output .= nl2br($row['description']);
}
return $output;
你在用什麼語言? – koerbcm
你應該有不同類型的緩存(在頁面上,sql查詢和語言)。你有這樣的事嗎? – Adrian
你可以問Stack Overflow團隊。他們對DoS攻擊有某種保護。幾分鐘前我發現了。困難的方法:) –