I have a page where I need to do a lot of DB activities - yiidebug says 289 queries.
So I started to look at the queries to see where I could optimize and I saw that model::findByPk() uses 3 select statements to select 1 record, like this:
Querying SQL: SHOW COLUMNS FROM `ugeplaner`
in /Web/webtest/public/protected/models/Ugeplaner.php (31)
in /Web/webtest/public/protected/controllers/SlankeplanController.php (142)
in /Web/webtest/public/protected/controllers/SlankeplanController.php (135)
Querying SQL: SHOW CREATE TABLE `ugeplaner`
in /Web/webtest/public/protected/models/Ugeplaner.php (31)
in /Web/webtest/public/protected/controllers/SlankeplanController.php (142)
in /Web/webtest/public/protected/controllers/SlankeplanController.php (135)
Querying SQL: SELECT * FROM `ugeplaner` `t` WHERE `t`.`ugeplannr`=94 LIMIT 1
in /Web/webtest/public/protected/controllers/SlankeplanController.php (142)
in /Web/webtest/public/protected/controllers/SlankeplanController.php (135)
in /Web/webtest/public/index.php (13)
if the model have the getPrimaryKey() method implemented you could skip 2 statements - and I even don’t understand why there is a ‘show create table’ query
After adding it to your config you will have 3 queries again. On further requests you will have 1 query (since the other 2 about the meta data got cached).