Skip to content

Commit

Permalink
improving acl perfomance by adding indexes on acl tables and reorgani…
Browse files Browse the repository at this point in the history
…zation joins in getting acl node function
  • Loading branch information
Anatoliy Petrovskiy committed Jun 20, 2014
1 parent 6a8033a commit bb15271
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
18 changes: 15 additions & 3 deletions app/Config/Schema/db_acl.php
Expand Up @@ -56,7 +56,11 @@ public function after($event = array()) {
'alias' => array('type' => 'string', 'null' => true),
'lft' => array('type' => 'integer', 'null' => true, 'default' => null, 'length' => 10),
'rght' => array('type' => 'integer', 'null' => true, 'default' => null, 'length' => 10),
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1))
'indexes' => array(
'PRIMARY' => array('column' => 'id', 'unique' => 1),
'idx_acos_lft_rght' => array('column' => array('lft', 'rght'),'unique' => 0),
'idx_acos_alias' => array('column' => 'alias','unique' => 0)
)
);

/**
Expand All @@ -70,7 +74,11 @@ public function after($event = array()) {
'alias' => array('type' => 'string', 'null' => true),
'lft' => array('type' => 'integer', 'null' => true, 'default' => null, 'length' => 10),
'rght' => array('type' => 'integer', 'null' => true, 'default' => null, 'length' => 10),
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1))
'indexes' => array(
'PRIMARY' => array('column' => 'id','unique' => 1),
'idx_aros_lft_rght' => array('column' => array('lft', 'rght'),'unique' => 0),
'idx_aros_alias' => array('column' => 'alias','unique' => 0)
)
);

/**
Expand All @@ -85,7 +93,11 @@ public function after($event = array()) {
'_read' => array('type' => 'string', 'null' => false, 'default' => '0', 'length' => 2),
'_update' => array('type' => 'string', 'null' => false, 'default' => '0', 'length' => 2),
'_delete' => array('type' => 'string', 'null' => false, 'default' => '0', 'length' => 2),
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1), 'ARO_ACO_KEY' => array('column' => array('aro_id', 'aco_id'), 'unique' => 1))
'indexes' => array(
'PRIMARY' => array('column' => 'id', 'unique' => 1),
'ARO_ACO_KEY' => array('column' => array('aro_id', 'aco_id'), 'unique' => 1),
'idx_aco_id' => array('column' => 'aco_id', 'unique' => 0)
)
);

}
13 changes: 12 additions & 1 deletion app/Config/Schema/db_acl.sql
Expand Up @@ -38,4 +38,15 @@ CREATE TABLE aros (
lft INTEGER(10) DEFAULT NULL,
rght INTEGER(10) DEFAULT NULL,
PRIMARY KEY (id)
);
);

/* this indexes will improve acl perfomance */
CREATE INDEX idx_acos_lft_rght ON `acos` (`lft`, `rhgt`);

CREATE INDEX idx_acos_alias ON `acos` (`alias`);

CREATE INDEX idx_aros_lft_rght ON `aros` (`lft`, `rhgt`);

CREATE INDEX idx_aros_alias ON `aros` (`alias`);

CREATE INDEX idx_aco_id ON `aros_acos` (`aco_id`);
15 changes: 11 additions & 4 deletions lib/Cake/Model/AclNode.php
Expand Up @@ -89,6 +89,8 @@ public function node($ref = null) {
)),
'order' => $db->name("{$type}.lft") . ' DESC'
);

$conditions_after_join = array();

foreach ($path as $i => $alias) {
$j = $i - 1;
Expand All @@ -98,18 +100,23 @@ public function node($ref = null) {
'alias' => "{$type}{$i}",
'type' => 'INNER',
'conditions' => array(
$db->name("{$type}{$i}.lft") . ' > ' . $db->name("{$type}{$j}.lft"),
$db->name("{$type}{$i}.rght") . ' < ' . $db->name("{$type}{$j}.rght"),
$db->name("{$type}{$i}.alias") . ' = ' . $db->value($alias, 'string'),
$db->name("{$type}{$j}.id") . ' = ' . $db->name("{$type}{$i}.parent_id")
$db->name("{$type}{$i}.alias") . ' = ' . $db->value($alias, 'string')
)
);

// it will be better if this conditions will performs after join operation
$conditions_after_join[] = $db->name("{$type}{$j}.id") . ' = ' . $db->name("{$type}{$i}.parent_id");
$conditions_after_join[] = $db->name("{$type}{$i}.rght") . ' < ' . $db->name("{$type}{$j}.rght");
$conditions_after_join[] = $db->name("{$type}{$i}.lft") . ' > ' . $db->name("{$type}{$j}.lft");

$queryData['conditions'] = array('or' => array(
$db->name("{$type}.lft") . ' <= ' . $db->name("{$type}0.lft") . ' AND ' . $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}0.rght"),
$db->name("{$type}.lft") . ' <= ' . $db->name("{$type}{$i}.lft") . ' AND ' . $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}{$i}.rght"))
);
}

$queryData['conditions'] = array_merge($queryData['conditions'], $conditions_after_join);

$result = $db->read($this, $queryData, -1);
$path = array_values($path);

Expand Down

0 comments on commit bb15271

Please sign in to comment.