我有一个具有不同表的系统:一个存储票证,一个存储部门,另一个存储用户。
前两个表只保留其他人的静态信息(基本上是 id),但是当我检索票证列表时,我还需要收集部门和操作员的名称,此时我运行另一个查询来检索部门的名称并将它们存储在 $_SESSION 中,然后循环遍历它:
检索部门的名称:
function retrive_depa_names($Hostname, $Username, $Password, $DatabaseName, $SupportDepaTable){
if(isset($_SESSION['status']) && $_SESSION['status']<3){
$mysqli = new mysqli($Hostname, $Username, $Password, $DatabaseName);
$stmt = $mysqli->stmt_init();
if($stmt){
$query = "SELECT `id`,`department_name` FROM ".$SupportDepaTable;
$prepared = $stmt->prepare($query);
if($prepared){
if($stmt->execute()){
$stmt->store_result();
$result = $stmt->bind_result($camaro, $mustang);
if($stmt->num_rows>0){
$_SESSION['departments']=array();
while (mysqli_stmt_fetch($stmt))
$_SESSION['departments'][$camaro]=$mustang;
}
}
}
}
$mysqli->close();
}
}
提取部门名称:
while (mysqli_stmt_fetch($stmt)) {
/*Departments Name*/
if(isset($_SESSION['departments'][$depid]))
$depname=$_SESSION['departments'][$depid];
else{
retrive_depa_names($Hostname, $Username, $Password, $DatabaseName, $SupportDepaTable);
if(isset($_SESSION['departments'][$depid]))
$depname=$_SESSION['departments'][$depid];
else
$depname='Unknown';
}
[..]
}
同样的事情也发生在运营商身上。
我想知道这是否是一个好方法,或者最好使用 JOIN 将所有工作交给查询(检索票证列表的那个),因为我使用的方法是第一个负载很重,但之后应该更容易,问题是它不跟踪其他用户可以做的更改。
基本上我想知道什么方法可以避免大量使用服务器资源。
谢谢
编辑
这些是表格:
CREATE TABLE IF NOT EXISTS `razorphyn_support_list_tickets` (
`id` BIGINT(15) UNSIGNED NOT NULL AUTO_INCREMENT,
`enc_id` CHAR(87),
`ref_id` VARCHAR(18),
`department_id` BIGINT(11) UNSIGNED NOT NULL,
`operator_id` BIGINT(11) UNSIGNED NOT NULL DEFAULT 0,
`user_id` BIGINT(11) UNSIGNED NOT NULL,
`title` VARCHAR(255) NOT NULL,
`priority` INT(2) UNSIGNED NOT NULL,
`website` VARCHAR(200) NOT NULL,
`contype` ENUM('0','1','2','3','4','5') NOT NULL DEFAULT '0',
`ftp_user` VARCHAR(60) NOT NULL,
`ftp_password` VARCHAR(60) NOT NULL,
`created_time` DATETIME NOT NULL,
`last_reply` DATETIME NOT NULL,
`ticket_status` ENUM('0','1','2','3') NOT NULL DEFAULT '2',
`operator_rate` DECIMAL(4,2) UNSIGNED,
PRIMARY KEY (`id`),
UNIQUE KEY (`user_id`,`title`),
INDEX (`enc_id`,`department_id`,`operator_id`,`user_id`,`ticket_status`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=20;
CREATE TABLE IF NOT EXISTS `razorphyn_support_departments` (
`id` BIGINT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`department_name` VARCHAR(70) NOT NULL,
`active` ENUM('0','1') NOT NULL DEFAULT '1',
`public_view` ENUM('0','1') NOT NULL DEFAULT '1',
PRIMARY KEY (`id`),
UNIQUE KEY (`department_name`),
INDEX (`id`,`department_name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=0;
CREATE TABLE IF NOT EXISTS `razorphyn_support_users` (
`id` BIGINT(15) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) NOT NULL,
`mail` VARCHAR(50) NOT NULL,
`password` VARCHAR(200) NOT NULL,
`reg_key` VARCHAR(260) ,
`tmp_password` VARCHAR(87) ,
`ip_address` VARCHAR(50) NOT NULL,
`status` ENUM('0','1','2','3','4') NOT NULL DEFAULT '3',
`holiday` ENUM('0','1') NOT NULL DEFAULT '0',
`mail_alert` ENUM('no','yes') NOT NULL DEFAULT 'yes',
`assigned_tickets` INT(5) UNSIGNED NOT NULL DEFAULT 0,
`solved_tickets` BIGINT(11) UNSIGNED NOT NULL DEFAULT 0,
`number_rating` BIGINT(6) UNSIGNED NOT NULL DEFAULT 0,
`rating` DECIMAL(4,2) UNSIGNED NOT NULL DEFAULT 0,
PRIMARY KEY (`id`),
UNIQUE KEY(`mail`),
INDEX (`name`,`mail`,`status`,`holiday`,`assigned_tickets`,`solved_tickets`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=55;
附加信息
这是检索报价单列表的主要查询:
$query = "SELECT `user_id`,`enc_id`,`department_id`,`operator_id`,`title`,CASE priority WHEN '0' THEN 'Low' WHEN '1' THEN 'Medium' WHEN '2' THEN 'High' WHEN '3' THEN 'Urgent' WHEN '4' THEN 'Critical' ELSE priority END,`created_time`,`last_reply`,CASE `ticket_status` WHEN '0' THEN '<span class=\'label label-success\'>Closed</span>' WHEN '1' THEN '<span class=\'label label-important\'>Open</span>' WHEN '2' THEN '<span class=\'label label-warning\'>To Assign</span>' WHEN '3' THEN '<span class=\'label label-important\'>Reported</span>' ELSE 'Error' END FROM ".$SupportTicketsTable." ORDER BY `last_reply` DESC LIMIT 350";
其中:department_id
是id
部门表的user_id
,operator_id
是id
用户表的(operator_id
可以status
等于1
或2
)