Skip to content

Instantly share code, notes, and snippets.

@andronex
Last active August 20, 2020 11:28
Show Gist options
  • Save andronex/a2e8eff48c554fb35ca3 to your computer and use it in GitHub Desktop.
Save andronex/a2e8eff48c554fb35ca3 to your computer and use it in GitHub Desktop.
Примеры выборок из БД в MODX и кол-во запросов к БД в зависимости от конструкции и способа запроса.
Пустая страница MODX при отключенном кэше:
кол-во зпросов/время запросов/время выполнения PHP кода/общее время
6/0.0002 s/0.0336 s/0.0338 s
***********************************************************
1-ый код с pdoFetch, избранными методами:
$pdo = $modx->getService('pdoFetch');
$pdo->setConfig(array(
'class' => 'modResource',
'where' => 'modResource.id = 200',
'select' => 'id,pagetitle'
));
$pdo->makeQuery();
$pdo->addSelects();
$pdo->addWhere();
$query = $pdo->prepareQuery();
$query->execute();
$res = $query->fetch(PDO::FETCH_ASSOC);
print_r($res);
Array ( [id] => 200 [pagetitle] => test 196 )
9 699 328: Memory usage
8/0.0004 s/0.0340 s/0.0344 s
***********************************************************
2-ой код с pdoFetch, все методы:
$pdo = $modx->getService('pdoFetch');
$pdo->setConfig(array(
'class' => 'modResource',
'where' => 'modResource.id = 200',
'select' => 'id,pagetitle'
));
$res = $pdo->run();
print_r($res);
Array
(
[id] => 200
[pagetitle] => test 196
[idx] => 1
[link] =>
)
9 699 328: Memory usage
11/0.0008 s/0.0334 s/0.0342 s
***********************************************************
3-ий код с pdoFetch, getArray:
$pdo = $modx->getService('pdoFetch');
$res = $pdo->getArray('modResource', '', array('class' => 'modResource',
'where' => 'modResource.id = 200',
'select' => 'id,pagetitle'));
print_r($res);
Array ( [id] => 200 [pagetitle] => test 196 )
0.0008011: Total time
9 699 328: Memory usage
10/0.0013 s/0.0339 s/0.0352 s
***********************************************************
4-ый код с pdoFetch, getCollection:
$pdo = $modx->getService('pdoFetch');
$res = $pdo->getCollection('modResource', '', array('class' => 'modResource',
'where' => 'modResource.id = 200',
'select' => 'id,pagetitle'));
print_r($res);
Array ( [0] => Array ( [id] => 200 [pagetitle] => test 196 ) )
0.0006490: Total time
9 699 328: Memory usage
10/0.0004 s/0.0321 s/0.0325 s
***********************************************************
5-ый код без pdoFetch, prepare execute:
$q = $modx->newQuery('modResource');
$q->select('modResource.id,modResource.pagetitle');
$q->where(array(
'id' => 200
));
$q->prepare();
$q->stmt->execute();
$result = $q->stmt->fetch(PDO::FETCH_ASSOC);
print_r($result);
Array ( [id] => 200 [pagetitle] => test 196 ) 8/0.0007 s/0.0331 s/0.0338 s
***********************************************************
6-ой код без pdoFetch, getObject и $o->_lazy = array():
$q = $modx->newQuery('modResource');
$q->select('modResource.id,modResource.pagetitle');
$q->where(array(
'id' => 200
));
if($o = $modx->getObject('modResource', $q)){
$o->_lazy = array();
print_r($o->toArray());
}
Array ( [id] => 200 [type] => document [contentType] => text/html [pagetitle] => test 196 [longtitle] => [description] => [alias] => [link_attributes] => [published] => [pub_date] => 0 [unpub_date] => 0 [parent] => 0 [isfolder] => [introtext] => [content] => [richtext] => 1 [template] => 0 [menuindex] => 0 [searchable] => 1 [cacheable] => 1 [createdby] => 0 [createdon] => 0 [editedby] => 0 [editedon] => 0 [deleted] => [deletedon] => 0 [deletedby] => 0 [publishedon] => 0 [publishedby] => 0 [menutitle] => [donthit] => [privateweb] => [privatemgr] => [content_dispo] => 0 [hidemenu] => [class_key] => modDocument [context_key] => web [content_type] => 1 [uri] => [uri_override] => 0 [hide_children_in_tree] => 0 [show_in_tree] => 1 [properties] => ) 9/0.0005 s/0.0316 s/0.0321 s
***********************************************************
7-ой код без pdoFetch, без $o->_lazy = array() с getObject:
$q = $modx->newQuery('modResource');
$q->select('modResource.id,modResource.pagetitle');
$q->where(array(
'id' => 200
));
if($o = $modx->getObject('modResource', $q)){
//$o->_lazy = array();
print_r($o->toArray());
}
Array ( [id] => 200 [type] => document [contentType] => text/html [pagetitle] => test 196 [longtitle] => [description] => [alias] => test-196 [link_attributes] => [published] => 1 [pub_date] => 0 [unpub_date] => 0 [parent] => 0 [isfolder] => [introtext] => [content] => [richtext] => 1 [template] => 1 [menuindex] => 199 [searchable] => 1 [cacheable] => 1 [createdby] => 1 [createdon] => 2015-05-14 15:48:23 [editedby] => 0 [editedon] => 0 [deleted] => [deletedon] => 0 [deletedby] => 0 [publishedon] => 2015-05-14 15:48:23 [publishedby] => 1 [menutitle] => [donthit] => [privateweb] => [privatemgr] => [content_dispo] => 0 [hidemenu] => [class_key] => modDocument [context_key] => web [content_type] => 1 [uri] => [uri_override] => 0 [hide_children_in_tree] => 0 [show_in_tree] => 1 [properties] => ) 10/0.0005 s/0.0336 s/0.0341 s
***********************************************************
8-ой код, чистый PDO с SQL кодом:
$q = 'SELECT `modResource`.`id`, `modResource`.`pagetitle` FROM `modx_site_content` AS `modResource` WHERE `modResource`.`id` = 200';
$o = $modx->prepare($q);
$o->execute();
$res = $o->fetch(PDO::FETCH_ASSOC);
print_r($res);
Array ( [id] => 200 [pagetitle] => test 196 ) 8/0.0005 s/0.0295 s/0.0300 s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment