drjltlm156790 2014-02-09 06:02
浏览 26
已采纳

选项卡导航栏有多个计数

I have created a view like gmail inbox where inbox is clickable from left side and then there are tabs on top , like Promotions, Forums etc. When user will click on each tab, then the set of items that belong to that tab is shown, for example - forums and MySQL query is fired.

The individual views can have little differences in items look and feel but navigation is alike.

So, there are individual SQL queries being fired for each tab on click, but now I have to also show count of total items for each tab all the time and keep it updated upon click of any other tab / left main icon as well.

So, I am ending up firing SQL queries for all tabs and keep their counts all the time which is very database intensive. 

The other extreme will be to keep all such counts in MySQL table and increment/decrement it all the time for which I need to change all existing functionality. Please suggest other means for such navigations.

PHP Yii framework 1.1.* and MySQL 5.6.

  • 写回答

1条回答 默认 最新

  • duankang8114 2014-02-09 09:09
    关注

    The way you're loading and counting the items for each tab can make a difference to the MySQL load and the page speed. A simple benchmark between CActiveRecord::count() and CActiveRecord::findAll() can be seen here:

    $modelCount = Model::model()->count();
    
    $modelCount = count(Model::model()->findAll());
    
    // DB profile log:
    // system.db.CDbCommand.query(SELECT COUNT(*) FROM `throw` `t`)     1   0.00029s    
    // system.db.CDbCommand.query(SELECT * FROM `throw` `t`)    1   0.00283s
    

    The CActiveRecord::count() is ten times faster, even before you've counted the FindAll() with PHP count(). Of course, for the tab you're actually on - you could use the same findAll() query to count for the tab number, and populate the list of items on that page.

    To test this for yourself and experiment with SQL query speeds, add query profiling add the following to your config.php file:

    'db'=>array(
        // ... existing db config etc
        'enableProfiling' => true,
        'enableParamLogging' => true,
    ),
    

    And in log config...

    'log'=>array(
        'class'=>'CLogRouter',
        'routes'=>array(
             array(
                'class' => 'CProfileLogRoute',
                'levels' => 'profile',
                'enabled' => true,
              ),
    

    Optimising up your count queries might be enough to improve your page speed and MySQL load to your or your user's satisfaction.

    Alternatively you could keep the tab counts in a session array:

    Yii::app()->session['tabCounts'] = array(
        'tab1' => 44,
        'tab2' => 456,
        'tab3' => 9345,
    );
    

    then in any controller action that would result in a change to the number of items, add a hook to alter the number. Of course you'd still have to make the initial MySQL queries to populate the array on first page load.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值