demora ao carregar dados. Olá, estou com problemas com as listagens no template II do adianti, estão demorando muito para carregar algumas paginas, (duas paginas, uma contem mais de 3 mil linhas na tabela, a outra não tentos dados assim, mas o select é mais criterioso) as vezes nem carrega a pagina (esgota o tempo). Como resolver esse problema? Meu pc é um notebook core i5 com 4gb de memoria RAM...
BI
demora ao carregar dados.  
Fechado
Olá, estou com problemas com as listagens no template II do adianti, estão demorando muito para carregar algumas paginas, (duas paginas, uma contem mais de 3 mil linhas na tabela, a outra não tentos dados assim, mas o select é mais criterioso) as vezes nem carrega a pagina (esgota o tempo).

Como resolver esse problema?

Meu pc é um notebook core i5 com 4gb de memoria RAM

Curso Dominando o Adianti Framework

O material mais completo de treinamento do Framework.
Curso em vídeo aulas + Livro completo + Códigos fontes do projeto ERPHouse.
Conteúdo Atualizado!


Dominando o Adianti Framework Quero me inscrever agora!

Comentários (4)


JC

Saudações, já leu este artigo www.adianti.com.br/forum/pt/view_879?relatorios-com-queries-complexa, ou postar o codigo para apreciação.
BI

Opa, segue o código de uma pagina de listagem...

 
  1. <?php
  2. class Evento_aplicacaoList extends TPage
  3. {
  4. private $form; // registration form
  5. private $datagrid; // listing
  6. private $pageNavigation;
  7. private $loaded;
  8. /**
  9. * Class constructor
  10. * Creates the page, the form and the listing
  11. */
  12. public function __construct()
  13. {
  14. parent::__construct();
  15. // creates the form
  16. $this->form = new TForm('form_search_Evento_aplicacao');
  17. $this->form->class = 'tform'; // CSS class
  18. // creates a table
  19. $table = new TTable;
  20. $table-> width = '100%';
  21. $this->form->add($table);
  22. // add a row for the form title
  23. $row = $table->addRow();
  24. $row->class = 'tformtitle'; // CSS class
  25. $row->addCell( new TLabel('Evento_aplicacao') )->colspan = 6;
  26. // create the form fields
  27. $evento = new TEntry('evento');
  28. $empreendimento_id = new TDBCombo('empreendimento_id','sqlserver','Empreendimento','id','sigla');
  29. $aplicacao_id = new TCombo('aplicacao_id');
  30. $evento = new TEntry('evento');
  31. $grupo_id = new TDBCombo('grupo_id', 'sqlserver','Grupo','id','descricao');
  32. // set change action for empreendimento
  33. $change_action = new TAction(array($this, 'onChange'));
  34. $empreendimento_id->setChangeAction($change_action);
  35. // define the sizes
  36. $evento->setSize(625);
  37. $grupo_id->setSize(200);
  38. $aplicacao_id->setSize(100);
  39. $empreendimento_id->setSize(100);
  40. // add one row for each form field
  41. $row = $table->addRow();
  42. $row->addCell($label_empreendimento_id = new TLabel('Empreendimento:'));
  43. $row->addCell($empreendimento_id);
  44. $row->addCell($label_aplicacao_id = new TLabel('Aplicação:'));
  45. $row->addCell($aplicacao_id);
  46. $row->addCell($label_grupo_id = new TLabel('Grupo:'));
  47. $row->addCell($grupo_id);
  48. $row = $table->addRow();
  49. $row->addCell($label_evento = new TLabel('Evento:'));
  50. $row->addCell($evento)->colspan = 6;
  51. $this->form->setFields(array($evento,$grupo_id,$aplicacao_id,$empreendimento_id));
  52. // keep the form filled during navigation with session data
  53. $this->form->setData( TSession::getValue('Evento_aplicacao_filter_data') );
  54. // create two action buttons to the form
  55. $find_button = TButton::create('find', array($this, 'onSearch'), _t('Find'), 'ico_find.png');
  56. $new_button = TButton::create('new', array('Evento_aplicacaoForm', 'onEdit'), _t('New'), 'ico_new.png');
  57. $this->form->addField($find_button);
  58. $this->form->addField($new_button);
  59. $buttons_box = new THBox;
  60. $buttons_box->add($find_button);
  61. $buttons_box->add($new_button);
  62. // add a row for the form action
  63. $row = $table->addRow();
  64. $row->class = 'tformaction'; // CSS class
  65. $row->addCell($buttons_box)->colspan = 6;
  66. // creates a Datagrid
  67. $this->datagrid = new TDataGrid;
  68. $this->datagrid->setHeight(320);
  69. // creates the datagrid columns
  70. $id = new TDataGridColumn('id', 'ID', 'center', 30);
  71. $evento = new TDataGridColumn('evento', 'Evento', 'left', 450);
  72. $grupo_id = new TDataGridColumn('grupos', 'Grupo', 'left', 100);
  73. $aplicacao_id = new TDataGridColumn('aplicacaos', 'Aplicação', 'center', 50);
  74. $empreendimento_id = new TDataGridColumn('empreendimentos', 'Empreendimento', 'center', 50);
  75. $indicador = new TDataGridColumn('indicador', 'Indicador', 'center', 50);
  76. // add the columns to the DataGrid
  77. $this->datagrid->addColumn($id);
  78. $this->datagrid->addColumn($empreendimento_id);
  79. $this->datagrid->addColumn($aplicacao_id);
  80. $this->datagrid->addColumn($evento);
  81. $this->datagrid->addColumn($grupo_id);
  82. $this->datagrid->addColumn($indicador);
  83. // creates the datagrid column actions
  84. $order_id= new TAction(array($this, 'onReload'));
  85. $order_id->setParameter('order', 'id');
  86. $id->setAction($order_id);
  87. $order_evento= new TAction(array($this, 'onReload'));
  88. $order_evento->setParameter('order', 'evento');
  89. $evento->setAction($order_evento);
  90. $order_grupo_id= new TAction(array($this, 'onReload'));
  91. $order_grupo_id->setParameter('order', 'grupo_id');
  92. $grupo_id->setAction($order_grupo_id);
  93. $order_aplicacao_id= new TAction(array($this, 'onReload'));
  94. $order_aplicacao_id->setParameter('order', 'aplicacao_id');
  95. $aplicacao_id->setAction($order_aplicacao_id);
  96. $order_empreendimento_id= new TAction(array($this, 'onReload'));
  97. $order_empreendimento_id->setParameter('order', 'empreendimento_id');
  98. $empreendimento_id->setAction($order_empreendimento_id);
  99. // creates two datagrid actions
  100. $action1 = new TDataGridAction(array('Evento_aplicacaoForm', 'onEdit'));
  101. $action1->setLabel(_t('Edit'));
  102. $action1->setImage('ico_edit.png');
  103. $action1->setField('id');
  104. $action2 = new TDataGridAction(array($this, 'onDelete'));
  105. $action2->setLabel(_t('Delete'));
  106. $action2->setImage('ico_delete.png');
  107. $action2->setField('id');
  108. // add the actions to the datagrid
  109. $this->datagrid->addAction($action1);
  110. $this->datagrid->addAction($action2);
  111. // create the datagrid model
  112. $this->datagrid->createModel();
  113. // creates the page navigation
  114. $this->pageNavigation = new TPageNavigation;
  115. $this->pageNavigation->setAction(new TAction(array($this, 'onReload')));
  116. $this->pageNavigation->setWidth($this->datagrid->getWidth());
  117. // create the page container
  118. $table = new TTable;
  119. $table->style = 'width: 80%';
  120. $table->addRow()->addCell(new TXMLBreadCrumb('menu.xml', __CLASS__));
  121. $table->addRow()->addCell($this->form);
  122. $table->addRow()->addCell($this->datagrid);
  123. $table->addRow()->addCell($this->pageNavigation);
  124. parent::add($table);
  125. }
  126. /**
  127. * method onInlineEdit()
  128. * Inline record editing
  129. * @param $param Array containing:
  130. * key: object ID value
  131. * field name: object attribute to be updated
  132. * value: new attribute content
  133. */
  134. function onInlineEdit($param)
  135. {
  136. try
  137. {
  138. // get the parameter $key
  139. $field = $param['field'];
  140. $key = $param['key'];
  141. $value = $param['value'];
  142. TTransaction::open('sqlserver'); // open a transaction with database
  143. $object = new Evento_aplicacao($key); // instantiates the Active Record
  144. $object->{$field} = $value;
  145. $object->store(); // update the object in the database
  146. TTransaction::close(); // close the transaction
  147. $this->onReload($param); // reload the listing
  148. new TMessage('info', "Record Updated");
  149. }
  150. catch (Exception $e) // in case of exception
  151. {
  152. new TMessage('error', '<b>Error</b> ' . $e->getMessage()); // shows the exception error message
  153. TTransaction::rollback(); // undo all pending operations
  154. }
  155. }
  156. /**
  157. * method onSearch()
  158. * Register the filter in the session when the user performs a search
  159. */
  160. function onSearch()
  161. {
  162. // get the search form data
  163. $data = $this->form->getData();
  164. // clear session filters
  165. TSession::setValue('Evento_aplicacaoList_filter_evento', NULL);
  166. TSession::setValue('Evento_aplicacaoList_filter_grupo_id', NULL);
  167. TSession::setValue('Evento_aplicacaoList_filter_aplicacao_id', NULL);
  168. TSession::setValue('Evento_aplicacaoList_filter_empreendimento_id', NULL);
  169. if (isset($data->evento) AND ($data->evento)) {
  170. $filter = new TFilter('evento', 'like', "%{$data->evento}%"); // create the filter
  171. TSession::setValue('Evento_aplicacaoList_filter_evento', $filter); // stores the filter in the session
  172. }
  173. if (isset($data->grupo_id) AND ($data->grupo_id)) {
  174. $filter = new TFilter('grupo_id', '=', "$data->grupo_id"); // create the filter
  175. TSession::setValue('Evento_aplicacaoList_filter_grupo_id', $filter); // stores the filter in the session
  176. }
  177. if (isset($data->aplicacao_id) AND ($data->aplicacao_id)) {
  178. $filter = new TFilter('aplicacao_id', '=', "$data->aplicacao_id"); // create the filter
  179. TSession::setValue('Evento_aplicacaoList_filter_aplicacao_id', $filter); // stores the filter in the session
  180. }
  181. if (isset($data->empreendimento_id) AND ($data->empreendimento_id)) {
  182. $filter = new TFilter('empreendimento_id', '=', "$data->empreendimento_id"); // create the filter
  183. TSession::setValue('Evento_aplicacaoList_filter_empreendimento_id', $filter); // stores the filter in the session
  184. }
  185. // fill the form with data again
  186. $this->form->setData($data);
  187. // keep the search data in the session
  188. TSession::setValue('Evento_aplicacao_filter_data', $data);
  189. $param=array();
  190. $param['offset'] =0;
  191. $param['first_page']=1;
  192. $this->onReload($param);
  193. }
  194. /**
  195. * method onReload()
  196. * Load the datagrid with the database objects
  197. */
  198. function onReload($param = NULL)
  199. {
  200. try
  201. {
  202. // open a transaction with database 'sqlserver'
  203. TTransaction::open('sqlserver');
  204. // creates a repository for Evento_aplicacao
  205. $repository = new TRepository('Evento_aplicacao');
  206. $limit = 30;
  207. // creates a criteria
  208. $criteria = new TCriteria;
  209. // default order
  210. if (empty($param['order']))
  211. {
  212. $param['order'] = 'id';
  213. $param['direction'] = 'asc';
  214. }
  215. $criteria->setProperties($param); // order, offset
  216. $criteria->setProperty('limit', $limit);
  217. if (TSession::getValue('Evento_aplicacaoList_filter_evento')) {
  218. $criteria->add(TSession::getValue('Evento_aplicacaoList_filter_evento')); // add the session filter
  219. }
  220. if (TSession::getValue('Evento_aplicacaoList_filter_grupo_id')) {
  221. $criteria->add(TSession::getValue('Evento_aplicacaoList_filter_grupo_id')); // add the session filter
  222. }
  223. if (TSession::getValue('Evento_aplicacaoList_filter_aplicacao_id')) {
  224. $criteria->add(TSession::getValue('Evento_aplicacaoList_filter_aplicacao_id')); // add the session filter
  225. }
  226. if (TSession::getValue('Evento_aplicacaoList_filter_empreendimento_id')) {
  227. $criteria->add(TSession::getValue('Evento_aplicacaoList_filter_empreendimento_id')); // add the session filter
  228. }
  229. // load the objects according to criteria
  230. $objects = $repository->load($criteria, FALSE);
  231. $this->datagrid->clear();
  232. if ($objects)
  233. {
  234. // iterate the collection of active records
  235. foreach ($objects as $object)
  236. {
  237. //identifica quando é indicador ou não.
  238. if ($object->indicador == 1){
  239. $object->indicador = 'Sim';
  240. }else{
  241. $object->indicador = 'Não';
  242. }
  243. // add the object inside the datagrid
  244. $this->datagrid->addItem($object);
  245. }
  246. }
  247. // reset the criteria for record count
  248. $criteria->resetProperties();
  249. $count= $repository->count($criteria);
  250. $this->pageNavigation->setCount($count); // count of records
  251. $this->pageNavigation->setProperties($param); // order, page
  252. $this->pageNavigation->setLimit($limit); // limit
  253. // close the transaction
  254. TTransaction::close();
  255. $this->loaded = true;
  256. }
  257. catch (Exception $e) // in case of exception
  258. {
  259. // shows the exception error message
  260. new TMessage('error', '<b>Error</b> ' . $e->getMessage());
  261. // undo all pending operations
  262. TTransaction::rollback();
  263. }
  264. }
  265. /**
  266. * method onDelete()
  267. * executed whenever the user clicks at the delete button
  268. * Ask if the user really wants to delete the record
  269. */
  270. function onDelete($param)
  271. {
  272. // define the delete action
  273. $action = new TAction(array($this, 'Delete'));
  274. $action->setParameters($param); // pass the key parameter ahead
  275. // shows a dialog to the user
  276. new TQuestion(TAdiantiCoreTranslator::translate('Do you really want to delete ?'), $action);
  277. }
  278. /**
  279. * method Delete()
  280. * Delete a record
  281. */
  282. function Delete($param)
  283. {
  284. try
  285. {
  286. $key=$param['key']; // get the parameter $key
  287. TTransaction::open('sqlserver'); // open a transaction with database
  288. $object = new Evento_aplicacao($key, FALSE); // instantiates the Active Record
  289. $object->delete(); // deletes the object from the database
  290. TTransaction::close(); // close the transaction
  291. $this->onReload( $param ); // reload the listing
  292. new TMessage('info', TAdiantiCoreTranslator::translate('Record deleted')); // success message
  293. }
  294. catch (Exception $e) // in case of exception
  295. {
  296. new TMessage('error', '<b>Error</b> ' . $e->getMessage()); // shows the exception error message
  297. TTransaction::rollback(); // undo all pending operations
  298. }
  299. }
  300. /**
  301. * method show()
  302. * Shows the page
  303. */
  304. function show()
  305. {
  306. // check if the datagrid is already loaded
  307. if (!$this->loaded AND (!isset($_GET['method']) OR $_GET['method'] !== 'onReload') )
  308. {
  309. $this->onReload( func_get_arg(0) );
  310. }
  311. parent::show();
  312. }
  313. //função para carregar as aplicações 'equipamentos'
  314. public static function onChange($param)
  315. {
  316. TTransaction::open('sqlserver');
  317. $criteria = new TCriteria;
  318. $criteria->add(new TFilter('empreendimento_id', '=', $param['empreendimento_id']));
  319. $repository = new TRepository('Aplicacao');
  320. $aplicacaos = $repository->load($criteria);
  321. $options = array();
  322. foreach ($aplicacaos as $aplicacao){
  323. $options[$aplicacao->id] = $aplicacao->sigla;
  324. }
  325. //TForm::sendData('form_Vertedouro', $objeto);
  326. TCombo::reload('form_search_Evento_aplicacao', 'aplicacao_id', $options);
  327. }
  328. }
  329. ?>
PD

Não creio que o problema tenha alguma relação com o framework.
A tabela é muito pequena e o select é paginado. Temos cases com milhões de registros.
Acredito que é um problema relativo ao ambiente. Precisamos de mais informações sobre
seu ambiente para ajudar. Aparentemente seu código não tem problemas.
BI

Obrigado pela atenção!
Resolvido o problema, em tabelas onde tem muitas chaves estrangeiras e a classe model carrega os objetos referenciados, a listagem fica lenta.

Então segui as recomendações do próprio Pablo em um artigo publicado... Criei views somente para as listagens, fica extremamente rápido para carregar a página.