I have a problem that they made in a database, which I need to solve temporarily, they created several tables with index in the table name, where the system creates a new table with each new user ... probably the developer created this new problem, to address a slowness problem, and probably he has never heard of table indexes, however, I have to make a temporary workaround, and then fix the database. So I am trying to solve this through code, it occurs that I am having difficulty implementing a query, I am using Zend Framework 1.12.20.
This is the Dao Model:
class Application_Model_Dao_DataSchoolMembers
{
private $dbTable;
public function __construct()
{
$this->dbTable = new Application_Model_DbTable_SchoolsMembers();
$this->dbTable->getDefaultAdapter()->setFetchMode(Zend_Db::FETCH_OBJ);
}
public function getDataTableMembers()
{
$selectTopTables = $this->dbTable->select()
->setIntegrityCheck(false);
$selectTopTables->from(array('tb' => 'information_schema.tables'),
array(
'first_number' => 'CONVERT(REPLACE(REPLACE(table_name, "School_",""),"_members",""), UNSIGNED)'
));
$selectTopTables->where('tb.table_name LIKE ? ','%School_%');
$selectTopTables->where('tb.table_name LIKE ? ','%_members%');
$selectTopTables->order(array('first_number ASC'));
$v= $selectTopTables->assemble();
print_r($v);
$data = $this->getAllDados($selectTopTables);
return $data;
}
public function getMemberByFilter(array $param = array())
{
$dataTables = $this->getDataTableMembers();
$parseSql = array();
if (!empty($dataTables)) {
foreach ($dataTables as $key => $value) {
$parseSql[] = 'Select ID, user_id, user_type from School_' . $value['first_number'] . '_members';
}
$sql = '('.implode(' UNION ALL ', $parseSql).')';
$select = $this->dbTable->select()
->setIntegrityCheck(false)
->from(new Zend_Db_Expr($sql).' AS mb',
array(
'mb.ID',
'mb.user_id',
'mb.user_type'
));
if (isset($param['user_id'])) {
// $w[] = 'mb.user_id = ? ';
// $val[] = $param['user_id'];
$select->where('mb.user_id = ? ', $param['user_id']);
}
if (isset($param['id'])) {
// $w[] = 'mb.ID = ? ';
// $val[] = $param['id'];
$select->where('mb.ID = ? ', $param['id']);
}
if (isset($param['user_type'])) {
// $w[] = 'mb.user_type = ? ';
// $val[] = $param['user_type'];
$select->where('mb.user_type = ? ', $param['user_type']);
}
$select->group( array(
'mb.ID',
'mb.user_id',
'mb.user_type'
))->assemble();
var_dump($select); die();
return $this->getAllDados($select);
}
}
}
The SQL of the method: getMemberByFilter()
should look like this:
SELECT mb.ID, mb.user_id, mb.user_type FROM (
Select ID, user_id, user_type from School_1_members
UNION ALL
Select ID, user_id, user_type
from School_2_members
UNION ALL
Select ID, user_id, user_type from School_3_members
UNION ALL Select ID, user_id, user_type
from School_3_members
) as mb
WHERE mb.user_id = ?
group by mb.ID,
mb.user_id,
mb.user_type
But this is not happening, it is giving error.
The tables are basically like this:
CREATE TABLE 'School_1_members' (
'ID' int(11) NOT NULL AUTO_INCREMENT,
'user_id' text COLLATE utf8_unicode_ci NOT NULL,
'user_type' text COLLATE utf8_unicode_ci NOT NULL,
'data' text COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY ('ID')
) ENGINE=MyISAM AUTO_INCREMENT=394 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE 'School_2_members' (
'ID' int(11) NOT NULL AUTO_INCREMENT,
'user_id' text COLLATE utf8_unicode_ci NOT NULL,
'user_type' text COLLATE utf8_unicode_ci NOT NULL,
'data' text COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY ('ID')
) ENGINE=MyISAM AUTO_INCREMENT=394 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE 'School_3_members' (
'ID' int(11) NOT NULL AUTO_INCREMENT,
'user_id' text COLLATE utf8_unicode_ci NOT NULL,
'user_type' text COLLATE utf8_unicode_ci NOT NULL,
'data' text COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY ('ID')
) ENGINE=MyISAM AUTO_INCREMENT=394 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
I also tried another alternative, which was to create a view, or a procedure in the database, to minimize the problem, making only a direct query:
CREATE OR REPLACE VIEW School_X_Members AS (
/* aqui viria a consulta */
);
SET @resultQuery = NULL;
SELECT
GROUP_CONCAT(
DISTINCT
CONCAT('SELECT ID, user_id, user_type FROM ', table_name)
SEPARATOR '\r\nUNION ALL\r\n'
)
INTO
@resultQuery
FROM
information_schema.tables as tb
WHERE (tb.table_name LIKE '%School_%' ) AND (tb.table_name LIKE '%_members%' ) order by
CONVERT(REPLACE(REPLACE(table_name, "School_",""),"_members",""), UNSIGNED);
SELECT @resultQuery;
I just can not make it work, could anyone help me with that, thanks.