Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dependency::$reusable doesn't work resulting in many DB queries #3052

Closed
githubjeka opened this issue Apr 10, 2014 · 19 comments
Closed

Dependency::$reusable doesn't work resulting in many DB queries #3052

githubjeka opened this issue Apr 10, 2014 · 19 comments
Labels
Milestone

Comments

@githubjeka
Copy link
Contributor

<?php
Yii::$app->db->beginCache(
    null, 
    new \yii\caching\DbDependency(['sql' => 'SELECT MAX(updateAt) FROM sector']));
?>
<?= $piping->getLine() ?>
<?= $piping->getLineOnBalance() ?>
<?= $piping->getLineOffBalance() ?>
<?= $piping->getLineBeforeOffBalance() ?>
<?= $piping->getLineAftereOffBalance() ?>

<?php
Yii::$app->db->endCache();
?>

in logs

1   14:53:07.133    0.0 ms  yii\db\Connection::open Opening DB connection: mysql:host=localhost;dbname=inspection_ts
2   14:53:07.133    0.0 ms  yii\db\Command::query   SELECT MAX(updateAt) FROM sector
3   14:53:07.133    0.0 ms  yii\db\Command::query   SELECT MAX(updateAt) FROM sector
4   14:53:07.148    0.0 ms  yii\db\Command::query   SELECT MAX(updateAt) FROM sector
5   14:53:07.148    0.0 ms  yii\db\Command::query   SELECT MAX(updateAt) FROM sector
6   14:53:07.148    0.0 ms  yii\db\Command::query   SELECT MAX(updateAt) FROM sector

Why repeatedly perform dependent sql?

p.s.

 'cache' => ['class' => 'yii\caching\FileCache'],
@samdark
Copy link
Member

samdark commented Apr 10, 2014

It's logging regardless getting from db or cache, I think.

@resurtm
Copy link
Contributor

resurtm commented Apr 10, 2014

There is dedicated property for customizing that behavior. Check the \yii\caching\Dependency::$reusable property.

@githubjeka
Copy link
Contributor Author

 public $reusable = false;

@resurtm
Copy link
Contributor

resurtm commented Apr 10, 2014

@githubjeka, have you tried to turn it on and check the result? :-)

@githubjeka
Copy link
Contributor Author

I try ^_^. But there false, obviously the same as it is disabled.

@resurtm
Copy link
Contributor

resurtm commented Apr 10, 2014

@githubjeka, yes, and you suggest us to change its default value?

@githubjeka
Copy link
Contributor Author

does not matter $reusable = true or false. i get five logs yii\db\Command::query SELECT MAX(updateAt) FROM sector

@qiangxue qiangxue added this to the 2.0 RC milestone Apr 10, 2014
@samdark
Copy link
Member

samdark commented Apr 10, 2014

Can you check the following? If it doesn't work it should be a bug.

<?php
Yii::$app->db->beginCache(
    null, 
    new \yii\caching\DbDependency(['sql' => 'SELECT MAX(updateAt) FROM sector', 'reusable' => true]));
?>
<?= $piping->getLine() ?>
<?= $piping->getLineOnBalance() ?>
<?= $piping->getLineOffBalance() ?>
<?= $piping->getLineBeforeOffBalance() ?>
<?= $piping->getLineAftereOffBalance() ?>

<?php
Yii::$app->db->endCache();
?>

@githubjeka
Copy link
Contributor Author

i tried - not work

@samdark
Copy link
Member

samdark commented Apr 10, 2014

Looks like Dependency::getHasChanged() logic is flawed. i.e. serialize result will be different after adding stuff to $_reusableData.

@githubjeka
Copy link
Contributor Author

In \yii\db\Command::queryInternal

 if (($result = $cache->get($cacheKey)) !== false) {
                Yii::trace('Query result served from cache', 'yii\db\Command::query');
                return $result;
            }

should not be performed repeatedly. Here ($cache->get) it is necessary to stop him. How? :)

@samdark
Copy link
Member

samdark commented Apr 10, 2014

@githubjeka it's not query cache level, it's dependency level that should be fixed.

@githubjeka
Copy link
Contributor Author

$cache->get call $value[1]->getHasChanged($this) and call

  if (!$this->reusable) {
            return $this->generateDependencyData($cache) !== $this->data;

How to stop him on the second sql:

<?= $piping->getLine() ?>
<?= $piping->getLineOnBalance() ?>
<?= $piping->getLineOffBalance() ?>
<?= $piping->getLineBeforeOffBalance() ?>
<?= $piping->getLineAftereOffBalance() ?>

@samdark
Copy link
Member

samdark commented Apr 10, 2014

See #3052 (comment)

@samdark samdark changed the title Query Caching Dependency::$reusable doesn't work resulting in many DB queries Apr 10, 2014
@samdark
Copy link
Member

samdark commented Apr 10, 2014

I've renamed the issue.

@qiangxue
Copy link
Member

@githubjeka Could you update and try again? Thanks.

cebe added a commit that referenced this issue Apr 10, 2014
* 'master' of github.com:yiisoft/yii2: (79 commits)
  Refactored app bootstrap logic.
  Update authorization.md
  Fixes #3052: Fixed the issue that cache dependency data is not reused when `reusable` is set true
  start debug logging only if debug runs when bootstrap.
  Update finnish translation
  Add ODBC support to yii\db\Connection
  updated error handler and requirement checker links.
  fixed broken API links [skip ci]
  added more doc [skip ci]
  update class map.
  Fixes #2034: Added `ContentNegotiator` to support response format and language negotiation
  renamed attributes to attributeNames in model
  updated phpdoc
  Removed `Application::preload` in favor of `Application::bootstrap`
  Update module-debug.md
  Update model.md
  Fixes
  Update basics.md
  typo fix [skip ci]
  Added `HtmlResponseFormatter` and `JsonResponseFormatter`
  ...
@githubjeka
Copy link
Contributor Author

Not work:
see my comment #3052 (comment)

$reusable = false; and work the

  if (!$this->reusable) {
            return $this->generateDependencyData($cache) !== $this->data;

PR not fixed this, it essentially has not changed this section.
4674f5b#diff-25b027a479beaf1cc92348af5e3acbafR52

In short, everything is still.

@githubjeka
Copy link
Contributor Author

But...

I delete folder cache and set $reusable = false; - not work. I get five rows inside logs.

Delete forlder cache and set $reusable = true; , this work, but work like false. I get one row SELECT MAX(updateAt) FROM sector.

It turns out if you want to reusable , then reusable is false. Is this logical?

@githubjeka
Copy link
Contributor Author

Revised his views, is all logical. Everything works. Thanks.

@cebe cebe modified the milestones: 2.0 Beta, 2.0 RC Apr 11, 2014
tonydspaniard added a commit to tonydspaniard/yii2 that referenced this issue Apr 12, 2014
* upstream: (401 commits)
  Minor refacotring of bootstrap active field and form.
  support hiding error and label tags via error(false) and label(false).
  updated base AR phpdoc for findOne() and findAll()
  fixed wrong example.
  allow . in input name
  added not about joinWith background
  add guide about filtering and sorting related columns
  Update data-grid.md
  Issue yiisoft#3029: Implement ActiveForm and ActiveField
  added docs about gridview filtering
  typo
  Update data-overview.md
  Looks like GridView has its own section... :)
  Some docs on GridView
  Bulgarian translation
  Most trivial edit
  fixed file PHPdoc
  Refactored app bootstrap logic.
  Update authorization.md
  Fixes yiisoft#3052: Fixed the issue that cache dependency data is not reused when `reusable` is set true
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants