简体   繁体   English

缓存mysql结果后PHP速度低

[英]PHP speed low after caching mysql results

after caching mysql result my web page lower to load page, i'm testing on firebug and get any result for after and before cache result, when i dont use cache my page can fast load , 在缓存mysql结果后我的网页下载到加载页面,我正在测试firebug并获得缓存结果之后和之前的任何结果,当我不使用缓存时我的页面可以快速加载,

PHP: PHP:

<?php
@session_start();
$filename = '../cache/cache.dump';
$AND = $WHERE = NULL;
if ( isset( $_SESSION['portal_ID'] )) {
    $AND   = " AND contents.portal = {$_SESSION['portal_ID']}";
    $WHERE = " WHERE portal = {$_SESSION['portal_ID']}";
}

// ------------------ CACHE MYSQL RESULTS ----------------
if (filemtime($filename) < time()-24*3600) { 
    $getTopics=$db->loadAssoc($db->setQuery("SELECT * ,  categories.title AS category_title, 
                                            status_topics.title as status_topic
                                            FROM contents
                                            JOIN categories ON categories.id = contents.category
                                            JOIN status_topics ON status_topics.id = contents.status 
                                            $AND"));
    $getCategories           = $db->loadAssoc($db->setQuery("SELECT * FROM categories $WHERE"));
    $getAllCategories        = $db->loadAssoc($db->setQuery("SELECT * FROM categories $WHERE"));
    $getTags                 = $db->loadAssoc($db->setQuery("SELECT * FROM tags $WHERE"));
    $getAllTags              = $db->loadAssoc($db->setQuery("SELECT * FROM tags "));
    $getStatusTopics         = $db->loadAssoc($db->setQuery("SELECT * FROM status_topics"));
    $getPages                = $db->loadAssoc($db->setQuery("SELECT * FROM pages $WHERE"));
    $getSiteInformation      = $db->loadRow  ($db->setQuery("SELECT * FROM settings $WHERE"));
    $getSubDomainInformation = $db->loadAssoc($db->setQuery("SELECT * FROM sub_domains"));
    $getUserInformation      = $db->loadAssoc($db->setQuery("SELECT * FROM users $WHERE"));
    $getActiveSubDomains     = $db->loadAssoc($db->setQuery("SELECT * FROM sub_domains WHERE  subdomain_active = 1"));
    $getDeactiveSubDomains   = $db->loadAssoc($db->setQuery("SELECT * FROM sub_domains WHERE  subdomain_active = 0"));
    $getUser_information=$db->loadRow($db->setQuery("SELECT * FROM users JOIN permissions on permissions.id = users.permission WHERE username = '{$_SESSION['username']}'"));
    file_put_contents($filename, serialize(
        array(
                $getTopics,
                $getCategories, 
                $getAllCategories,
                $getTags,
                $getAllTags,
                $getStatusTopics,
                $getPages,
                $getSiteInformation,
                $getSubDomainInformation,
                $getUserInformation,
                $getActiveSubDomains,
                $getDeactiveSubDomains,
                $getUser_information
            ))); 
} 
// ------------------READ FROM CACHE MYSQL RESULTS ----------------
else 
{
  $data = unserialize(file_get_contents($filename));
  list(
        $getTopics,
        $getCategories, 
        $getAllCategories,
        $getTags,
        $getAllTags,
        $getStatusTopics,
        $getPages,
        $getSiteInformation,
        $getSubDomainInformation,
        $getUserInformation,
        $getActiveSubDomains,
        $getDeactiveSubDomains,
        $getUser_information
      ) = $data;
}

//print_r($WHERE);

?>

firebug result in screenshots: firebug导致截图:

after cache 缓存后 缓存后 before cache 在缓存之前 在缓存之前

cache.dump file size is 7kb cache.dump文件大小为7kb

Looks like approach you choose is quite wrong. 看起来你选择的方法是非常错误的。

Your code is trying to sit on someone's else place. 你的代码试图坐在别人的地方。 Database is for storing data. 数据库用于存储数据。
PHP is for retrieving and format it. PHP用于检索和格式化它。

Just do not duplicate your database with homebrewed cache. 只是不要使用自制缓存复制数据库。 Database has it's own and it's blazingly fast. 数据库拥有它自己,它的速度非常快。

So, just get rid of this "cache" and go for the common way - on the every page select the only data required on this very page. 因此,只需摆脱这种“缓存”并采用通用方式 - 在每个页面上选择此页面上所需的唯一数据。 That's all 就这样

You could use var_export() rather than serialize . 您可以使用var_export()而不是serialize I think this will improve the performance . 我认为这会提高性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM