doubi1713 2013-11-22 23:35
浏览 32
已采纳

Yii应用程序日志乱丢COUNT(*)

When running SELECT queries it seems as if Yii is often performing each one twice. The first is a COUNT() and the second is the actual query.

What is causing this? It seems terribly inefficient.

In a related note, why does Yii perform a SHOW COLUMNS FROM and SHOW CREATE TABLE so often? Doesn't setting up a relation within the Model tell Yii enough about the schema?

  • 写回答

1条回答 默认 最新

  • doutang6600 2013-11-23 08:11
    关注

    I assume you are using active records a lot in conjunction with listing widgets such as CGridView and CListView.

    What is causing this? It seems terribly inefficient.

    Well, in order for the pagination to work in CListView and CGridView, the assigned CActiveDataProvider (or actually any data provider) needs to fetch the total item count. This won't work with the result set which usually has a LIMIT clause applied. Hence, an additional COUNT() is performed to retrieve said number.

    In a related note, why does Yii perform a SHOW COLUMNS FROM and SHOW CREATE TABLE so often? Doesn't setting up a relation within the Model tell Yii enough about the schema?

    No. Yii does far more than managing related models. Part of the AR abstraction layer is also to determine which fields are available in a table and hence can be accessed on a model representing a table row. However, you don't have to live with this as schemata can be cached conveniently. To do so, follow these steps:

    1. Configure a caching component such as CApcCache in your protected/config/main.php in the components stanza.
    2. Change the configuration of your db component so it contains the following lines:

      'schemaCacheId'=>'cache',    // This is the name of the cache component you
                                   // configured in step 1. It's also the default value.
      'schemaCacheDuration'=>3600, // Cache table schemata for an hour.
                                   // Set this higher if you like.
      

    A word of advice; don't do this in your development environment: If your database design changes, AR models might not reflect this due to stale caches.

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

报告相同问题?

悬赏问题

  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗